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

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

问题描述

  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, (数据,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)

几乎所有的行都有重叠。有没有办法以某种方式抖动线条,使它们彼此接近,但不重叠。或者做一些事情知道有两行?

解决方案

有一个函数可以做到这个叫做抖动。如果你只是想添加抖动到图中的行,下面的代码将执行它:

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

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

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?

解决方案

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)

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

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

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