用SE绘制3分类& R中的1个连续变量 [英] Point Plot with SE for 3 Categorical & 1 Continuous Variable in R

查看:104
本文介绍了用SE绘制3分类& R中的1个连续变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$我试图在包含三个分类变量(mea,tre和sex)的设计中生成一个以单个测量值(len)为单位的单点图。 b $ b

我制作了一个情节,包含所有我想要分割的六个不同的小插图:



但我最好喜欢在可能的情况下将它们全部放在一张图中,优先使用ggplot。



这是我目前的R代码:

  ggplot(mydf,aes(x = factor(mea),y = len),group = sex)+ 
geom_point()+ geom_errorbar(limits,width = 0.1)+ facet_wrap(〜tre + sex)

以及来自dput的示例数据:

 结构(list(mea = structure c(1L,1L,1L,1L,1L,1L,
2L,2L,2L,2L,2L,2L),.Label = c(PO_P,Melaniz ),
tre =结构(c(1L,1L,2L,2L,3L,3L,1L,1L,2L,
2L,3L,3L),。标签= c (1L,2L,1L,2L,1L,2L,1L,2L,1L,2L)的结构(a,b,c),class =factor ,
1L,2L),.Label = c(男,女),class =factor),
N = c(26,26,25,25,27,27 ,14,13,12,11,14,13),len = c(10.6615384615385,
10.5807692307692,10.292,10.6,10.2851851851852,10.6518518518519,
11.4785714285714,11.7153846153846,11.7083333333333,11.5,
11.6214285714286,11.8923076923077),SD = C(0.869057845290829,
0.779753412698774,0.722218803410712,0.654471797202395,
0.906686148609193,0.8040141456708,1.0123685642542,0.805032249712347,
1.13654846981659,0.822192191643779,0.833139171519908,0.739889111580849
)中, SE = C(0.170436265829955,0.152922225659293,0.144443760682142,
0.130894359440479,0.174491830656674,0.154732594478434,
0.270566879755675,0.223275773441538,0.328093282497832,
0.247900273203854,0.222665809666299,0.205208317689404)
ci = c(0.351020060264102,0.314949219318153,0.298117269908016,0
0.270152680174426,0.35676794.9717481,0.318057403068012,
0.584524206501098,0.486476119728297,0.722128445903482,
0.552356230143519,0.481040236068982,0.4447110515336101)),.Names = c(mea ,tre,sex,N,len,sd,se,ci),row.names = c(NA,
-12L),class =data .frame)


解决方案

要区分所有这些分类变量您可以使用颜色,形状,大小,点型等。以下是使用性别 tre

  library(ggplot2)
limits <-aes(ymax = mydf $ len + mydf $ se ,ymin = mydf $ len-mydf $ se)

ggplot(mydf,aes(x = factor(mea),y = len,color = sex,pch = tre))+
geom_point(position = position_dodge(width = 0.5))+
geom_errorbar(limits,position = position_dodge(width = 0.5))


I'm attempting to generate a single plot of points that features the values for a single measurement (len) in a design with three categorical variables (mea, tre, and sex).

I've produced a plot that has all I'm looking for split across six different subplots:

But I'd ideally like to have them all in a single plot if possible, preferentially using ggplot.

Here is my current R code:

ggplot(mydf, aes(x=factor(mea), y=len), group=sex) +
  geom_point() + geom_errorbar(limits, width=0.1) + facet_wrap(~ tre + sex)

And sample data from dput:

structure(list(mea = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("PO_P", "Melaniz"), class = "factor"), 
    tre = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 
    2L, 3L, 3L), .Label = c("a", "b", "c"), class = "factor"), 
    Sex = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L), .Label = c("Male", "Female"), class = "factor"), 
    N = c(26, 26, 25, 25, 27, 27, 14, 13, 12, 11, 14, 13), len = c(10.6615384615385, 
    10.5807692307692, 10.292, 10.6, 10.2851851851852, 10.6518518518519, 
    11.4785714285714, 11.7153846153846, 11.7083333333333, 11.5, 
    11.6214285714286, 11.8923076923077), sd = c(0.869057845290829, 
    0.779753412698774, 0.722218803410712, 0.654471797202395, 
    0.906686148609193, 0.8040141456708, 1.0123685642542, 0.805032249712347, 
    1.13654846981659, 0.822192191643779, 0.833139171519908, 0.739889111580849
    ), se = c(0.170436265829955, 0.152922225659293, 0.144443760682142, 
    0.130894359440479, 0.174491830656674, 0.154732594478434, 
    0.270566879755675, 0.223275773441538, 0.328093282497832, 
    0.247900273203854, 0.222665809666299, 0.205208317689404), 
    ci = c(0.351020060264102, 0.314949219318153, 0.298117269908016, 
    0.270152680174426, 0.358673094717481, 0.318057403068012, 
    0.584524206501098, 0.486476119728297, 0.722128445903482, 
    0.552356230143519, 0.481040236068982, 0.447110515336101)), .Names = c("mea", "tre", "sex", "N", "len", "sd", "se", "ci"), row.names = c(NA, 
-12L), class = "data.frame")

解决方案

To differentiate between all those categorical variables you can use color, shape, size, pointtype, etc. Here is an example using color and point type for sex and tre,

library(ggplot2)
limits <- aes(ymax=mydf$len+mydf$se, ymin=mydf$len-mydf$se)

ggplot(mydf, aes(x=factor(mea), y=len, color=sex, pch=tre)) +
  geom_point(position=position_dodge(width=0.5)) +
  geom_errorbar(limits, position=position_dodge(width=0.5))

这篇关于用SE绘制3分类&amp; R中的1个连续变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