添加第二个geom_points后重新排序不起作用 [英] Reorder does not work after adding second geom_points

查看:135
本文介绍了添加第二个geom_points后重新排序不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据如下所示:

 >头(GEE)

性状测试分析标志
1特性1 0.078 0.01 9.0e-13组1 1
2特性2 0.076 0.01 1.7e-11组1 1
3 trait3 0.063 0.01 1.8e-08 group1 1
4 trait4 0.013 0.01 .06 group1 0
5 trait5 0.018 0.01 .54 group1 0
6 trait6 -0.014 0.01 .64 group1 0

我试图在第二列命名为beta的gglot2上绘制点和错误条。以下代码可以正常工作:

  ggplot(gEE,aes(y = beta,x = reorder(trait,beta) ,group = analysis))+ 
geom_point(aes(color = analysis))+
geom_errorbar(aes(ymin = beta-2 * se,ymax = beta + 2 * se,color = analysis), width = .2,
position = position_dodge(.9))+
theme_light()+
coord_flip()

然而,我想为明显的点添加星号,为此我添加了一段代码,它成功地将星号添加到了重要的点上:

  geom_point(data = GEE [GEE $ signif == 1,],
color =red,
shape =*,
size = 12,
show.legend = F)+

它到上面的plot命令,完整的命令如下所示:

  ggplot(GEE,aes(y = beta,x = reorder(trait,beta),group = analysis))+ 
geom_point(data = GEE [GEE $ signif == 1,],
color =red,
shape =*,
size = 12,
show.legend = F)+
geom_point(aes(color = analysis ))+
geom_errorbar(aes(ymin = beta-2 * se,ymax = beta + 2 * se,color = analysis),width = .2,
position = position_dodge(.9))+
theme_light()+
coord_flip()

然而,添加星号却是以某种方式取消了我用x = reorder(trait,beta)指定的重新排序。订单现在完全不同。为什么? :'(



更新
@Lamia在评论中指出,该问题无法用数据重新生成这篇文章中的数据只显示了所有来自同一组的最高行(group1,见分析一栏),但是在我的完整数据集中有三个组,ggplot2命令绘制了这三个不同的组不同颜色的群体,当我读完全部数据集时,我只能重现这个问题,所以看起来这可能与这个问题有关......



更新2
由于@Lamia建议(对不起,应该先开始),我在下面添加了重现问题的数据:

  trait,beta,se,p,analysis,signif 
trait1,0.078,0.01,9.00E-13,group1,1
trait2,0.076,0.01,1.70E-11,group1,1
trait3,-0.032,0.01,0.004,group1,0
trait4,0.026,0.01,0.024,group1,0
trait5,0.023,0.01,0.037,group1,0
tra it1,0.042,0.01,4.50E-04,group2,1
trait2,0.04,0.01,0.002,group2,1
trait3,0.03,0.01,0.025,group2,0
trait4, 0.025,0.01,0.078,组2.0,0
特性5,0.015,0.01,0.294,组2.0,0
特性1,0.02,0.01,0.078,组3,0
特性2,0.03,0.01,0.078 ,group3,0
trait3,0.043,0.01,1.90E-04,group3,0
trait4,0.043,0.01,2.40E-04,group3,1
trait5,0.029,0.01, 0.013,group3,0


解决方案



1:在将数据框输入 ggplot() 之前,由于trait是一个分类变量,因此您可以明确指定其级别。

  library(dplyr)

g2 < - GEE%>%
mutate(trait = factor(trait,levels = trait [order(beta [analysis ==group1])]))

>等级(g2 $ trait)
[1]trait3trait5trait4trait2trait1

我们可以直观地验证根据组1的beta值的特征顺序是3-5-4-2-1:

  ggplot(GEE,aes(x = beta,y = analysis,label = trait))+ geom_label()



