R语言 视障人士如何更好地区分ggplot中的线条?

qxgroojn  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(152)

在运行下面的代码时,我试图更好地描绘和可视化情节线,以帮助视觉受损的人(像我一样,色盲和视野盲点)。即使是目前呈现的虚线看起来也太相似了,我几乎无法区分它们。如何添加标记和其他区别特征,如下图所示,使其更容易阅读?

代码:

library(feasts)
library(fable)
library(ggplot2)
library(tsibble)

tmp <- data.frame(
  Month = c(1,2,3,4,5,6,7,8,9,10),
  StateX = c(1527,1297,933,832,701,488,424,353,302,280)
  ) %>%
  as_tsibble(index = Month)
tmpNext4 <- data.frame(
  Month = c(11,12,13,14),
  StateX = c(211,182,153,125)
  ) %>%
  as_tsibble(index = Month)

# Fit the models to tmp dataframe:
fit <- tmp %>%
  model(
    Mean = MEAN(StateX),
    `Naïve` = NAIVE(StateX),
    Drift = NAIVE(StateX ~ drift())
  )

# Produce forecasts for the next 4 months:
fcTmp <- fit %>%
  forecast(new_data = tmpNext4)

# Plot the forecasts:
fcTmp %>%
  ggplot(aes(Month, .mean)) +
  geom_line(aes(linetype = .model, color = .model)) +
  geom_line(aes(y = StateX, linetype = "Actual", color = "Actual"), data = tmpNext4) +
  geom_line(aes(y = StateX), data = tmp)
m1m5dgzv

m1m5dgzv1#

困难的部分是将图例一分为二,我们可以使用ggnewscale::new_scale_color()geom_line创建一个新的图层,其中包含实际数据。您可以在answers中找到其他方法来完成此操作。

  • 为了表示不同的点,我们使用geom_point(aes(shape = .model, color = .model))
  • 为了使线更粗,我们使用geom_line(..., linewidth = 1.2)
  • scales::hue_pal()被使用,因此我们可以保持ggplot2原始颜色
# Produce forecasts for the next 4 months:
fcTmp <- fit %>%
  forecast(new_data = tmpNext4)

legend_title = "Forecast"
# Plot the forecasts:
fcTmp %>%
  ggplot(aes(Month, .mean)) +
  geom_line(aes(linetype = .model, color = .model)) +
  geom_point(aes(shape = .model, color = .model)) + 
  scale_color_manual(aesthetics = "colour",values = scales::hue_pal()(4)[-1]) + 
  labs(linetype =legend_title ,shape = legend_title, colour = legend_title) +
  ggnewscale::new_scale_color() +
  geom_line(aes(y = StateX, color = "Actual"), data = tmpNext4,  linewidth = 1.2) +
  geom_line(aes(y = StateX), data = tmp, linewidth = 1.2)  +
  scale_color_manual(aesthetics = "colour",values = scales::hue_pal()(1), name = "")

Created on 2023-01-24 with reprex v2.0.2

相关问题