【发布时间】:2015-07-23 15:18:57
【问题描述】:
我找不到以下问题的解决方案。我将不胜感激!
以下代码使用 facet 生成条形图。然而,由于“额外空间” ggplot2 在某些组中它使条更宽,即使我指定宽度为 0.1 或类似。我觉得这很烦人,因为它看起来很不专业。我希望所有的条看起来都一样(填充除外)。我希望有人能告诉我如何解决这个问题。
其次,如何重新排序构面窗口中的不同类,以便在适用的情况下顺序始终为 C1、C2 ... C5、M、F、All。我尝试对因子的级别进行排序,但由于并非所有类都出现在每个图形部分中,所以它不起作用,或者至少我认为这是原因。
第三,我怎样才能减少酒吧之间的空间?使整个图更加压缩。即使我将图像缩小以进行导出,R 也会将条形缩放得更小,但条形之间的空间仍然很大。
对于任何这些答案,我将不胜感激!
我的数据: http://pastebin.com/embed_iframe.php?i=kNVnmcR1
我的代码:
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,)
需要修复的当前图表:
非常感谢!
【问题讨论】:
-
1.将粗略与
geom_bar,width解释为相邻条之间距离的比例。也许改用geom_segment,然后size将被解释为绝对。对于 2,重新排序因子水平是这样做的方法。对于 3,增加width,您已将其设置为 0.1,因此您告诉 ggplot 仅使用 10% 的空间(或使用geom_segment)。 -
谢谢!我会调查的。关于因子水平的问题。我可以将所有类(C1....C2&1 等)放在一个水平因子中,即使并非所有类都存在于相同的方面?
标签: r ggplot2 bar-chart geom-bar