Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

r - Bars in geom_bar have unwanted different widths when using facet_wrap

I can'd find a solution for the following problem(s). I would appreciate some help a lot!

The following code produces bar charts using facet. However, due to "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 that very annoying since it makes it look very unprofessional. I want all the bars to look the same (except for the fill). I hope somebody can tell me how to fix this.

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

Thirdly, how can I reduce the space between the bars? So that the whole graph is more compressed. Even if I make the image smaller for exporting, R will scale the bars smaller but the spaces between the bars are still huge.

I would appreciate feedback for any of those answers!

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

My Code:

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 graph that needs some fixing:

enter image description here

Thanks a lot!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here what I did after suggestion from Gregor. Using geom_segment and geom_point makes a nice 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...