R ggplot-多个箱线图

R ggplot-多个箱线图,r,ggplot2,boxplot,R,Ggplot2,Boxplot,我正在尝试使用此数据集按因子组合(3个箱线图组合)创建箱线图: 这些都是我失败的尝试 library(ggplot2) qplot( Rs, ia, data=daf) + facet_grid(mix ~ ., scales = "free", space = "free", labeller = label_both) »当我添加qplot(Rs,ia,data=daf,geom=“boxplot”) 它只是显示一条线,而不是框 ggplot(data=daf, aes(x=i

我正在尝试使用此数据集按因子组合(3个箱线图组合)创建箱线图:

这些都是我失败的尝试

library(ggplot2) 

qplot( Rs, ia, data=daf) + 
  facet_grid(mix ~ ., scales = "free", space = "free", labeller = label_both)

»当我添加
qplot(Rs,ia,data=daf,geom=“boxplot”)
它只是显示一条线,而不是框

ggplot(data=daf, aes(x=ia, y=Rs))+
  geom_boxplot(outlier.colour = "black", outlier.size = 2)  +  
  coord_flip() +  theme_bw() +
  scale_y_continuous(breaks=seq(0,1,by=0.25))+
  stat_summary(fun.y = mean, geom="point", shape = 4, size = 3, colour = "blue") +
  facet_grid(mix ~. , scales = "free", space="free", labeller = label_both) 

»它将每个“ia”级别重复到每个“混合”级别

最后,我想要三个情节的组合:

从第一个图中,facet.grid(不重复“ia”变量),从第二个图中,框,从第三个图中,左边内边距的中值,如果可能的话,到因子“mix”的每个级别,按中值对“ia”重新排序

有人能帮我吗


提前谢谢

geom_箱线图
假设分类变量位于x轴上
coord_flip
不能与
facet_grid
+
geom_boxplot
结合使用。一种解决方法是旋转文本。您可以在另一个程序中导出和旋转图像(或者了解如何拉出对象并旋转它)

我找到了答案,并认为我应该给出另一个答案

library(devtools)
devtools::install_github("lionel-/ggstance")

library(ggplot2)
library(ggstance)
daf <- read.table("http://pastebin.com/raw.php?i=xxYjmdgD", header=T, sep="\t")

library(dplyr)
a = daf %>% 
  group_by(ia, mix) %>%
  summarize(Rs=mean(Rs))

ggplot(daf, aes(x=Rs, y=ia)) +
  geom_boxploth() +
  geom_point(data=a, shape = 4, size = 3, colour = "blue") +
  geom_text(data = a, mapping = aes(y = ia, x=0, label=round(Rs,2)),
            color="NavyBlue", size=3.5, hjust=0) +
  facet_grid(mix~., scales="free_y")
库(devtools)
devtools::install_github(“lionel-/ggstance”)
图书馆(GG2)
图书馆(GGSTANTE)
daf%
分组依据(ia,混合)%>%
汇总(Rs=平均值(Rs))
ggplot(daf,aes(x=Rs,y=ia))+
geom_boxploth()+
几何点(数据=a,形状=4,尺寸=3,颜色=“蓝色”)+
几何图形文本(数据=a,映射=aes(y=ia,x=0,标签=round(Rs,2)),
color=“NavyBlue”,size=3.5,hjust=0)+
镶嵌面网格(混合,比例=“自由度”)

我认为您需要在x轴标签上添加
vjust=0.5
,以便它们位于刻度线下方的中心位置。此外,您还可以将x轴标题旋转90度,以便在整个绘图旋转后,它将处于所需的方向。@kdauria:我使用ggplot2 2.1.0尝试了您的代码,但在图层中出现
错误(data=a,mapping=aes(x=ia,y=0.02,label=round(Rs),未使用的参数(color=“NavyBlue”,size=3.5,angle=90,hjust=1,scale=“free_x”)
。如果我只使用
图层(数据=a,映射=aes(x=ia,y=0.02,标签=round(Rs,2)),geom=“text”
,发生了另一个错误
错误:尝试创建没有位置的图层。
知道出了什么问题吗?非常感谢!我不知道为什么以前使用了
图层
。我在ggplot 2.0.0上遇到了相同的错误。我更新了答案,使其在ggplot 2.0.0上工作。如果对您不起作用,请告诉我。
ggplot(data=daf, aes(x=ia, y=Rs))+
  geom_boxplot(outlier.colour = "black", outlier.size = 2)  +  
  coord_flip() +  theme_bw() +
  scale_y_continuous(breaks=seq(0,1,by=0.25))+
  stat_summary(fun.y = mean, geom="point", shape = 4, size = 3, colour = "blue") +
  facet_grid(mix ~. , scales = "free", space="free", labeller = label_both) 
ggplot(data=daf, aes(x=ia, y=Rs))+
  geom_boxplot(outlier.colour = "black", outlier.size = 2)  +  
  layer(data = a, mapping = aes(x = ia, y= 0, label=a$Rs.median),
        geom = "text", color="NavyBlue", size=3.5) +
  coord_flip() +  theme_bw() +
  scale_y_continuous(breaks=seq(0,1,by=0.25))+
  stat_summary(fun.y = mean, geom="point", shape = 4, size = 3, colour = "blue") 
a = ddply(daf, .(ia,mix), function(x) c(Rs=median(x$Rs, na.rm=TRUE)))

ggplot( data=daf, aes(x=ia, y=Rs) ) + 
  geom_boxplot() +
  facet_wrap(~mix, scales="free_x") +
  stat_summary(fun.y = mean, geom="point", shape = 4, size = 3, colour = "blue") +
  theme(axis.text.x=element_text(angle = 90, hjust = 1, vjust=0.5)) +
  theme(axis.title.x=element_text(angle = 90, vjust=0.5)) +
  theme(axis.text.y=element_text(angle = 90, hjust=0.5)) +
  theme(strip.text=element_text(angle = 90, hjust=0.5)) +
  geom_text(data = a, mapping = aes(x = ia, y= 0.02, label=round(Rs,2)),
        color="NavyBlue", size=3.5, angle=90, hjust=1) +
  ylim(-0.03,1)
library(devtools)
devtools::install_github("lionel-/ggstance")

library(ggplot2)
library(ggstance)
daf <- read.table("http://pastebin.com/raw.php?i=xxYjmdgD", header=T, sep="\t")

library(dplyr)
a = daf %>% 
  group_by(ia, mix) %>%
  summarize(Rs=mean(Rs))

ggplot(daf, aes(x=Rs, y=ia)) +
  geom_boxploth() +
  geom_point(data=a, shape = 4, size = 3, colour = "blue") +
  geom_text(data = a, mapping = aes(y = ia, x=0, label=round(Rs,2)),
            color="NavyBlue", size=3.5, hjust=0) +
  facet_grid(mix~., scales="free_y")