Home > Mobile >  R ggplot2 geom_point disappear
R ggplot2 geom_point disappear

Time:07-14

When I try the code below on the following data, the points disappear on the plot.

What could be causing this?

Sample data (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"))

Code:

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))

CodePudding user response:

It is due to your alpha. Change it to 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))

enter image description here

  • Related