如何在ggplot2中抖动线条 [英] How to jitter lines in ggplot2

查看:32
本文介绍了如何在ggplot2中抖动线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据和情节:

Say I have the following data and plot:

require(reshape2)
require(ggplot2)
data <- data.frame(id=seq(1,9,1), var1=c(10,3,5,7,8,9,4,6,5), var2=c(9,3,5,7,8,9,4,6,5))
data_graph <- melt(data, id="id")
ggplot(data=data_graph, aes(y=value, x=id, group=variable, col=variable)) +
geom_line(size=2) + geom_point() + 
geom_text(aes(label=value), size=5, hjust=-.6, vjust=1.5)

几乎整条线都有重叠.有没有办法以某种方式抖动线条,使它们彼此靠近,但不重叠.还是做点什么才知道有两行?

For almost the entirety of both lines, there is overlap. Is there any way to somehow jitter the lines so that they are close to each other, but don't overlap. Or to do something to know that there are two lines?

推荐答案

有一个函数可以做到这一点,称为 jitter.如果你只是想给图中的线条添加抖动,下面的代码就可以做到:

There is a function to do just this called jitter. If you just want to add jitter to the lines in the plot, the following code will do it:

ggplot(data=data_graph, aes(y=value, x=id, group=variable, col=variable)) +
  geom_line(size=2, 
            aes(y = jitter(value, 5), x = jitter(id, 2), group=variable, col=variable)) + 
  geom_point() + 
  geom_text(aes(label=value), size=5, hjust=-.6, vjust=1.5)

jitter 函数中的第二个值指定要添加多少抖动

The second value in the jitter function specifies how much jitter to add

这篇关于如何在ggplot2中抖动线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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