Bars in geom_bar have unwanted different widths when using facet_wrap

I can not find a solution for the following problem. I would appreciate help!

The following code creates charts using a facet. However, due to the "extra space" ggplot2 has in some groups, it makes the bars much wider, even if I specify a width of 0.1 or similar. I find it very annoying because it makes him very unprofessional. I want all the bars to look the same (except fill). Hope someone can tell me how to fix this.

Secondly, how can I change the order of different classes in facet windows so that the order is always C1, C2 ... C5, M, F, where applicable. I tried it with ordering factor levels, but since not all classes are present in every part of the graph, this did not work, or at least I assume that was the reason.

Thirdly, how can I reduce the space between the bars? So the whole graph is more compressed. Even if I reduce the image for export, R will reduce the size of the bars, but the gaps between the bars are still huge.

I would appreciate feedback for any of these answers!

My details: http://pastebin.com/embed_iframe.php?i=kNVnmcR1

My code is:

library(dplyr) library(gdata) library(ggplot2) library(directlabels) library(scales) all<-read.xls('all_auto_visual_c.xls') all$station<-as.factor(all$station) #all$group.new<-factor(all$group, levels=c('C. hyperboreus','C. glacialis','Special Calanus','M. longa','Pseudocalanus sp.','Copepoda')) allp <- ggplot(data = all, aes(x=shortname2, y=perc_correct, group=group,fill=sample_size)) + geom_bar(aes(fill=sample_size),stat="identity", position="dodge", width=0.1, colour="NA") + scale_fill_gradient("Sample size (n)",low="lightblue",high="navyblue")+ facet_wrap(group~station,ncol=2,scales="free_x")+ xlab("Species and stages") + ylab("Automatic identification and visual validation concur (%)") + ggtitle("Visual validation of predictions") + theme_bw() + theme(plot.title = element_text(lineheight=.8, face="bold", size=20,vjust=1), axis.text.x = element_text(colour="grey20",size=12,angle=0,hjust=.5,vjust=.5,face="bold"), axis.text.y = element_text(colour="grey20",size=12,angle=0,hjust=1,vjust=0,face="bold"), axis.title.x = element_text(colour="grey20",size=15,angle=0,hjust=.5,vjust=0,face="bold"), axis.title.y = element_text(colour="grey20",size=15,angle=90,hjust=.5,vjust=1,face="bold"),legend.position="none", strip.text.x = element_text(size = 12, face="bold", colour = "black", angle = 0), strip.text.y = element_text(size = 12, face="bold", colour = "black")) allp #ggsave(allp, file="auto_visual_stackover.jpeg", height= 11, width= 8.5, dpi= 400,) 

The current chart that needs some fixing:

enter image description here

Thank you so much!

+7
r ggplot2 bar-chart geom-bar
source share
2 answers

Assuming that the stroke width is inversely proportional to the number of x-breaks, the corresponding scaling factor can be built into the geom_bar() function. But first, calculate the number of x-gaps in each panel, calculate the scaling factor and return them to all the β€œdata”.

Ggplot2 2.0.0 update Each column mentioned in facet_wrap gets its own row in the strip. As amended, the new label variable is configured in the data frame so that the strip label remains on the same line.

 # Calculate scaling factor and insert into data frame library(plyr) N = ddply(all, .(station, group), function(x) length(row.names(x))) N$Fac = N$V1 / max(N$V1) all = merge(all, N[,-3], by = c("station", "group")) all$label = paste(all$group, all$station, sep = ", ") allp <- ggplot(data = all, aes(x=shortname2, y=perc_correct, group=group,fill=sample_size)) + # Adjust the bar widths according to the scaling factor geom_bar(aes(width = .5*Fac), stat="identity", position="dodge", colour="NA") + scale_fill_gradient("Sample size (n)",low="lightblue",high="navyblue")+ facet_wrap(~label,ncol=2,scales="free_x")+ # facet_wrap(group~station,ncol=2,scales="free_x")+ xlab("Species and stages") + ylab("Automatic identification and visual validation concur (%)") + ggtitle("Visual validation of predictions") + theme_bw() + theme(plot.title = element_text(lineheight=.8, face="bold", size=20,vjust=1), axis.text.x = element_text(colour="grey20",size=12,angle=0,hjust=.5,vjust=.5,face="bold"), axis.text.y = element_text(colour="grey20",size=12,angle=0,hjust=1,vjust=0,face="bold"), axis.title.x = element_text(colour="grey20",size=15,angle=0,hjust=.5,vjust=0,face="bold"), axis.title.y = element_text(colour="grey20",size=15,angle=90,hjust=.5,vjust=1,face="bold"), legend.position="none", strip.text.x = element_text(size = 12, face="bold", colour = "black", angle = 0), strip.text.y = element_text(size = 12, face="bold", colour = "black")) allp 

enter image description here

+5
source share

Here's what I did after Gregor's suggestion. Using geom_segment and geom_point makes a good graph as I think.

 library(ggplot2) all<-read.xls('all_auto_visual_c.xls') all$station<-as.factor(all$station) all$group.new<-factor(all$group, levels=c('C. hyperboreus','C. glacialis','Combined','M. longa','Pseudocalanus sp.','Copepoda')) all$shortname2.new<-factor(all$shortname2, levels=c('All','F','M','C5','C4','C3','C2','C1','Micro', 'Oith','Tric','Cegg','Cnaup','C3&2','C2&1')) allp<-ggplot(all, aes(x=perc_correct, y=shortname2.new)) + geom_segment(aes(yend=shortname2.new), xend=0, colour="grey50") + geom_point(size=4, aes(colour=sample_size)) + scale_colour_gradient("Sample size (n)",low="lightblue",high="navyblue") + geom_text(aes(label = perc_correct, hjust = -0.5)) + theme_bw() + theme(panel.grid.major.y = element_blank()) + facet_grid(group.new~station,scales="free_y",space="free") + xlab("Automatic identification and visual validation concur (%)") + ylab("Species and stages")+ ggtitle("Visual validation of predictions")+ theme_bw() + theme(plot.title = element_text(lineheight=.8, face="bold", size=20,vjust=1), axis.text.x = element_text(colour="grey20",size=12,angle=0,hjust=.5,vjust=.5,face="bold"), axis.text.y = element_text(colour="grey20",size=12,angle=0,hjust=1,vjust=0,face="bold"), axis.title.x = element_text(colour="grey20",size=15,angle=0,hjust=.5,vjust=0,face="bold"), axis.title.y = element_text(colour="grey20",size=15,angle=90,hjust=.5,vjust=1,face="bold"),legend.position="none", strip.text.x = element_text(size = 12, face="bold", colour = "black", angle = 0), strip.text.y = element_text(size = 8, face="bold", colour = "black")) allp ggsave(allp, file="auto_visual_no_label.jpeg", height= 11, width= 8.5, dpi= 400,) 

This is what it produces!

enter image description here

+6
source share

All Articles