Reputation: 73
I'm publishing a paper in a scientific journal with some boxplots, I have two figures, and they have different numbers of boxplots on them. I want the plot bars to be the same width. I don't want to control the width of the entire plot, but of the bars.
Here's what I've got:
Clades<-c("C-A","C-A","C-A","C-A","C-A","C-A","C-A","C-A","C-A","C-B","C-B","C-B","C-B","C-B","C-B","C-B","C-B","C-C","C-C","C-C","C-C","C-C","C-C","C-C","C-C","C-D","C-D","C-D","C-D","C-D","C-D","C-D","C-D","G-A","G-A","G-A","G-A","G-A","G-A","G-A","G-A","G-A","G-A","G-A","G-B","G-B","G-B","G-B","G-B","G-B","G-B","G-B","G-B","G-B","G-C","G-C","G-C","G-C","G-C","G-C","G-C","G-D","G-D","G-D","G-D","G-D","G-D","G-D","G-D","G-D","G-D","G-D","G-E","G-E","G-E","G-E","G-E","G-E","G-E","G-E","G-E","G-E")
NoGClades<-c("C-A","C-A","C-A","C-A","C-A","C-A","C-A","C-A","C-A","C-B","C-B","C-B","C-B","C-B","C-B","C-B","C-B","C-C","C-C","C-C","C-C","C-C","C-C","C-C","C-C","C-D","C-D","C-D","C-D","C-D","C-D","C-D","C-D","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G")
HapDiv<-c(12,23,12,34,12,34,12,34,12,115,123,12,34,45,123,3,23,13,156,14,5,14,14,11,12,12,34,12,34,12,115,123,12,34,45,123,3,23,12,23,12,12,23,12,123,12,34,45,12,34,12,12,34,12,13,156,14,5,12,34,45,115,123,12,14,14,11,34,12,34,12,34,123,3,23,12,23,12,12,13,156,14)
Haplotypes<-data.frame(Clades,NoGClades,HapDiv) # create data frame
plot(Haplotypes$Clades, Haplotypes$HapDiv,
xlab="Clade", ylab="Haplotype Diversity",
main="Haplotype Diversity with Clades in G")
plot(Haplotypes$NoGClades, Haplotypes$HapDiv,
xlab="Clade", ylab="Haplotype Diversity",
main="Haplotype Diversity with no Clades in G")
Because there are different numbers of bars - the individual bars are different widths in the two plots. How do I control their width so it is exactly equal?
In general, I'd like to have a straightforward way to control the look of the boxplots, as well.
Upvotes: 0
Views: 943
Reputation: 81683
You can use ggplot2
to create the plot.
Change you data to the long format with tidyr
:
library(tidyr)
dat <- gather(Haplotypes, variable, value, -HapDiv)
Rename the factor levels:
levels(dat$variable) <- c("Haplotype Diversity with Clades in G",
"Haplotype Diversity with no Clades in G")
Plot:
library(ggplot2)
ggplot(dat, aes(x = value, y = HapDiv)) +
geom_boxplot() +
facet_grid(. ~ variable, scales = "free_x", space = "free_x") +
xlab("Clade") +
theme_bw()
Upvotes: 2