Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot boxplot multiple groups of y with continuous x

Tags:

r

ggplot2

I want to produce a boxplot for multiple groups y with continuous x. I give an example as follows:

library(ggplot2)
ggplot(diamonds, aes(carat, price)) +
  geom_boxplot(aes(group = cut_width(carat, 0.25), colour=color))

I get the figure as follow: enter image description here

However, the 'colour' does not work here. Could anybody help me to fix this problem? Thanks in advance.

like image 937
Lin Avatar asked Sep 18 '25 04:09

Lin


1 Answers

You need to also group by color (otherwise it will group different colors together):

ggplot(diamonds, aes(carat, price)) +
    geom_boxplot(aes(group = interaction(cut_width(carat, 0.25), color), colour=color))

enter image description here

like image 180
Axeman Avatar answered Sep 19 '25 20:09

Axeman