首页 文章

具有多个x变量的Boxplot

提问于
浏览
0

我是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也根据患者是 het 还是 hom 来分类 X173 . 所以根据这些数据,我想象我的箱子图上有4个盒子 .

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

这是我用于logpavlue与SNP的boxplot的常用代码:

qplot(logpvalue, SNP, data = mydata, geom="boxplot")
+ geom_jitter(position=position_jitter(w=0.1, h=0.1)) + theme_bw()

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

1 回答

  • 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

相关问题