如何添加4个组以制作具有均值段的分类散点图? [英] How to add 4 groups to make Categorical scatter plot with mean segments?

查看:65
本文介绍了如何添加4个组以制作具有均值段的分类散点图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为R Studio中的数据制作这种图形(图形图片的参考:发布,使用R中的ggplot2使用均值段进行分类的散点图),但是我没有得到如何添加组的信息,到x轴并在Y轴上缩放一个以上.

I want to make this kind of graph(reference of graph picture: post, Categorical scatter plot with mean segments using ggplot2 in R) for my data set in R studio ,however i am not getting how can i add my groups, which is more than one, to x axis and scale on Y axis.

这是我的数据,它以cvs文件的形式保存在Windows PC中:

Here is my data which is saved in Windows PC as cvs file:

GROUP A 
22.51506233
21.86862564
21.20981979
21.44734764
21.45001411
19.99370003
GROUP B
18.95846367
20.99542427
20.96941566
21.49574852
21.18944359
21.88916016
19.47029114
19.50328064
GROUP C
20.76145554
19.29909134
21.62098885
26.1908226
21.95579529
20.79806519
24.57015228
22.81287003
21.68307304
GROUP D
20.89354706
20.52819443
22.62171173
21.20273018
20.35452652
20.89900398
21.66306114
19.66979218
19.77578926
19.31722832
21.89787102
20.92485237
20.60872269
19.97720909
21.31039047
21.76075363
22.42200661
22.59609222
21.5938015
22.24318123
22.26913261
21.67864227
18.97455406
21.47759438

以下是必填详细信息:

我没有尝试过使用图形代码,只是在观看视频以学习R,但是不幸的是,我没有获得正确的代码来制作这样的图形.该图的链接是 在R中使用ggplot2的均值段分类散点图

I didn’t tried code for graph , I am just watching videos to learn R but unfortunately I didn’t get the proper code to make such a graph. The link of the graph is Categorical scatter plot with mean segments using ggplot2 in R

我的数据在excel中,我将其保存为CVS格式,然后导入了Rstudio中.它以BCL6.DATAcvs的形式存储在我的R窗口中.我按如下所示读取文件,它是每组一列,有4组,每组具有不同数量的值,例如A具有6个值,B具有8个值,C具有9个值,D具有24个值.

My data was in excel , I saved it in CVS format then I have imported in Rstudio . It stores in my R window as BCL6.DATAcvs . I read the file as below and it is one column per group, there are 4 groups and each group has different number of values such as A has 6 values, B has 8 values, C has 9 values and D has 24 values.

summary(BCL6.DATAcvs)
       A               B               C               D        
 Min.   :19.99   Min.   :18.96   Min.   :19.30   Min.   :18.97  
 1st Qu.:21.27   1st Qu.:19.50   1st Qu.:20.80   1st Qu.:20.48  
 Median :21.45   Median :20.98   Median :21.68   Median :21.26  
 Mean   :21.41   Mean   :20.56   Mean   :22.19   Mean   :21.11  
 3rd Qu.:21.76   3rd Qu.:21.27   3rd Qu.:22.81   3rd Qu.:21.80  
 Max.   :22.52   Max.   :21.89   Max.   :26.19   Max.   :22.62  
 NA's   :18      NA's   :16      NA's   :15

请指导我如何制作这张图.

Please guide me how i can make this graph.

推荐答案

假设您有一个group列和一个value列,让我们首先重建数据:

Supposing you have a group column and a value column, lets first reconstruct your data:

A <- data.frame(group="A", value=c(22.51506233,21.86862564,21.20981979,21.44734764,21.45001411,19.99370003))
B <- data.frame(group="B", value=c(18.95846367,20.99542427,20.96941566,21.49574852,21.18944359,21.88916016,19.47029114,19.50328064))
C <- data.frame(group="C", value=c(20.76145554,19.29909134,21.62098885,26.1908226,21.95579529,20.79806519,24.57015228,22.81287003,21.68307304))
D <- data.frame(group="D", value=c(20.89354706,20.52819443,22.62171173,21.20273018,20.35452652,20.89900398,21.66306114,19.66979218,19.77578926,19.31722832,21.89787102,20.92485237,20.60872269,19.97720909,21.31039047,21.76075363,22.42200661,22.59609222,21.5938015,22.24318123,22.26913261,21.67864227,18.97455406,21.47759438))
df <- rbind(A,B,C,D)

现在,您可以使用以下方法创建散点图:

Now you can make a grouped scatterplot with:

library(ggplot2)
ggplot(df, aes(x=group, y=value, color=group)) +
  geom_point(size=4, alpha=0.7, position=position_jitter(w=0.1, h=0)) +
  stat_summary(fun.y=mean, geom="point", shape=23, color="black", aes(fill=group), size=4) +         
  stat_summary(fun.ymin=function(x)(mean(x)-sd(x)), 
               fun.ymax=function(x)(mean(x)+sd(x)),
               geom="errorbar", width=0.1) +
  theme_bw()

结果:

使用的参数的说明:

我将alpha=0.7position=position_jitter(w=0.1, h=0)结合使用以区分各点. alpha设置透明度,其值在0(完全透明)和1(非透明)之间.

I used alpha=0.7 in combination with position=position_jitter(w=0.1, h=0) in order to distinguish between the points. The alpha sets the transparency and has a value between 0 (completely transparant) and 1 (non-transparant).

使用position_jitter,您可以稍微更改点的位置.这是在精确点的某些边界内随机完成的.这样做的原因是某些点重叠.通过使用position=position_jitter(),可以使重叠点更清晰可见.使用wh参数设置边界.通过在position_jitter中设置h=0,可以确保位置变化仅在水平方向发生,垂直位置与实际值完全相同.为了查看效果,请运行不带position=position_jitter(w=0.1, h=0)部分的代码,并将其与上面的图进行比较.

With position_jitter you can change the location of the points a bit. This is done randomly within certain boundaries of the exact point. The reason for doing this that some points overlap. By using position=position_jitter() you can make the overlapping points better visible. The boundaries are set with the w and h parameters. By setting h=0 in position_jitter you assure that the change in location is only happening horizontally, the vertical location is exactly the same the actual value. In order to see the effect, run the code without the position=position_jitter(w=0.1, h=0) part and compare it with the plot above.

theme_bw()将图的布局设置为黑白布局,而不使用灰色背景.

The theme_bw() sets the layout of the plot to a black/white layout instead of using a grey background.

有关多个部分的更多信息: geom_point stat_summary

More info about the several parts: geom_point, stat_summary, geom_errorbar and theme(). For more info about the shapes of the points, just type ?pch in the console.

这篇关于如何添加4个组以制作具有均值段的分类散点图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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