具有多个x变量的Boxplot

时间:2014-06-26 10:31:51

标签: r ggplot2 boxplot

我是R的新手,在使用ggplot2时遇到了一些问题。

这是我的数据(较大数据集的子集)的示例:

df <- 
structure(list(logpvalue = c(22.36, 6.93, 16.78, 1.78, 17.75, 
20.99, 21.03, 9.19, 15.01, 22.25, 13.4, 6.47, 1.34, 13.4, 3.21, 
0.37, 0.5, 0.12, 1.8, 0.71, 1.15, 6.73, 0.12, 6.97, 0.64, 9.85, 
1.45, 1.67, 2.6, 1.8, 1.35, 4.69, 0.37, 1.91, 0.31, 0, 2.45, 
1.68, 2.31, 1.35, 6.48, 4.68), SNP = structure(c(1L, 7L, 6L, 
5L, 11L, 1L, 9L, 5L, 8L, 11L, 7L, 5L, 8L, 11L, 1L, 7L, 1L, 4L, 
2L, 3L, 10L, 7L, 1L, 4L, 2L, 3L, 10L, 4L, 2L, 3L, 10L, 4L, 2L, 
3L, 10L, 4L, 2L, 3L, 7L, 9L, 5L, 1L), .Label = c("rs10244", "rs10891244", 
"rs10891245", "rs11213821", "rs12296076", "rs138567267", "rs45615536", 
"rs6589218", "rs7103178", "rs7127721", "rs7944895"), class = "factor"), 
X173 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("het", "hom"), class = "factor")), .Names = c("logpvalue", 
"SNP", "X173"), class = "data.frame", row.names = c(NA, -42L))

我想在y轴上绘制logpvalue的箱线图,在x轴上绘制SNP,但每个SNP也根据患者hethom进行分类{ {1}}。所以根据这些数据,我想象我的箱子图上有4个盒子。

如果可能的话,我还想将各个数据点(dotplot-boxplot overlay)与抖动结合起来。

这是我用于logpavlue与SNP的箱线图的常用代码:

X173

如何将额外的x变量添加到此代码中?

1 个答案:

答案 0 :(得分:0)

试试这个:

boxplot(df$logpvalue~paste(df$SNP,df$X173))

或使用ggolot2

library(ggplot2)

ggplot(data=df,aes(SNP,logpvalue,colour=SNP)) +
  geom_boxplot() +
  geom_jitter() +
  facet_grid(.~X173)

enter image description here