ggplot:重新缩放轴(对数)和切割轴
ggplot: rescale axis (log) and cut axis
我想在 R 中绘制一个像这样的非常简单的箱线图:
想要的图表
是一个log-link(Gamma分布:jh_conc
是一个激素浓度变量)一个分类分组变量的连续因变量(jh_conc
)的广义线性模型(组:type of bee
)
我已有的脚本是:
> jh=read.csv("data_jh_titer.csv",header=T)
> jh
group jh_conc
1 Queens 6.38542714
2 Queens 11.22512563
3 Queens 7.74472362
4 Queens 11.56834171
5 Queens 3.74020100
6 Virgin Queens 0.06080402
7 Virgin Queens 0.12663317
8 Virgin Queens 0.08090452
9 Virgin Queens 0.04422111
10 Virgin Queens 0.14673367
11 Workers 0.03417085
12 Workers 0.02449749
13 Workers 0.02927136
14 Workers 0.01648241
15 Workers 0.02150754
fit1=glm(jh_conc~group,family=Gamma(link=log), data=jh)
ggplot(fit, aes(group, jh_conc))+
geom_boxplot(aes(fill=group))+
coord_trans(y="log")
结果图如下所示:
我的问题是:我可以使用什么(geom)扩展来拆分 y 轴并重新缩放它们?另外,我如何添加黑色圆圈(平均值;按对数刻度计算然后反向转换为原始刻度)水平线,它们是基于对对数转换数据执行的事后测试的显着性水平:**:p<0.01 , *** :p< 0.001?
您无法在 ggplot2
中创建一个破损的数字轴,主要是因为它在视觉上扭曲了所表示的 data/differences 并被认为具有误导性。
但是,您可以使用 scale_log10() + annotation_logticks()
来帮助压缩各种值的数据或更好地显示异方差数据。您还可以使用 annotate
构建您的 p-value 代表星和条。
您还可以使用它的命名属性轻松地从模型中获取信息,这里我们关心 fit$coef
:
# make a zero intercept version for easy plotting
fit2 <- glm(jh_conc ~ 0 + group, family = Gamma(link = log), data = jh)
# extract relevant group means and use exp() to scale back
means <- data.frame(group = gsub("group", "",names(fit2$coef)), means = exp(fit2$coef))
ggplot(fit, aes(group, jh_conc)) +
geom_boxplot(aes(fill=group)) +
# plot the circles from the model extraction (means)
geom_point(data = means, aes(y = means),size = 4, shape = 21, color = "black", fill = NA) +
# use this instead of coord_trans
scale_y_log10() + annotation_logticks(sides = "l") +
# use annotate "segment" to draw the horizontal lines
annotate("segment", x = 1, xend = 2, y = 15, yend = 15) +
# use annotate "text" to add your pvalue *'s
annotate("text", x = 1.5, y = 15.5, label = "**", size = 4) +
annotate("segment", x = 1, xend = 3, y = 20, yend = 20) +
annotate("text", x = 2, y = 20.5, label = "***", size = 4) +
annotate("segment", x = 2, xend = 3, y = .2, yend = .2) +
annotate("text", x = 2.5, y = .25, label = "**", size = 4)
我想在 R 中绘制一个像这样的非常简单的箱线图:
想要的图表
是一个log-link(Gamma分布:jh_conc
是一个激素浓度变量)一个分类分组变量的连续因变量(jh_conc
)的广义线性模型(组:type of bee
)
我已有的脚本是:
> jh=read.csv("data_jh_titer.csv",header=T)
> jh
group jh_conc
1 Queens 6.38542714
2 Queens 11.22512563
3 Queens 7.74472362
4 Queens 11.56834171
5 Queens 3.74020100
6 Virgin Queens 0.06080402
7 Virgin Queens 0.12663317
8 Virgin Queens 0.08090452
9 Virgin Queens 0.04422111
10 Virgin Queens 0.14673367
11 Workers 0.03417085
12 Workers 0.02449749
13 Workers 0.02927136
14 Workers 0.01648241
15 Workers 0.02150754
fit1=glm(jh_conc~group,family=Gamma(link=log), data=jh)
ggplot(fit, aes(group, jh_conc))+
geom_boxplot(aes(fill=group))+
coord_trans(y="log")
结果图如下所示:
我的问题是:我可以使用什么(geom)扩展来拆分 y 轴并重新缩放它们?另外,我如何添加黑色圆圈(平均值;按对数刻度计算然后反向转换为原始刻度)水平线,它们是基于对对数转换数据执行的事后测试的显着性水平:**:p<0.01 , *** :p< 0.001?
您无法在 ggplot2
中创建一个破损的数字轴,主要是因为它在视觉上扭曲了所表示的 data/differences 并被认为具有误导性。
但是,您可以使用 scale_log10() + annotation_logticks()
来帮助压缩各种值的数据或更好地显示异方差数据。您还可以使用 annotate
构建您的 p-value 代表星和条。
您还可以使用它的命名属性轻松地从模型中获取信息,这里我们关心 fit$coef
:
# make a zero intercept version for easy plotting
fit2 <- glm(jh_conc ~ 0 + group, family = Gamma(link = log), data = jh)
# extract relevant group means and use exp() to scale back
means <- data.frame(group = gsub("group", "",names(fit2$coef)), means = exp(fit2$coef))
ggplot(fit, aes(group, jh_conc)) +
geom_boxplot(aes(fill=group)) +
# plot the circles from the model extraction (means)
geom_point(data = means, aes(y = means),size = 4, shape = 21, color = "black", fill = NA) +
# use this instead of coord_trans
scale_y_log10() + annotation_logticks(sides = "l") +
# use annotate "segment" to draw the horizontal lines
annotate("segment", x = 1, xend = 2, y = 15, yend = 15) +
# use annotate "text" to add your pvalue *'s
annotate("text", x = 1.5, y = 15.5, label = "**", size = 4) +
annotate("segment", x = 1, xend = 3, y = 20, yend = 20) +
annotate("text", x = 2, y = 20.5, label = "***", size = 4) +
annotate("segment", x = 2, xend = 3, y = .2, yend = .2) +
annotate("text", x = 2.5, y = .25, label = "**", size = 4)