2

I am trying to plot individual boxplots per specimen, grouped and coloured by gender. However, the apparently logical code plots in 3 colors when I only have two genders.

Here is the dataset:

https://www.dropbox.com/s/vm6bdiqmymi5jbe/methodissues.csv?dl=0

Here is the code:

VMT=read.csv("methodissues.csv")
ggplot(VMT, aes(x=sex,group=specimen,fill=sex, y=VMT)) +
geom_boxplot(width = 1) +
xlab("specimens")+
ylab("VMT (°C)")+
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"),
     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.justification=c(1,1), legend.position="none")
2
  • What do you mean by 3 colors? The boxes are all black and white...? I guess you forgot to set aes(..., color = specimen) right?
    – Manuel R
    Jun 1, 2016 at 6:25
  • Nearly that, i actually forgot to set fill=sex, now it is edited and the problem is actually reproducible., but is not solved. Jun 1, 2016 at 13:43

1 Answer 1

0

The problem is that you have a "conflict of interest". You want the boxes to be grouped by specimen (which results in a box based on at the maximum only 4 values in VMT. More on that below.). At the same time, however, you want each box colored by sex, which is impossible to do in some cases (like when specimen is equal to C5MT), since there are some values in specimen that are related to both sexes M and F. Thats where the third color is comming from. It is simply a mixture of both colours used to color sex.

Possible solutions:

1. Fill by sex and put specimen on the x-axis

require(ggplot2)

 VMT=read.csv("methodissues.csv")

ggplot(VMT, aes(x = specimen, y = VMT, fill = sex)) +
  geom_boxplot(width = 1) 

enter image description here

2. Facet by specimen

ggplot(VMT, aes(x = sex, fill = sex, y = VMT)) +
  geom_boxplot(width = 1) +
  facet_grid(~ specimen)

enter image description here

Word of caution:

I would strongly recommend to rethink what you are actually trying to say. I dont think a graph like that is suitable, since each box is only based on a maximum of 4 values which makes it pretty much meaningless (after all, you are trying to convey some sense of the distribution). Some "boxes" (like the one for specimen = C5MT and sex = F ) are even only made up of 1 value (and hence, for males it's only 3). This makes looking at the graph even more dubious.

1
  • thanks, but that does not group the boxplots by sexes, one at each side. May 31, 2016 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.