使用`jitterdodge`时对齐ggplot中的点和误差条 [英] align points and error bars in ggplot when using `jitterdodge`
问题描述
下面的可重现数据包含每个动物(猫和狗)对于两个协变量(cov1和cov2)和它们各自的误差估计(SE)的每个季节(夏季和冬季)的50个观测值。
library(ggplot2);库(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)
季节种类cov1 cov1SE cov2 cov2SE
1夏日狗29 -0.71040656 24 -0.07355602
2夏日狗79 0.25688371 69 -1.16865142
夏日狗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))$ b $ (SEV,SE,c(COV1SE,COV2SE))
datLong <-EstLong [,c(1,2,5,6)]
datlong $ SE< - SE [,6]
头(datlong)
赛季种类Cov估计SE
1夏季狗队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
来闪避和抖动的点(如
您可以扩展 position_dodge
来生成一个修正抖动
为数据:
myjit < - ggproto(fixJitter,PositionDodge,
width = 0.3,
dodge.width = 0.1,
jit = NULL,
compute_panel =函数(self,data,params,scale)
{
#Generate Jitter if if not尚未
if(is.null(self $ jit)){
self $ jit< -jitter(rep(0,nrow(data)),amount = self $ dodge.width)
}
data <-ggproto_parent(PositionDodge,self)$ compute_panel(数据,参数,比例)
data $ x < - data $ x + self $ jit
#为正确的错误扩展
if(xmin%in%colnames( (data))data $ xmin < - data $ xmin + self $ jit
if(xmax%in colnames(data))data $ xmax< - data $ xmax + self $ jit
数据
})
ggplot(datlong,aes(y =估计值,x = Cov,color =物种,组=物种)) +
geom_point(position = myjit,size = 1)+
geom_errorbar(aes(ymin = Estimate-SE,ymax =估计+ SE),width = 0.2,position = myjit)+
theme_bw()+
facet_wrap(〜Season,ncol = 1,scales =free)+
scale_color_manual(values = c(blue,red))
请注意,您必须为每个图创建一个新对象 fixJitter
。
以下是情节:
The reproducible data below contains 50 observations for each animal (cat and dog) for each season (Summer and Winter) for two covariates (cov1 and cov2) and their respective error estimates (SE).
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
Below I gather the data into long format for 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
I am trying to plot all points and am using position_jitterdodge
to dodge and jitter the points (as suggested in this SO post), but cannot correctly align the error bars with the respective points as shown below. position_dodge
correctly aligns points and error bars, but jitter
is needed to reduce overlap along the x-axis. Any suggestions would be greatly appreciated.
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"))
You can extend the position_dodge
to generate a fix jitter
for the data:
myjit <- ggproto("fixJitter", PositionDodge,
width = 0.3,
dodge.width = 0.1,
jit = NULL,
compute_panel = function (self, data, params, scales)
{
#Generate Jitter if not yet
if(is.null(self$jit) ) {
self$jit <-jitter(rep(0, nrow(data)), amount=self$dodge.width)
}
data <- ggproto_parent(PositionDodge, self)$compute_panel(data, params, scales)
data$x <- data$x + self$jit
#For proper error extensions
if("xmin" %in% colnames(data)) data$xmin <- data$xmin + self$jit
if("xmax" %in% colnames(data)) data$xmax <- data$xmax + self$jit
data
} )
ggplot(datLong, aes(y = Estimate, x = Cov, color = Species, group=Species)) +
geom_point(position = myjit, size = 1) +
geom_errorbar(aes(ymin = Estimate-SE, ymax = Estimate+SE), width = 0.2, position = myjit)+
theme_bw() +
facet_wrap(~ Season, ncol = 1, scales = "free") +
scale_color_manual(values = c("blue", "red"))
Note that you have to create a new object fixJitter
for every plot.
Here is the plot:
这篇关于使用`jitterdodge`时对齐ggplot中的点和误差条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!