Home > Net >  Grouped density plot fill colors disappear when using coord_cartesian() function
Grouped density plot fill colors disappear when using coord_cartesian() function

Time:05-27

I'm currently facing a problem that's giving me headaches and I can't really figure out the solution.

Basically, I'm drawing a grouped density plot (grouping variable is 'member_casual', categorical variable with 2 levels). The output I want is two density curves on the same panel (no facets). The code I wrote so far works fine, and plots 2 overlapping density curves with 2 different colors.

However the right tails of these 2 curves are really too flat, so i used coord_cartesian() in order to set the x-axis limits. I don't want to trim the right tails obs. because doing this would change my density curves. I just want to remove the tails from the plot by zooming on it, so coord_cartesian() is actually my only option.

The problem is: if I use coord_cartesian() the density curves fill colors just disappear and I get the same 2 curves with transparent areas. What am I doing wrong?

Up to now, i tried:

  • Moving coord_cartesian() code chunk immediately after geom_density().
  • Removing the theme_stata() chunk.

Here a subset of my data (hope 30 rows are enough, I also removed all unnecessary variables):

noGeo_data <- structure(list(ride_id = c("1E1D708751A45FBB", "EF150999D6799927", 
"F01C5D9474B37874", "3241CB12D978A919", "3C1E34B72CF1DF6F", "7780E5A193F16BAA", 
"A1250C4C180BDB7A", "DCE53F03D9E930BC", "C1C200854F4A4289", "4613417749115AB3", 
"F02EF690EDD4F285", "39A8B97FBCDE17F0", "6D758C3BA9CD9ACC", "451AFECBD520C5F6", 
"A064FF122A657C26", "0F4D542DEF5C14BE", "A8621985220E1873", "2E5E911966CE495B", 
"ABBBC8112D862E07", "D4B96FE29C9BA6F0", "F8990692B6E4537A", "E852C8A32A5F1A81", 
"0B6BAF9C856FC155", "44B43295891ECBF8", "1DFCB9DF65688E01", "0E2FA7FEF3BC9659", 
"254204CC5A9514F4", "06457F2DE65545D2", "F457BEE1E71539B1", "971A92DBAFBE083D", 
"330440548027D078"), ride_length = c(184, 1705, 1722, 1872, 13262, 
848, 910, 1034, 35, 3639, 132, 1988, 2874, 2729, 1948, 475, 867, 
1847, 2676, 116, 2136, 3499, 39763, 88, 2470, 1744, 1760, 1609, 
3064, 44, 1223), member_casual = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L), .Label = c("casual", 
"member"), class = "factor")), row.names = c(NA, -31L), class = c("tbl_df", 
"tbl", "data.frame"))

Below the code I wrote so far.

library(tidyverse)
library(ggthemes)

p_rLength <- ggplot(noGeo_data)  
  
  geom_density(
    aes(x = ride_length/60, fill = member_casual, color = member_casual),
    size = 0.8,
    alpha = 0.70
  )  
  
  scale_fill_manual(values = c("#400F73FF", "#FD9567FF"))  
  scale_color_manual(values = c("#400F73FF", "#FD9567FF"))  
  
  scale_x_continuous(breaks = seq(0,150,30))  
  scale_y_continuous(breaks = seq(0.00,0.15,0.01))  
  
  ### coord_cartesian(xlim = c(0,150))        # error ??
  
  theme_stata()  
  
  theme(
    legend.position = c(.85,.9),
    legend.background = element_rect(color = "whitesmoke"),
    legend.title = element_blank(),
    title = element_text(face = "bold"),
    axis.title = element_text(face = "bold", size = 12),
    panel.background = element_rect(color = "black", fill = "gray80"),
    panel.grid.major.y = element_line(color = "whitesmoke"),
    panel.grid.major.x = element_line(color = "whitesmoke"),
    panel.grid.minor.x = element_line(color = "whitesmoke")
  )  
  
  ggtitle(label = "Ride length density plot", 
          subtitle = "Grouped by member_casual")  
  
  xlab("ride length (minutes)")

CodePudding user response:

@I_O answered my question. This is an actual bug, and it's caused by geom_density() alpha parameter. https://github.com/tidyverse/ggplot2/issues/4498

The bug happens when alpha < 1. As a workaround you can set the graphics device backend to AGG in Rstudio global options, and the issue no longer occurs.

  • Related