ggplot2偏移散点图点 [英] ggplot2 offset scatterplot points

查看:821
本文介绍了ggplot2偏移散点图点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两组带误差条的点。我想抵消第二个,所以它显示在第一集略微下降,所以它不会掩盖原来的。



这是一个模拟数据集:

  x = runif(4,-2,2)
y = c(A,B, C,D)
upper = x + 2
lower = x-2
x_1 = runif(4,-1,3)
upper_1 = x_1 + 1
lower_1 = x_1-2

以下是我用来生成图的代码:

  qplot(x,y)+ 
geom_point(size = 6)+
geom_errorbarh(aes(xmax =大,xmin =下),size = 1)+
geom_point(aes(x_1,y),size = 6,pch = 8,vjust = -1,col =grey40)+
geom_errorbarh(aes(xmax = upper_1,xmin = lower_1),size = 1,col =grey40)



<这里是情节:





我希望灰色星号和相关的错误条在黑色圆圈和相关的错误条之下绘制成一条头发。我将转换数据集,但Y轴是分类变量。

使用Richie重组您的数据,这是也可以纯粹在 ggplot 内,而不必混淆轴线:

  dodge < -  position_dodge(width = 0.5)
p < - ggplot(dfr,aes(x = y,y = x,color = type))+
geom_point(aes(shape = type) ,position = dodge)+
geom_errorbar(aes(ymax = upper,ymin = lower),position = dodge)+
scale_colour_manual(values = c('gray','black'))+
scale_shape_manual(values = c(8,19))+
coord_flip()+
opts(legend.position =none)

这给了我这个阴谋:



$ b 注意:自版本0.9.2开始 opts 已被主题替换

  + theme(legend.position =none)


I have two sets of points with error bars. I would like to offset the second so it's displayed slightly down from the first set, so that it doesn't obscure the original.

Here is a mock data set:

x=runif(4,-2,2)
y=c("A","B","C","D")
upper=x+2
lower=x-2
x_1=runif(4,-1,3)
upper_1=x_1+1
lower_1=x_1-2

Here is the code that I used to produce the plot:

qplot(x,y)+
  geom_point(size=6)+
  geom_errorbarh(aes(xmax=upper,xmin=lower),size=1)+
  geom_point(aes(x_1,y),size=6,pch=8,vjust=-1,col="grey40")+
  geom_errorbarh(aes(xmax=upper_1,xmin=lower_1),size=1,col="grey40")

And here is the plot:

I would like the grey asterisks and associated errors bars to be plotted a hair below the black circles and associated error bars. I would transform the data set, but the Y-axis is categorical variables.

解决方案

Using Richie's reorganization of your data, this is also possible purely within ggplot, without having to mess with the axis:

dodge <- position_dodge(width=0.5)  
p <- ggplot(dfr,aes(x=y,y=x,colour=type)) + 
        geom_point(aes(shape=type),position=dodge) +
        geom_errorbar(aes(ymax=upper,ymin=lower),position = dodge) + 
        scale_colour_manual(values = c('gray','black')) +
        scale_shape_manual(values = c(8,19)) +
        coord_flip() + 
        opts(legend.position="none")

which gives me this plot:

Note: Since version 0.9.2 opts has been replaced by theme:

+ theme(legend.position = "none")

这篇关于ggplot2偏移散点图点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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