2:如果可能,只使用一个数据源用于ggplot 。确保所有的美学映射链接都是一致的,&

  ggplot(g2,aes(y = beta,x = trait,group = analysis,color =分析,
ymin = beta - 2 * se,ymax = beta + 2 * se))+
geom_point(aes(alpha = signif),#在使用相同数据集时隐藏非显着点$ b ()
geom_errorbar()+
scale_alpha_identity()


3:对所有相关的geom重复使用相同的position_dodge()。再次,这应该更简单您希望下次更改闪避宽度。

  pd < -  position_dodge(0.9)

ggplot(g2,aes(y = beta,x = trait,group = analysis,color = analysis,
ymin = beta - 2 * se,ymax = beta + 2 * se))+
geom_point aes(alpha = signif),
color =red,shape =*,size = 12,show.legend = F,
position = pd)+
geom_point(position = pd)+
geom_errorbar(width = .2,position = pd)+
scale_alpha_identity()+
theme_light()+
coord_flip()

结果(trait从底部排序为3-5-4-2-1到顶部):





数据:

  GEE < -  read.csv(
text =
)特质,beta,se,p,analysis,signif
trait1,0.078,0.01,9.00E-13,group1,1
trait2,0.076,0.01,1.70E-11,group1,1
trait3,-0.032,0.01,0.004,group1, 0
trait4,0.026,0.01,0.024,group1,0
trait5,0.023,0.01,0.037,group1,0
trait1,0.042,0.01,4.50E-04,group2,1
trait2,0.04,0.01,0.002,group2,1
trait3,0.03,0.01,0.025,group2,0
trait4,0.025,0.01,0.078,group2,0
trait5, 0.015,0.01,0.294,group2,0
trait1,0.02,0.01,0.078,group3,0
trait2,0.03,0.01,0.078,group3,0
trait3,0.043,0.01,1.90E-04,group3,0
trait4,0.043,0.01,2.40E-04,group3,1
trait5,0.029,0.01,0.013,group3,0 )

> level(GEE $ trait)#默认订单为1-2-3-4-5
[1]trait1trait2trait3trait4trait5


My data looks like this:

> head(GEE)

  trait   beta   se       p   analysis signif
1 trait1  0.078 0.01 9.0e-13  group1   1
2 trait2  0.076 0.01 1.7e-11  group1   1
3 trait3  0.063 0.01 1.8e-08  group1   1
4 trait4  0.013 0.01 .06      group1   0
5 trait5  0.018 0.01 .54      group1   0
6 trait6 -0.014 0.01 .64      group1   0

I'm trying to make a plot with gglot2 of points and errorbars that are ordered on the second column named beta. That works fine with the following code:

ggplot(GEE, aes(y=beta, x=reorder(trait, beta), group=analysis)) + 
  geom_point(aes(color=analysis)) +
  geom_errorbar(aes(ymin=beta-2*se, ymax=beta+2*se,color=analysis), width=.2,
                position=position_dodge(.9)) +
  theme_light() +
  coord_flip() 

However, I want to add asterisks for significant points, for which I have an additional piece of code, that successfully adds asterisks to significant points:

geom_point(data = GEE[GEE$signif == 1, ],
           color="red",
           shape = "*", 
           size=12, 
           show.legend = F) +

When adding it to the previous plot command above, the full command looks like this:

ggplot(GEE, aes(y=beta, x=reorder(trait, beta), group=analysis)) + 
  geom_point(data = GEE[GEE$signif == 1, ],
             color="red",
             shape = "*", 
             size=12, 
             show.legend = F) +
  geom_point(aes(color=analysis)) +
  geom_errorbar(aes(ymin=beta-2*se, ymax=beta+2*se,color=analysis), width=.2,
                position=position_dodge(.9)) +
  theme_light() +
  coord_flip() 

The problem with the code after adding the asterisks however is that it somehow cancels the re-ordering that I specified with "x=reorder(trait, beta)". The order is now completely different. Why? :'(

UPDATE: @Lamia noted in the comments that the problem could not be re-produced with the data that is presented in this post. The data in this post only shows the top rows which are all from the same group (group1, see column "analysis"), but in my full dataset there are three groups. The ggplot2 command plots these three different groups with different colors. I can only reproduce the problem when I read in the full dataset with all three groups, so it seems that that may have something to do with this problem...

