【问题标题】:R ggplot2 geom_point disappearR ggplot2 geom_point 消失
【发布时间】:2022-07-13 21:48:20
【问题描述】:

当我在以下数据上尝试下面的代码时,点在图上消失了。

这可能是什么原因造成的?

样本数据 (df):

structure(list(year = 1980:2021, AvgTMin = c(67.2833594069828, 
66.1884293276104, 69.5033078337241, 67.5901404824569, 67.6575292661207, 
67.5105525807862, 68.5233499715776, 68.3510672593276, 68.326288756069, 
68.32559571275, 69.857649104181, 70.0254370357328, 68.3083094663621, 
68.7337793145086, 70.7775163238103, 69.2169034146552, 68.1480784827241, 
69.9324034314914, 69.9568689931379, 68.2360121145603, 68.5744250002069, 
68.6254430718603, 69.2027051957672, 68.6790086484733, 68.4194284208621, 
67.9075792408276, 68.2006094435, 69.0469430709138, 68.7260982544224, 
68.2848526742414, 66.6119982602621, 69.5211913653448, 68.8781957073965, 
69.8467482637586, 68.9918181213448, 71.6258828067759, 70.4352370787155, 
70.1039310821983, 69.8729905482241, 70.6863166494138, 70.9844774828103, 
70.237952471931)), row.names = c(NA, -42L), class = c("tbl_df", 
"tbl", "data.frame"))

代码:

library(tidyverse)

df %>% 
  ggplot(aes(x = year, y = AvgTMin)) +
  geom_point(aes(color = "Temperature"), size = 2, shape = 1, alpha = 0.1) +
  geom_smooth(method = lm, aes(linetype = "LM"), se = FALSE, color = "red") +
  scale_linetype_manual(values = 2, name = NULL) +
  scale_colour_manual(values = "deepskyblue4", name = NULL) +
  theme(text = element_text(size = 16)) +
  xlab("Year") +
  ylab("Minimum Temperature (F)") +
  ggtitle("1980-2021 Historical Trends") +
  guides(color = guide_legend(override.aes = list(alpha = 0.5), order = 1))

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这是由于您的 Alpha。将其更改为 alpha 0.5:

    library(tidyverse)
    
    df %>% 
      ggplot(aes(x = year, y = AvgTMin)) +
      geom_point(aes(color = "Temperature"), size = 2, shape = 1, alpha = 0.5) +
      geom_smooth(method = lm, aes(linetype = "LM"), se = FALSE, color = "red") +
      scale_linetype_manual(values = 2, name = NULL) +
      scale_colour_manual(values = "deepskyblue4", name = NULL) +
      theme(text = element_text(size = 16)) +
      xlab("Year") +
      ylab("Minimum Temperature (F)") +
      ggtitle("1980-2021 Historical Trends") +
      guides(color = guide_legend(override.aes = list(alpha = 0.5), order = 1))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多