R: how to fill boxplot depending on value

Advertisements

I have a little problem. I would like to sort boxplot colors properly. For lower values cooler colors, for higher – warmer. There is wrong sorting in my example.

Is it any ideas how to do this?

library(ggplot2)

#  data
set.seed(123)  
data <- data.frame(tavg = sample(-30:30, 2000, replace = TRUE),
                   attendance = sample(100:30000, 1000, replace = TRUE))


my_colors <- viridisLite::viridis(length(unique(cut(data$tavg, seq(-20, 30, 5)))))


ggplot(data, aes(x = cut(tavg, seq(-20, 30, 5)), 
                 y = attendance, 
                 fill = factor(cut(tavg, seq(-20, 30, 5)), 
                 levels = unique(cut(tavg, seq(-20, 30, 5))))))+
         geom_boxplot(outlier.shape = NA) +
         geom_jitter(color = "black", size = 0.4, alpha = 0.1) +
         scale_fill_manual(values = my_colors) +
         labs(x = "Temperature", y = "Attendance", fill="Sequence") +
         ggtitle("Title") +
         theme_minimal()

>Solution :

The issue is that using the unnecessary factor(cut(tavg, seq(-20, 30, 5)), levels = unique(cut(tavg, seq(-20, 30, 5)) you messed up the order of the properly ordered factor returned by cut. Overall I would suggest to add a column with the binned values once to your data which could then be mapped on x and fill:

library(ggplot2)

#  data
set.seed(123)

data <- data.frame(
  tavg = sample(-30:30, 2000, replace = TRUE),
  attendance = sample(100:30000, 1000, replace = TRUE)
)

data$tavg_binned <- cut(data$tavg, seq(-30, 30, 5), include.lowest = TRUE)

my_colors <- viridisLite::viridis(length(levels(data$tavg_binned)))

ggplot(data, aes(
  x = tavg_binned,
  y = attendance,
  fill = tavg_binned
)) +
  geom_boxplot(outlier.shape = NA) +
  geom_jitter(color = "black", size = 0.4, alpha = 0.1) +
  scale_fill_manual(values = my_colors) +
  labs(x = "Temperature", y = "Attendance", fill = "Sequence") +
  ggtitle("Title") +
  theme_minimal()

Leave a Reply Cancel reply