Agus camacho Agus camacho - 3 months ago 8
R Question

boxplots along a continous axis in ggplot2

I have the following dataset.

And want to plot it using

ggplot2
, but plotting the boxplots in a continous scale.

However, I could not find a way to do that. This is the furthest I could get:

ggplot(t, aes(x=treatment, y=loss)) +
scale_fill_manual(values=cbPalette)+
geom_boxplot(fill="white") +
geom_point(col="blue")+
scale_x_discrete(labels=c("35.9°C\nmedian\nPBT","40.8°C\n95perc \nPBT","42.6°C\n median\nVMT"))+
ylab("weight lost(g)")+
xlab("")+
theme(panel.background = element_rect(fill='transparent', colour='black'),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_text(colour="black", size=14),
axis.text.x = element_text(angle=0, vjust=0.5,colour="black", size=14),
axis.title.y = element_text(colour="black", size=14),
axis.text.y = element_text(angle=0, vjust=0.5,colour="black", size=14)
)+
theme(legend.position="none")+
annotate("text", size=5, x = 0.5,y= 25, label = "A")

Answer

Column loss is missing from the dataset: I presume it is the difference between pre and post weights (which are expressed in Kg in the dataset). I also presume that by "continuous scale" you mean that the treatment is represented as a real number, not as a factor on the x-axis. If that is the case, then it is enough to add a group statement.

## add loss (in gr)
t <- t %>% mutate(loss=-1e3*(post.weight-pre.weight))

p <- ggplot(t, aes(x=treatment, y=loss, group=treatment)) +
   scale_fill_manual(values=cbPalette)+
   geom_boxplot(fill="white") +
   geom_point(col="blue")+
   ylab("weight lost(g)")+
   xlab("")+
   theme(panel.background = element_rect(fill='transparent', colour='black'),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title.x = element_text(colour="black", size=14),
        axis.text.x  = element_text(angle=0, vjust=0.5,colour="black",   size=14),
        axis.title.y = element_text(colour="black", size=14),
        axis.text.y  = element_text(angle=0, vjust=0.5,colour="black", size=14)
        )+ 
  theme(legend.position="none")
Comments