Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: how to fill boxplot depending on value

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading