R - ggplot避开geom_lines [英] R - ggplot dodging geom_lines

查看:167
本文介绍了R - ggplot避开geom_lines的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一直是我一直在尝试寻找修复的一段时间,但基本上我想知道是否有一个快速的方法来闪避ggplot2中两个不同数据集的线图。



我的代码目前是:

 #示例数据
id< - C(A,A)
var <-c(1,10)
id_num <-c(1,1)
df1 < - data.frame( id,var,id_num)

id <-c(A,A)
var <-c(1,15)
id_num < - c(0.9,0.9)
df2< - data.frame(id,var,id_num)


#尝试图
闪避< - position_dodge(宽= 0.5)
p < - ggplot(data = df1,aes(x = var,y = id))+
geom_line(aes(color =Group 1),position =dodge) +
geom_line(data = df2,aes(x = var,y = id,color =Group 2),position =dodge)+
scale_color_manual(,values = c(鲑鱼,skyblue2))
p

产生:



这里的第2组行隐藏了所有不属于我想要的第1组行。相反,我希望第2组行低于第1组行。我环顾四周,发现以前的帖子:



当我试图扩展它以适应数字方法时,可能会有点麻烦我的实际数据。我必须将我的y值转换为因子,将它们更改为数值,然后将这些值合并到第二个数据集中,因此更快捷的方法将更可取。感谢您的帮助!

解决方案

您实际上有两个问题:


  1. 如果两行用 geom_line()每条线不知道另一条。因此,它们不能相互闪避。

  2. position_dodge()用于在水平方向闪避。标准示例是条形图,您可以在其中放置各个条(而不是彼此顶部)。然而,你想避开垂直方向。

问题1是通过将数据帧合并为一个:

  library(dplyr)
df_all< - bind_rows(Group1 = df1,Group2 = df2,.id = group)
df_all
##来源:本地数据框[4 x 4]
##
##组ID id var x_num
##(chr) (fctr)(dbl)(dbl)
## 1 Group1 A 1 1.0
## 2 Group1 A 10 1.0
## 3 Group2 A 1 0.9
## 4 Group2 A 15 0.9

请注意如何设置 .id =Group bind_rows()创建一个列 group df1 df2



geom_line()

  library(ggplot2)
ggplot(data = df_all,aes(x = var,y = id,color = group))+
geom_line(position = position_dodge(width = 0.5))+
scale_color_manual(,values = c(salmon,skyblue2))



我还用 position_dodge ()显式地显示你问题2。如果仔细观察,可以看到红线在左侧稍微伸出一点。这是两条线在垂直方向上相互遮住(不是很成功)的结果。



您可以通过交换x和y坐标来解决问题2。在这种情况下,水平躲避是正确的做法:

  ggplot(data = df_all,aes(y = var,x = b,color = group))+ 
geom_line(position = position_dodge(width = 0.5))+
scale_color_manual(,values = c(salmon,skyblue2))



然后,最后一步是使用 coord_flip()来获得所需的图表:

  ggplot(data = df_all,aes(y = var,x = id,color = group))+ 
geom_line(position = position_dodge(width = 0.5))+
scale_color_manual ,values = c(salmon,skyblue2))+
coord_flip()


This has been something I've been experimenting with to find a fix for a while, but basically I was wondering if there is a quick way to "dodge" lineplots for two different data sets in ggplot2.

My code is currently:

#Example data
id <- c("A","A")
var <- c(1,10)
id_num <- c(1,1)
df1 <- data.frame(id,var,id_num)

id <- c("A","A")
var <- c(1,15)
id_num <- c(0.9,0.9)
df2 <- data.frame(id,var,id_num)


#Attempted plot
dodge <- position_dodge(width=0.5)
p<- ggplot(data= df1, aes(x=var, y=id))  +
  geom_line(aes(colour="Group 1"),position="dodge") + 
  geom_line(data= df2,aes(x=var, y=id,colour="Group 2"),position="dodge") +
  scale_color_manual("",values=c("salmon","skyblue2"))
p

Which produces:

Here the "Group 2" line is hiding all of the "Group 1" line which is not what I want. Instead, I want the "Group 2" line to be below the "Group 1" line. I've looked around and found this previous post: ggplot2 offset scatterplot points but I can't seem to adapt the code to get two geom_lines to dodge each other when using separate data frames.

I've been converting my y-variables to numeric and slightly offsetting them to get the desired output, but I was wondering if there was a faster/easier way to get the same result using the dodge functionality of ggplot or something else.

My work around code is simply:

p<- ggplot(data= df1, aes(x=var, y=id_num))  +
  geom_line(aes(colour="Group 1")) + 
  geom_line(data= df2,aes(x=var, y=id_num,colour="Group 2")) +
  scale_color_manual("",values=c("salmon","skyblue2")) + 
  scale_y_continuous(lim=c(0,1))
p

Giving me my desired output of:

Desired output:

The numeric approach can be a little cumbersome when I try to expand it to fit my actual data. I have to convert my y-values to factors, change them to numeric and then merge the values onto the second data set, so a quicker way would be preferable. Thanks in advance for your help!

解决方案

You have actually two issues here:

  1. If the two lines are plotted using two layers of geom_line() (because you have two data frames), then each line "does not know" about the other. Therefore, they can not dodge each other.

  2. position_dodge() is used to dodge in horizontal direction. The standard example is a bar plot, where you place various bars next to each other (instead of on top of each other). However, you want to dodge in vertical direction.

Issue 1 is solved by combining the data frames into one as follows:

library(dplyr)
df_all <- bind_rows(Group1 = df1, Group2 = df2, .id = "group")
df_all
## Source: local data frame [4 x 4]
## 
##    group     id   var id_num
##    (chr) (fctr) (dbl)  (dbl)
## 1 Group1      A     1    1.0
## 2 Group1      A    10    1.0
## 3 Group2      A     1    0.9
## 4 Group2      A    15    0.9

Note how setting .id = "Group" lets bind_rows() create a column group with the labels taken from the names that were used together with df1 and df2.

You can then plot both lines with a single geom_line():

library(ggplot2)
ggplot(data = df_all, aes(x=var, y=id, colour = group))  +
   geom_line(position = position_dodge(width = 0.5)) +
   scale_color_manual("",values=c("salmon","skyblue2"))

I also used position_dodge() to show you issue 2 explicitly. If you look closely, you can see the red line stick out a little on the left side. This is the consequence of the two lines dodging each other (not very successfully) in vertical direction.

You can solve issue 2 by exchanging x and y coordinates. In that situation, dodging horizontally is the right thing to do:

ggplot(data = df_all, aes(y=var, x=id, colour = group))  +
   geom_line(position = position_dodge(width = 0.5)) +
   scale_color_manual("",values=c("salmon","skyblue2"))

The last step is then to use coord_flip() to get the desired plot:

ggplot(data = df_all, aes(y=var, x=id, colour = group))  +
   geom_line(position = position_dodge(width = 0.5)) +
   scale_color_manual("",values=c("salmon","skyblue2")) +
   coord_flip()

这篇关于R - ggplot避开geom_lines的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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