ggplot2 position_dodge会影响错误栏宽度 [英] ggplot2 position_dodge affects error bar width

查看:819
本文介绍了ggplot2 position_dodge会影响错误栏宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用errorbars制作线条和点图。它有不同的因素,但有些因素只有一个值。我发现如果我使用position_dodge,与图中其他误差条相比,单值因子中的一个具有更宽的误差条。不知何故,position_dodge对错误栏上的宽度有影响。我没有发现任何人有过同样的问题,所以我希望有人能帮助我。



虚拟数据:

pre $ require(ggplot2)

x < - c(1,1,1,1,2,2,2,3,3,3,3,4,4,4,4,3,3,5)
y <-c(3,5,6,3,5,3,5,6,2,6,3,7,3,6,2,1,5,8,7)
se (-A),B,C,D,A,B,C,D,A B,C,D,A,B,C,D,E,F,G)
data < data.frame(x,y,se,治疗)
data $治疗< - as.factor(data $治疗)

首先没有position_dodge的情节 - 一切正常

 #无位置闪避
myplot <-ggplot(data,aes(x = x,y = y,group = treatment,fill = treatment,color = treatment))+
geom_line(stat =identity,size = 1)+
geom_point(stat =identity,size = 3,shape = 21)+
geom_errorbar(aes(ymin = y-se,ymax = y + se),width = 0.2)

myplot



现在有位置闪避的情节:

 #随着位置减退
myplot < - ggplot(data,aes(x = x,y = y,group = treatment,fill = treatment,color = treatment) )+
geom_line(stat =identity,size = 1,position = position_dodge(width = 0.2))+
geom_point(stat =identity,size = 3,shape = 21,position = (宽度= 0.2))+
geom_errorbar(aes(ymin = y-se,ymax = y + se),width = 0.2,position = position_dodge(width = 0.2))

myplot



正如您所看到的,与其他错误栏相比,最右侧的错误栏的宽度更大。这可能是因为这个点没有重叠的x变量,并且误差条可以具有正常的大小。我仍然想知道如何让错误栏具有相同的宽度。

解决方案

正如@aosmith所建议的那样,解决方法是将错误栏的宽度缩放为点的数量 X 。但是,这不需要手动完成。在下面,我使用 dplyr data.frame 中创建一个新列,该列基于 X 。我还删除了填充映射,因为这里不需要(只要形状更改为版本由 color 而不是 fill >着色的实心圆圈。最后,为了避免重复,我定义了位置一次,然后为每个 geom 使用一个变量。


$ b

  library(dplyr)
data < - data%>%
group_by (x)%>%
mutate(
width = 0.1 * n()


pos< - position_dodge(width = 0.2)
myplot< -
ggplot(data,
aes(
x = x,
y = y,
color = treatment,
width = width
$)gent_line(size = 1,position = pos)+
geom_point(size = 3,shape = 16,position = pos)+
geom_errorbar(aes(ymin = y - se,ymax = y + se),position = pos)

myplot


I am trying to make a line and point graph with errorbars. It has different factors, however some factors have only one value. I found out that if i use position_dodge, one of the single value factor has a much wider error bar compared to the other error bars in the graphs. Somehow position_dodge has an influence on the width on the error bar. I did not found anyone that had the same problem before so I hope that someone can help me.

The dummy data:

require(ggplot2)

x <- c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,3,3,5)
y <- c(3,5,6,3,5,3,5,6,2,6,3,7,3,6,2,1,5,8,7)
se <- x*0.2
treatment <- c("A", "B","C", "D","A", "B","C", "D","A", "B","C", "D","A",    "B","C", "D","E", "F", "G" )
data <- data.frame(x, y, se ,treatment)
data$treatment <- as.factor(data$treatment)

First a plot without position_dodge - everything is fine

# Without position dodge
myplot <- ggplot(data, aes(x=x, y=y, group= treatment,  fill = treatment, colour = treatment)) +
  geom_line(stat="identity", size = 1) +
  geom_point(stat="identity", size = 3, shape = 21) + 
  geom_errorbar(aes(ymin = y-se, ymax = y+se), width = 0.2)

myplot

Now a plot with position dodge:

# With position dodge
myplot <- ggplot(data, aes(x=x, y=y, group= treatment,  fill = treatment, colour = treatment)) +
  geom_line(stat="identity", size = 1, position=position_dodge(width=0.2)) +
  geom_point(stat="identity", size = 3, shape = 21, position=position_dodge(width=0.2)) + 
  geom_errorbar(aes(ymin = y-se, ymax = y+se), width = 0.2, position=position_dodge(width=0.2))

myplot

As you can see the error bar on the far right has a much larger width compared to the other error bars. This is probably because there are no overlapping x variables for this point, and than the error bars can have a normal size. I still would like to know how I can get the error bars to have the same width.

解决方案

As @aosmith suggests, the fix for this is to scale the width of the error bar to the number of points with that x. However, this does not need to be done manually. Below I use dplyr to create a new column in the data.frame based on the number of points at that x. I've also removed the group and fill mappings since neither is needed here (provided the shape is changed to the version of a filled circle coloured by colour rather than fill). Finally, to avoid repetition I've defined the position once and then used a variable for each geom.

library(dplyr)
data <- data %>%
  group_by(x) %>%
  mutate(
    width = 0.1 * n()
  )

pos <- position_dodge(width = 0.2)
myplot <-
  ggplot(data,
         aes(
           x = x,
           y = y,
           colour = treatment,
           width = width
         )) +
  geom_line(size = 1, position = pos) +
  geom_point(size = 3, shape = 16, position = pos) +
  geom_errorbar(aes(ymin = y - se, ymax = y + se), position = pos)

myplot

这篇关于ggplot2 position_dodge会影响错误栏宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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