避开误差线和误差点,以避免重叠 [英] Dodge error bars and points to avoid overlap

查看:150
本文介绍了避开误差线和误差点,以避免重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修复误差线,以使误差线在我的图形上实际上是可读的.造成问题的唯一原因是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))

解决方案

您要将所有geomdodge width设置为相同的值,即将position = position_dodge(width = <the-desired-width>)添加到每个position = position_dodge(width = <the-desired-width>).参见 position_dodge中的width参数是什么?了解详情.

# 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))

I'm trying to fix the error bars so that they are actually readable on my graph. The only one that are causing problems are on the 2013 data. How do I go about doing this? I saw some posts about jitter or dodge but I am not sure how to apply that to fix my issue.

Here is the code I am attempting to alter:

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))

解决方案

You to set the dodge width to the same value for all geoms, i.e. add position = position_dodge(width = <the-desired-width>) to each of them. See What is the width argument in position_dodge? for details.

# 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))

这篇关于避开误差线和误差点,以避免重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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