叠加框绘制并使用ggplot添加一条线 [英] Overlay box plots and add a line using ggplot

查看:147
本文介绍了叠加框绘制并使用ggplot添加一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据框

  data = as.data.frame(data.table :: rbindlist(PLOTS ))

头(数据)
YX dep_C1

3.96655960 0 184
-8.71308460 0 184
-11.11638947 0 184
-6.84213562 11 184
-1.25926609 11 184
-4.60649529 11 184
0.27577858 11 184
11.85394249 20 184
-0.27114563 20 184
1.73081284 20 184
1.78209915 20 184
11.34305840 20 184
13.49688263 20 184
-7.54752045 20 184
-13.63673286 25 184
-5.75711517 25 184
0.35823669 25 184
-2.45237694 25 184
0.49313087 0 66
-9.04148674 0 66
-15.50337906 0 66
-17.51445351 0 66
-10.66807098 0 66
-2.24337845 5 66
-13.7992 9533 5 66
1.33287125 5 66
2.22143402 5 66
11.46484833 10 66
23.26805916 10 66
9.07377968 10 66
4.28664665 10 66

我试图为两个 dep_C1 值创建两个箱形图并覆盖它们。我尝试了这个

  ggplot(data,aes(x = factor(X),y = Y,color = factor(dep_C1 )))
+ geom_boxplot(outlier.size = 0,fill =white)
+ stat_summary(fun.y = median,geom =line,aes(group = 1,color = factor) (dep_C1)),size = 2)

这些是我的问题

1)两个箱形图是并排的,并且没有重叠。

2)在中线之间画线的命令不起作用:线应该连接相同组的盒形图的中位数(只有一个盒形图是好的)3)x轴上的值被搞乱了(系列再次开始96后)

有人可以帮我解决这些问题吗?提前致谢

您的前两个问题很容易解决。要叠加箱形图,您需要指定 position =identity,可能类似于 alpha = 0.5 。对于该行,该组应该是 group = factor(dep_C1)。一起看起来如下:

pre $ g $ p $ ggplot(data,aes(x = factor(X),y = Y,color =因子(dep_C1)))+
geom_boxplot(outlier.size = 0,fill =white,position =identity,alpha = .5)+
stat_summary(fun.y = median,geom =line,aes(group = factor(dep_C1)),size = 2)

第三个问题,您使用 factor(X)来定义轴的比例。您可以使用 scale_x_discrete 更改此比例。也许您正在寻找如下内容:

  scale_x_discrete(limits = seq(min(data $ X),max(data $ X)))


I have a data frame like this

data = as.data.frame(data.table::rbindlist(PLOTS))

head(data)
    Y              X     dep_C1

   3.96655960      0     184
  -8.71308460      0     184
 -11.11638947      0     184
  -6.84213562      11    184
  -1.25926609      11    184
  -4.60649529      11    184
   0.27577858      11    184
  11.85394249      20    184
  -0.27114563      20    184
   1.73081284      20    184
   1.78209915      20    184
  11.34305840      20    184
  13.49688263      20    184
  -7.54752045      20    184
 -13.63673286      25    184
  -5.75711517      25    184
   0.35823669      25    184
  -2.45237694      25    184
   0.49313087      0     66
  -9.04148674      0     66
 -15.50337906      0     66
 -17.51445351      0     66
 -10.66807098      0     66
  -2.24337845      5     66
 -13.79929533      5     66
   1.33287125      5     66
   2.22143402      5     66
  11.46484833      10    66
  23.26805916      10    66
   9.07377968      10    66
   4.28664665      10    66

I am trying to create two box plots for the two dep_C1 values and overlay them. I tried with this

ggplot(data, aes(x=factor(X), y=Y, colour = factor(dep_C1))) 
+ geom_boxplot(outlier.size=0,fill = "white") 
+ stat_summary(fun.y=median, geom="line", aes(group=1,colour = factor(dep_C1)),size=2)

These are my problems

1) The two box plots are side by side and not overlaid

2) The command that draw a line between the medians is not working OK: lines should connect medians of boxplots of the same group (with only one boxplot is fine)

3) The values on the x axis are messed up (the series start again after 96)

Can someone help me fix these problems? Thanks in advance

解决方案

Your first two problems are easy to fix. To overlay the boxplots, you want to specify position="identity" and probably something like alpha=0.5. For the line, the group should be group=factor(dep_C1). Together this will look as follows:

ggplot(data, aes(x=factor(X), y=Y, colour = factor(dep_C1)))  +
 geom_boxplot(outlier.size=0, fill = "white", position="identity", alpha=.5)  +
 stat_summary(fun.y=median, geom="line", aes(group=factor(dep_C1)), size=2) 

For the 3rd question, you are using factor(X), which defines the scale of the axis. You can change this scale using scale_x_discrete. Perhaps you are looking for something like the following:

scale_x_discrete(limits=seq(min(data$X), max(data$X)))

这篇关于叠加框绘制并使用ggplot添加一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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