1
以下可重复数据包含每个动物(猫和狗)对于两个协变量(cov1和cov2)以及它们各自的误差的每个季节(夏季和冬季)的50个观测值估计(SE)。使用`jitterdodge`时,在ggplot中对齐点和误差线
library(ggplot2); library(dplyr); library(tidyr)
set.seed(123)
dat <- data.frame(Season = rep(c("Summer", "Winter"), each = 100),
Species = rep(c("Dog", "Cat", "Dog", "Cat"), each = 50),
cov1 = sample(1:100, 200, replace = TRUE),
cov1SE = rnorm(200),
cov2 = sample(1:100, 200, replace = TRUE),
cov2SE = rnorm(200))
head(dat)
Season Species cov1 cov1SE cov2 cov2SE
1 Summer Dog 29 -0.71040656 24 -0.07355602
2 Summer Dog 79 0.25688371 69 -1.16865142
3 Summer Dog 41 -0.24669188 23 -0.63474826
4 Summer Dog 89 -0.34754260 32 -0.02884155
5 Summer Dog 95 -0.95161857 18 0.67069597
6 Summer Dog 5 -0.04502772 81 -1.65054654
下面我收集的数据为长格式ggplot
EstLong <- dat %>% gather(Cov, Estimate, c(cov1, cov2))
SE <- dat %>% gather(Cov, SE, c(cov1SE, cov2SE))
datLong <- EstLong[ , c(1,2,5,6)]
datLong$SE <- SE[ , 6]
head(datLong)
Season Species Cov Estimate SE
1 Summer Dog cov1 29 -0.71040656
2 Summer Dog cov1 79 0.25688371
3 Summer Dog cov1 41 -0.24669188
4 Summer Dog cov1 89 -0.34754260
5 Summer Dog cov1 95 -0.95161857
6 Summer Dog cov1 5 -0.04502772
我试图绘制所有点和正在使用position_jitterdodge
躲闪和抖动点(如this SO post建议),但不能如下所示将误差线与相应的点正确对齐。 position_dodge
正确对齐点和误差线,但需要jitter
以减少沿x轴的重叠。任何建议将不胜感激。
Jit <- position_jitterdodge(dodge.width=0.4)
ggplot(datLong, aes(y = Estimate, x = Cov, color = Species)) +
geom_point(position = Jit, size = 1) +
geom_errorbar(aes(ymin = Estimate-SE, ymax = Estimate+SE), width = 0.2, position = Jit) +
theme_bw() +
facet_wrap(~ Season, ncol = 1, scales = "free") +
scale_color_manual(values = c("blue", "red"))
相关的问题[这里](https://stackoverflow.com/questions/3165346/is-it-possible-to-jitter-two-ggplot-geoms -in最同路)。看起来像是归结为必须创建自己的抖动。 – aosmith
感谢您的参考。关于如何在分组因素中手动抖动作为现有问题的答案的任何想法? –
手动抖动显示在[这里](https://stackoverflow.com/a/39533567/2461552)。看起来'抖动'使用统一的分布,所以你可以总是从制服中自行绘制,以创建新的“抖动”变量。 – aosmith