将gomplot2中的geom_point和geom_line与position_jitterdodge结合使用以获得两个分组因子 [英] Combining geom_point and geom_line with position_jitterdodge in ggplot2 for two grouping factors

查看:288
本文介绍了将gomplot2中的geom_point和geom_line与position_jitterdodge结合使用以获得两个分组因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几条建议,这些建议是基于这里的几篇文章,以及阅读ggplot2文档,但是这个问题稍有不同,我还没有找到解决方案。



以下是一段代码,用于制作与我正在使用的数据框相似的融合数据框:

  a <-c(1,2,3,4,5,6,1,2,3,4,5,6)
b <-c(loss,draw,win ,抽奖,损失,胜利,损失,抽奖,胜利,抽奖,损失,胜利)
c < - c ,5,4,4,5,4,4,3,5,2,4)
d < - c(rep(x,6),rep(y,6))
temp< - data.frame(a,b,c,d)

我想要什么是在x轴上创建一个b点,y轴上创建一个点图,并将x轴上的点分组为d,并将它们之间的线条分组为a。如果我们先把图上的点放在这样的图上,结果会很好:

  ggplot(temp,aes(x = b,y = c,fill = d,color = d))+ 
geom_point(position = position_jitterdodge())

这是我得到的点图,这就是我想要的看点。这些点根据d因子进行颜色编码,而且它们被抖动到一边,因此x在左边,y在右边。



现在,我只需要按照因子a将点与线连接起来。 这是我在MS Paint中制作的一个模拟版本,它看起来应该如何。添加geom_line并将geom_line组设置为a应该有效...

$ g $ p $ ggplot(temp,aes(x = b,y = c,fill = d,color = d))+
geom_point(position = position_jitterdodge())+
geom_line(aes(group = a),position = position_jitterdodge())

...但它不能正确显示。它创建的线条是连接正确点的正确长度,但它们几乎不会触及它们,看起来像是随机在图表上。此外,如果我将群体美学更改为整体审美,如下所示:

>

  ggplot(temp,aes(x = b,y = c,fill = d,color = d,aes(group = a)) )+ 
geom_point(position = position_jitterdodge())+
geom_line(position = position_jitterdodge())

...然后点切换,以便它们位于错误的位置。在任何情况下,这些线条仍然没有完全满足要点。



我也试过在position_jitterdodge()部分中指定width和jitter.width值,线路和点仍然没有合适。我还通读了ggplot2文档,以前的几个stackoverflow问题,并尝试了position_jitter,position_dodge,geom_jitter等的大多数组合,但迄今为止没有运气。



任何帮助都将不胜感激。

解决方案

- 指定手动抖动值:

  library(ggplot2)

a < - c(1,2 ,3,4,5,6,1,2,3,4,5,6)
#b < - c(损失,绘制,胜利,绘制,损失,胜利,损失,抽奖,胜利,抽奖,损失,胜利)
b <-c(2,1,3,1,2,3 (2,3,5,4,4,5,4,4,3,5,2,4)
d(2,1,3,1,2,3)
c <-c(2,3,5,4,4,5,4,4,3,5,2,4)
d < -c(rep(x,6),rep(y,6))
temp< - data.frame(a,b,c,d)

set.seed(2016)
jitterVal < - runif(12,max = 0.25)
jitterVal <-jitterVal * ifelse(temp $ d ==x,-1,+ 1)

ggplot(temp,aes(x = b + jitterVal,y = c,fill = d,color = d))+
geom_point()+
geom_line(aes group = a))+
scale_x_continuous(breaks = c(1,2,3),labels = c(draw,loss,win))+
xlab(NULL)+
expand_limits(x = c(0.5,3.5))


I've tried several suggestions based on several posts on here, as well as reading through the ggplot2 documentation, but this question is slightly different, and I haven't found a solution yet.

Here's a snippet of code to make a melted dataframe similar to the one I'm working with:

a <- c(1,2,3,4,5,6,1,2,3,4,5,6)
b <- c("loss", "draw", "win", "draw", "loss", "win", "loss", "draw", "win", "draw", "loss", "win")
c <- c(2,3,5,4,4,5,4,4,3,5,2,4)
d <- c(rep("x", 6), rep("y", 6))
temp <- data.frame(a,b,c,d)

What I want is to create a dot plot with b on the x-axis, c on the y-axis, with the points on the x-axis grouped by d, and lines between them grouped by a. If we start by just putting the points on the graph like so, it comes out fine:

ggplot(temp, aes(x=b, y=c, fill=d, colour=d))+
  geom_point(position=position_jitterdodge())

This is the dot plot I get, which is how I want it to look. The points are colour-coded according to factor d, and moreover they're jitterdodged to one side so that x is on the left and y is on the right.

Now, all I want is to connect the points with lines according to factor a. This is a mocked-up version I made in MS Paint of how it should look. Adding a geom_line and setting the geom_line group to a should work...

ggplot(temp, aes(x=b, y=c, fill=d, colour=d))+
  geom_point(position=position_jitterdodge())+
  geom_line(aes(group=a),position=position_jitterdodge())

...but it doesn't come out right. The lines it creates are the right length to connect the correct points, but they barely even touch them, it looks like they're on the graph at random. I would add another screenshot, but don't have the privileges yet.

Moreover, if I change the group aesthetic to be in the overall aesthetic like so:

ggplot(temp, aes(x=b, y=c, fill=d, colour=d, aes(group=a)))+
  geom_point(position=position_jitterdodge())+
  geom_line(position=position_jitterdodge())

...then the points are switched around so that they're in the wrong position. And in any case, the lines still don't join up with the points fully.

I have also tried specifying width and jitter.width values in the position_jitterdodge() part, and the lines and points still don't join up properly. I have also read through the ggplot2 documentation, several previous stackoverflow questions, and tried out most combinations of position_jitter, position_dodge, geom_jitter, etc., but no luck so far.

Any help would be greatly appreciated.

解决方案

One possible solution - specifying jitter values manualy:

library(ggplot2)

a <- c(1,2,3,4,5,6,1,2,3,4,5,6)
# b <- c("loss", "draw", "win", "draw", "loss", "win", "loss", "draw", "win", "draw", "loss", "win")
b <- c(2, 1, 3, 1, 2, 3, 2, 1, 3, 1, 2, 3)
c <- c(2, 3, 5, 4, 4, 5, 4, 4, 3, 5, 2, 4)
d <- c(rep("x", 6), rep("y", 6))
temp <- data.frame(a,b,c,d)

set.seed(2016)
jitterVal <- runif(12, max = 0.25)
jitterVal <- jitterVal * ifelse(temp$d == "x", -1, +1)

ggplot(temp, aes(x = b + jitterVal, y = c, fill = d, colour = d)) +
  geom_point() +
  geom_line(aes(group = a)) +
  scale_x_continuous(breaks = c(1, 2, 3), labels = c("draw", "loss", "win")) +
  xlab(NULL) +
  expand_limits(x = c(0.5, 3.5))

这篇关于将gomplot2中的geom_point和geom_line与position_jitterdodge结合使用以获得两个分组因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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