r - 躲避误差条和点以避免重叠

标签 r ggplot2 errorbar

我正在尝试修复错误栏,以便它们在我的图表上实际上是可读的。唯一造成问题的是 2013 年的数据。我该怎么做呢?我看到了一些关于抖动或闪避的帖子,但我不确定如何应用它来解决我的问题。

这是我尝试更改的代码:

YearlyDensity <- read.table(header=T, text='
Station Year       mean           se
   M-25 2013   8944.444     3636.871
   M-25 2008       4212         2371
   M-25 2004        963          291
   M-45 2013   8495.169     2111.072
   M-45 2008      13023         1347
   M-45 2004       8748         1740
    X-2 2013  12345.411     1166.905
')    

library(ggplot2)
ggplot(YearlyDensity, aes(x=Year, y=mean, colour=Station,group=Station)) +
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), colour="black", width=.2) +
  geom_line(size=.8) +
  geom_point(size=4, shape=18) +
  coord_cartesian(ylim = c(0, 16000)) +
  scale_y_continuous(breaks=seq(0,16000,2000)) +
  xlab("Sampling Year") +
  ylab("Mean Density") +
  labs(fill="") +
  theme_bw() +
    theme(legend.justification=c(1,0), legend.position=c(1,0))

最佳答案

您设置dodge width为所有geom设置相同的值s,即添加position = position_dodge(width = <the-desired-width>)给他们每个人。请参阅What is the width argument in position_dodge?了解详情。

# set desired dodge width
pd <- position_dodge(width = 0.4)

ggplot(YearlyDensity, aes(x = Year, y = mean, colour = Station, group = Station)) +
  geom_errorbar(aes(ymin = mean-se, ymax = mean+se),
                colour = "black", width = 0.2, position = pd) +
  geom_line(size = .8, position = pd) +
  geom_point(size = 4, shape = 18, position = pd) +
  coord_cartesian(ylim = c(0, 16000)) +
  scale_y_continuous(breaks = seq(0, 16000, 2000)) +
  xlab("Sampling Year") +
  ylab("Mean Density") +
  labs(fill = "") +
  theme_bw() +
  theme(legend.justification = c(1, 0), legend.position = c(1, 0))

enter image description here

关于r - 躲避误差条和点以避免重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22917056/

相关文章:

r - 从 R 中的模型中提取公式

r - 为什么 R 中的 xts 对象除以常量会得到矩阵对象(丢失日期列)?

r - 选择要在 ggplot2 中绘制的数据框列

r - 如何使用 ggplot 更改轴上数字的格式?

python - 错误栏不在数据处

python - 如何在误差线旁边添加误差值?

r - 将错误栏添加到 ggplot 时出错

r - 如何迁移 Rstudio 文件和已安装的软件包(按版本到新计算机)

r - 检查 S3 通用/方法一致性......警告

r - 更改气泡图中使用的大小范围