按geom_segment的因子分组数据 [英] Grouped data by factor with geom_segment

查看:618
本文介绍了按geom_segment的因子分组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用 geom_segment 的因子组来显示细分群集,但是位置参数似乎没有做任何事情。这里有一个例子:

pre $ $ $ $ $ $ $ $ $ $ $ $ ,B,C)),
color = factor(c(1,2,3,4,5)),
start = c(1,2,1,4, 6),
end = c(3,4,6,5,8))

ggplot(mydata,aes(x = start,xend = end,y = variable,yend =变量))+
geom_segment(aes(color = color,position =stack),size = 3)



我也试过 position =dodge。 A组应该有3个部分,但它们都被绿色条遮住了。调整透明度将使视觉过于混淆。我如何确保每个因素的所有细分都并排显示?

我有点儿不确定的是@alistaire和我明确告诉你,所以我们的意思是:
$ b $ pre $ mydata = data.frame(variable =因子(c(A,A,A,B,C)),
color = factor(c(1,2,3,4,5)),
start = c(1,2,1,4,6),
end = c(3,4,6,5,8))

ggplot(mydata,aes (ymin = start,ymax = end,x = variable))+
geom_linerange(aes(color = color),position = position_dodge(width = 0.2),size = 3)+
coord_flip()

结果如下:


I would like to show clusters of segments by factor groups using geom_segment but the position parameter doesn't seem to be doing anything. Here's an example:

mydata = data.frame(variable = factor(c("A","A","A","B","C")),
                    color = factor(c(1,2,3,4,5)),
                    start = c(1,2,1,4,6),
                    end = c(3,4,6,5,8))

ggplot(mydata, aes(x = start, xend = end, y = variable, yend = variable)) + 
  geom_segment(aes(color = color, position = "stack"), size = 3)

I've also tried position = "dodge". Group A should have 3 segments, but they're all covered up by the green bar. Adjusting the transparency will be too confusing visually. How can I make sure that all the segments for each factor show up side-by-side?

解决方案

I'm a little uncertain that @alistaire and I are communicating this clearly to you, so here's what we mean:

mydata = data.frame(variable = factor(c("A","A","A","B","C")),
                                        color = factor(c(1,2,3,4,5)),
                                        start = c(1,2,1,4,6),
                                        end = c(3,4,6,5,8))

ggplot(mydata, aes(ymin = start, ymax = end, x = variable)) + 
    geom_linerange(aes(color = color),position = position_dodge(width = 0.2), size = 3) + 
    coord_flip()

Which results in:

这篇关于按geom_segment的因子分组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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