R:如何传播(抖动)点相对于X轴? [英] R: How to spread (jitter) points with respect to the x axis?

查看:148
本文介绍了R:如何传播(抖动)点相对于X轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  dat<  -  data.frame(cond = factor(rep( A,10)),
rating = c(1,2,3,4,6,6,7,8,9,10))
ggplot(dat,aes(x = cond ,y = rating))+
geom_boxplot()+
guides(fill = FALSE)+
geom_point(aes(y = 3))+
geom_point(aes(y = 3))+
geom_point(aes(y = 5))

代码会生成一个boxplot,其中一个点超过另一个点(在上述情况下,一个点3会超过另一个点3)。



如何移动点3以便点在y轴的相同位置上保持不变,但它在x轴上向左或向右稍微移动?

解决方案

可以通过使用 position_jitter 函数来实现:

  geom_point(aes( y = 3),position = position_jitter(w = 0.1,h = 0))

更新:
仅绘制三个供给您可以创建一个新的数据集并绘制:

  points_dat<  -  data.frame(cond = factor(rep A,3)),rating = c(3,3,5))
ggplot(dat,aes(x = cond,y = rating))+
geom_boxplot()+
b guides(fill = FALSE)+
geom_point(aes(x = cond,y = rating),data = points_dat,position = position_jitter(w = 0.05,h = 0))


I have the following code snippet in R:

dat <- data.frame(cond = factor(rep("A",10)), 
                  rating = c(1,2,3,4,6,6,7,8,9,10))
ggplot(dat, aes(x=cond, y=rating)) +
  geom_boxplot() + 
  guides(fill=FALSE) +
  geom_point(aes(y=3)) +
  geom_point(aes(y=3)) +
  geom_point(aes(y=5))

This particular snippet of code produces a boxplot where one point goes over another (in the above case one point 3 goes over another point 3).

How can I move the point 3 so that the point remains in the same position on the y axis, but it is slightly moved left or right on the x axis?

解决方案

This can be achieved by using the position_jitter function:

geom_point(aes(y=3), position = position_jitter(w = 0.1, h = 0))

Update: To only plot the three supplied points you can construct a new dataset and plot that:

points_dat <- data.frame(cond = factor(rep("A", 3)), rating = c(3, 3, 5))                  
ggplot(dat, aes(x=cond, y=rating)) +
  geom_boxplot() + 
  guides(fill=FALSE) +
  geom_point(aes(x=cond, y=rating), data = points_dat, position = position_jitter(w = 0.05, h = 0)) 

这篇关于R:如何传播(抖动)点相对于X轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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