13

我可以找到以下问题的解决方案。我会很感激一些帮助!

以下代码使用 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,)

需要修复的当前图表:

在此处输入图像描述

非常感谢!

4

2 回答 2

7

这是我在 Gregor 的建议后所做的。正如我所想,使用 geom_segment 和 geom_point 可以制作一个很好的图表。

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,)

这就是它产生的!

在此处输入图像描述

于 2015-05-12T21:30:52.080 回答
6

假设条形宽度与 x-breaks 的数量成反比,可以输入适当的比例因子作为width控制条形宽度的美学。但首先,计算每个面板中的 x-breaks 数量,计算比例因子,然后将它们放回“所有”数据框中。

更新到 ggplot2 2.0.0中提到的每一列在facet_wrap条带中都有自己的行。在编辑中,在数据框中设置了一个新的标签变量,以便条形标签保持在一行上。

library(ggplot2)
library(plyr)

all = structure(list(station = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Station 101", 
"Station 126"), class = "factor"), shortname2 = structure(c(2L, 
7L, 8L, 11L, 1L, 5L, 7L, 8L, 11L, 1L, 2L, 3L, 5L, 7L, 8L, 12L, 
11L, 1L, 6L, 8L, 15L, 14L, 9L, 10L, 4L, 6L, 2L, 7L, 8L, 11L, 
1L, 5L, 7L, 8L, 11L, 1L, 2L, 3L, 5L, 7L, 8L, 12L, 11L, 1L, 8L, 
11L, 1L, 15L, 14L, 13L, 9L, 10L), .Label = c("All", "C1", "C2", 
"C2&1", "C3", "C3&2", "C4", "C5", "Cegg", "Cnaup", "F", "M", 
"Micro", "Oith", "Tric"), class = "factor"), color = c(1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 21L, 26L, 30L, 31L, 33L, 34L, 20L, 21L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 26L, 28L, 29L, 30L, 31L, 32L, 33L, 34L), group = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 6L, 5L, 3L, 3L, 3L, 3L, 6L, 6L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 3L, 3L, 
3L, 3L, 3L), .Label = c("cgla", "Chyp", "Cope", "mlong", "pseudo", 
"specC"), class = "factor"), sample_size = c(11L, 37L, 55L, 16L, 
119L, 21L, 55L, 42L, 40L, 158L, 24L, 16L, 17L, 27L, 14L, 45L, 
98L, 241L, 30L, 34L, 51L, 22L, 14L, 47L, 13L, 41L, 24L, 41L, 
74L, 20L, 159L, 18L, 100L, 32L, 29L, 184L, 31L, 17L, 27L, 23L, 
21L, 17L, 49L, 185L, 30L, 16L, 46L, 57L, 16L, 12L, 30L, 42L), 
    perc_correct = c(91L, 78L, 89L, 81L, 85L, 90L, 91L, 93L, 
    80L, 89L, 75L, 75L, 76L, 81L, 86L, 76L, 79L, 78L, 90L, 97L, 
    75L, 86L, 93L, 74L, 85L, 88L, 88L, 90L, 92L, 90L, 91L, 89L, 
    89L, 91L, 90L, 89L, 81L, 88L, 74L, 78L, 90L, 82L, 84L, 82L, 
    90L, 94L, 91L, 81L, 69L, 83L, 90L, 81L)), class = "data.frame", row.names = c(NA, 
-52L))

all$station <- as.factor(all$station)

# 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, width = .5*Fac)) + 
  geom_bar(stat="identity", position="dodge",  colour="NA") +
  scale_fill_gradient("Sample size (n)",low="lightblue",high="navyblue")+
  facet_wrap(~label,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

在此处输入图像描述

于 2015-05-12T21:57:08.453 回答