R-CoderDotCom / calendR

Ready to print calendars with ggplot2
https://r-coder.com/calendar-plot-r/
MIT License
255 stars 35 forks source link

Legend options #22

Closed jeremiah-sanville closed 1 year ago

jeremiah-sanville commented 1 year ago

Could you make it possible to have to remove the numerical part of the legend scale?

Another option that would be helpful would be to assign a gradient scale maximum. For example, I'm plotting the difference between two types of users and amount of rides done each day. The two calendars have different maximums and the gradient is misleading when comparing both calendars. I attached the two calendars produced using the package. calendr2 calendr1

mschilli87 commented 1 year ago

Could you share the code to generate the plots so helping you won't require reverse engineering what you have done? :wink:

jeremiah-sanville commented 1 year ago

Could you share the code to generate the plots so helping you won't require reverse engineering what you have done? 😉

Yes, of course!

calendR(start_date = "2021-10-01",
        end_date   = "2022-09-30",
        title = "Daily Cyclistic activity for casual riders",  
        title.size = 20,                  
        title.col = 1,                   
        subtitle = "Data used from 10/2021-09/2022",
        subtitle.size = 10,
        col = "white",                  
        lwd = 1,                          
        lty = 1,                          
        mbg.col = 4,                      
        months.col = "white",             
        font.family = "mono",                              
        font.style = "bold",              
        weeknames.col = "black",         
        days.col = 1,                     
        day.size = 3.5,                 
        bg.col = "white",
        special.days = ride_times_casual$frequency,     
        gradient = TRUE,                  
        special.col = "purple",             
        legend.pos = "right",            
        legend.title = "Legend",            
        orientation = "portrait")         
calendR(start_date = "2021-10-01",
        end_date   = "2022-09-30",
        title = "Daily Cyclistic activity for annual members",  
        title.size = 20,                  
        title.col = 1,                    
        subtitle = "Data used from 10/2021-09/2022",
        subtitle.size = 10,
        col = "white",                  
        lwd = 1,                          
        lty = 1,                          
        mbg.col = 4,                     
        months.col = "white",            
        font.family = "mono",                             
        font.style = "bold",              
        weeknames.col = "black",          
        days.col = 1,                     
        day.size = 3.5,                   
        bg.col = "white",
        special.days = ride_times_member$frequency,     
        gradient = TRUE,                  
        special.col = "purple",             
        legend.pos = "right",             
        legend.title = "Legend",            
        orientation = "portrait")

Here is what ride_times_casual and ride_times_member looks like. They go from 2021-10-01 to 2022-09-30

> head(ride_times_casual)
        date  frequency
1 2021-10-01 0.07435735
2 2021-10-02 0.07916236
3 2021-10-03 0.06178044
4 2021-10-04 0.03840923
5 2021-10-05 0.04209163
6 2021-10-06 0.04477478

Thank you!

mschilli87 commented 1 year ago

Note that this still does not allow generating you plot since the data are not shared completely.

I ended up generating my own (fake) data to reconstruct your problem:

library(lubridate)

start <- "2021-10-01"
end <- "2022-09-30"

set.seed(42)

ride_times_casual <-
  data.frame(date = as.character(seq(ymd(start), ymd(end), by = "day")),
             frequency = rnorm(ymd(end) - ymd(start) + 1L, .5, .1))

range(ride_times_casual$frequency)
[1] 0.2006909917 0.7701891000

From what I understand from your problem, the issue is that those ranges are not comparable across your two calendars but determine the gradient limits by default:

library(calendR)

cal <-
  calendR(start_date = start,
          end_date = end,
          title = "Daily Cyclistic activity for casual riders",  
          title.size = 20,                  
          title.col = 1,                   
          subtitle = "Data used from 10/2021-09/2022",
          subtitle.size = 10,
          col = "white",                  
          lwd = 1,                          
          lty = 1,                          
          mbg.col = 4,                      
          months.col = "white",             
          font.family = "mono",                              
          font.style = "bold",              
          weeknames.col = "black",         
          days.col = 1,                     
          day.size = 3.5,                 
          bg.col = "white",
          special.days = ride_times_casual$frequency,
          gradient = TRUE,                  
          special.col = "purple",             
          legend.pos = "right",            
          legend.title = "Legend",            
          orientation = "portrait")

cal

cal

However, you can already use basic ggplot2 functionality to manually overwrite those limits to match them across calendars:

library(ggplot2)
cal + expand_limits(fill = 0:1)

cal2

I hope this solves your issue and you also learned a bit about how create a reprocible example for the next time you are asking for help. :wink:

jeremiah-sanville commented 1 year ago

Thank you mschilli87, I appreciate it! This worked perfectly. And yes, I'll make my next help post with a reproducible example :)

jeremiah-sanville commented 1 year ago

I also found that scale_fill_gradientn also works for this issue:

cal + scale_fill_gradientn(colours=c("white","purple"),limits=c(0,0.125))

image