UPDATE 2: As @Lamia suggested (sorry, should have done this to begin with), I have added data below that reproduces the problem:

trait,beta,se,p,analysis,signif
trait1,0.078,0.01,9.00E-13,group1,1
trait2,0.076,0.01,1.70E-11,group1,1
trait3,-0.032,0.01,0.004,group1,0
trait4,0.026,0.01,0.024,group1,0
trait5,0.023,0.01,0.037,group1,0
trait1,0.042,0.01,4.50E-04,group2,1
trait2,0.04,0.01,0.002,group2,1
trait3,0.03,0.01,0.025,group2,0
trait4,0.025,0.01,0.078,group2,0
trait5,0.015,0.01,0.294,group2,0
trait1,0.02,0.01,0.078,group3,0
trait2,0.03,0.01,0.078,group3,0
trait3,0.043,0.01,1.90E-04,group3,0
trait4,0.043,0.01,2.40E-04,group3,1
trait5,0.029,0.01,0.013,group3,0

解决方案

Several suggestions for consideration...

1: Wrangle your data frame before feeding it into ggplot(). Since trait is a categorical variable, you can specify its levels as a factor explicitly.

library(dplyr)

g2 <- GEE %>%
  mutate(trait = factor(trait, levels = trait[order(beta[analysis == "group1"])]))

> levels(g2$trait)
[1] "trait3" "trait5" "trait4" "trait2" "trait1"

We can visually verify that the order of traits according to group 1's beta values are 3-5-4-2-1:

ggplot(GEE, aes(x = beta, y = analysis, label = trait)) + geom_label()

2: Use only one data source for ggplot(), if possible. This ensures that all the aesthetic mapping linkages are consistent, & is also simpler to maintain.

ggplot(g2, aes(y = beta, x = trait, group = analysis, color = analysis,
               ymin = beta - 2*se, ymax = beta + 2*se)) + 
  geom_point(aes(alpha = signif),   # hide non-significant points while using the same dataset
             color = "red") +       # override 'color = analysis' in ggplot()
  geom_point() +
  geom_errorbar() +
  scale_alpha_identity()

3: Reuse the same position_dodge() for all relevant geoms. Again, this is simpler to maintain should you wish to change the dodge width next time.

pd <- position_dodge(0.9)

ggplot(g2, aes(y = beta, x = trait, group = analysis, color = analysis,
               ymin = beta - 2*se, ymax = beta + 2*se)) + 
  geom_point(aes(alpha = signif), 
             color = "red", shape = "*", size = 12, show.legend = F,
             position = pd) +
  geom_point(position = pd) +
  geom_errorbar(width=.2, position = pd) +
  scale_alpha_identity() +
  theme_light() +
  coord_flip() 

Result (trait is sorted as 3-5-4-2-1, from bottom to top):

Data:

GEE <- read.csv(
  text = 
"trait,beta,se,p,analysis,signif
trait1,0.078,0.01,9.00E-13,group1,1
trait2,0.076,0.01,1.70E-11,group1,1
trait3,-0.032,0.01,0.004,group1,0
trait4,0.026,0.01,0.024,group1,0
trait5,0.023,0.01,0.037,group1,0
trait1,0.042,0.01,4.50E-04,group2,1
trait2,0.04,0.01,0.002,group2,1
trait3,0.03,0.01,0.025,group2,0
trait4,0.025,0.01,0.078,group2,0
trait5,0.015,0.01,0.294,group2,0
trait1,0.02,0.01,0.078,group3,0
trait2,0.03,0.01,0.078,group3,0
trait3,0.043,0.01,1.90E-04,group3,0
trait4,0.043,0.01,2.40E-04,group3,1
trait5,0.029,0.01,0.013,group3,0")

> levels(GEE$trait) # default order is 1-2-3-4-5
[1] "trait1" "trait2" "trait3" "trait4" "trait5"

这篇关于添加第二个geom_points后重新排序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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