添加第二个geom_points后重新排序不起作用

时间:2017-10-19 22:26:24

标签: r ggplot2

我的数据如下:

> 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

我试图制作一个带有gglot2点和错误栏的图,这些点是在名为beta的第二列上排序的。使用以下代码可以正常工作:

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命令时,full命令如下所示:

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
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

2 个答案:

答案 0 :(得分:2)

有几点需要考虑......

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

library(dplyr)

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

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

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

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

plot1

2:如果可能,仅为ggplot()使用一个数据源。这可以确保所有美学映射链接都是一致的,&amp;也很容易维护。

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:对所有相关的geoms重复使用相同的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() 

结果(特性分为3-5-4-2-1,从下到上):

plot2

数据:

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"

答案 1 :(得分:1)

试试这个:

A = [1 0 1 4.3 4.5 5 4.3 3 0 0 0 2 6.2 6.3 7 6.2 7.4 8  7.2 1 2 3 4 2];
Yposlocfiltered = [ 6    15    23];
Yneglocfiltered = [2     9    20];

Yloc = [2     6     9    15    20    23];
Ymaximaminimanew = [0     5     0     7     1     4];

sA = numel(A);
n=3;
r = 1;
s = 1;
for t = 1:numel(Yloc)

    if (ismember(A(Yloc(1,t)),A(Yposlocfiltered)))
x = A(Yposlocfiltered); 
x = x(1,r);
poslocations=x ;
idx = poslocations-1;
sI = numel(idx);         

f = Yposlocfiltered(1,r) - [1:sA];
g = Yposlocfiltered(1,r) + [1:sA];

f(f<1) = [];
g(g>sA) = [];

backward_window(t) = f(find(A(f) <= idx, 1)) + 1;
forward_window(t) = g(find(A(g) <= idx, 1)) - 1;

r = r+1;
range = backward_window(t) : forward_window(t);
if n <= numel(range)
    A(range(1:n)) = idx;
else
    A(range) = idx;

end



elseif (ismember(A(Yloc(1,t)),A(Yneglocfiltered)))
c = A( Yneglocfiltered);
c = c(1,s);
neglocations=c;
idx_neg = neglocations+1;
sU = numel(idx_neg );

h = Yneglocfiltered(1,s) - [1:sA];
p = Yneglocfiltered(1,s) + [1:sA];
h(h<1) = [];
p(p>sA) = [];
backward_window_neg(t) = h(find(A(h) <= idx_neg, 1)) + 1;
forward_window_neg(t) = p(find(A(p) <= idx_neg, 1)) - 1;

 range = backward_window_neg(t) : forward_window_neg(t);
 s = s + 1;
if n <= numel(range)
    A(range(1:n)) = idx_neg;
else
    A(range) = idx_neg;

end           


end

    end 

它改变了这个情节:

enter image description here

并使它看起来像这样:

enter image description here

这就是你要找的东西吗?