gpt4 book ai didi

r - axis.break和ggplot2或gap.plot?情节可能太复杂

转载 作者:行者123 更新时间:2023-12-02 06:24:40 27 4
gpt4 key购买 nike

我用 ggplot2 创建了一个绘图。与牛奶蛋白质含量有关。我有两组和 4 次治疗。我想展示小组和治疗、手段和误差线之间的相互作用。蛋白质含量从2.6%开始。现在我的 y Axis 从那里开始没有间隙,但我的主管想要有一个间隙。我尝试了plotrix库的axis.break(),但什么也没发生。我尝试用gap.plot重建图形,但没有成功,但我必须承认我不是R英雄。

这是我的图形的代码:

Protein<-ggplot(data=D, aes(x=treat, y=Prot,group=group, shape=group))+
geom_line(aes(linetype=group), size=1, position=position_dodge(0.2))+
geom_point(size=3, position=position_dodge(0.2))+
geom_errorbar(aes(ymin=Prot-Prot_SD,ymax=Prot+Prot_SD), width=.2,
position=position_dodge(0.2))+
scale_shape_discrete(name='group\n', labels=c('1\n(n =
22,19,16,20)\n','2\n(n = 15,12,14,12)'))+
scale_linetype_discrete(name="group\n", labels=c('control\n(n =
22,19,16,20)\n','free-contact\n(n = 15,12,14,12)'))+
scale_x_discrete(labels=c('0', '1', '2', '3'))+
labs(x='\ntreatment', y='protein content (%)\n')
ProtStar<-Protein+annotate("text", x=c(1,2,3,4), y=c(3.25,3.25,3.25,3.25),
label=c("Aa","Aa","Ab","Ba"), size=4)
plot(ProtStar)

不幸的是,我没有足够的声誉来发布图像,但您可能从代码中看到该图形很复杂。

如果您能提供有用的建议,那就太好了。非常感谢!

最佳答案

TL;DR:看底部。

考虑这些数字:

ggplot(iris, aes(Species, Sepal.Length)) + geom_boxplot() + 
theme_classic()

enter image description here

这是你的基本情节。现在您必须考虑 Y Axis 。

<小时/>
ggplot(iris, aes(Species, Sepal.Length)) + geom_boxplot() + 
theme_classic() +
scale_y_continuous(limits = c(0,NA), expand = c(0,0))

enter image description here

这是强调数据有零下限的最小误导性方式,即使没有低于某个值的实际点。牛奶蛋白百分比是一个很好的数据示例,负值是不可能的,您想强调这一点,但没有观察值接近于零。

这也缩小了 Y Axis 的解释范围,从而减少了观测值之间的差异。如果这是你想强调的事情,那很好。但如果某些数据的自然范围很窄,包括零(以及由此产生的空白空间)就会产生误导。例如,如果牛奶蛋白始终在 2.6% 到 2.7% 之间,那么零值并不是数据的真正下限,而是与 -50% 一样不可能。

<小时/>
ggplot(iris, aes(Species, Sepal.Length)) + geom_boxplot() + 
theme_classic() +
scale_y_continuous(limits = c(0,NA), expand = c(0,0)) +
theme(axis.line.y = element_blank()) +
annotate(geom = "segment", x = -Inf, xend = -Inf, y = -Inf, yend = Inf)

enter image description here

不包含损坏的 Y Axis 的原因有很多。许多人认为将数据包含在数据范围内是不道德的或具有误导性的。但这个特殊情况已经超出了实际数据的极限。我认为规则可以为此稍微改变一下。

第一步是删除自动 Y 轴线并使用 annotate“手动”绘制它。请注意,该图看起来与上一图相同。如果您选择的主题使用了很多不同的尺寸,那么您的日子将会很糟糕。

<小时/>
ggplot(iris, aes(Species, Sepal.Length)) + geom_boxplot() + 
theme_classic() +
scale_y_continuous(limits = c(3.5,NA), expand = c(0,0),
breaks = c(3.5, 4:7)) +
theme(axis.line.y = element_blank()) +
annotate(geom = "segment", x = -Inf, xend = -Inf, y = -Inf, yend = Inf)

enter image description here

现在您可以考虑实际数据从哪里开始以及哪里是放置中断的好位置。你必须用手检查;例如min(iris$Sepal.Length) 并考虑刻度线的位置。这是个人判断。

我发现最低值为 4.3。我知道我希望中断时间低于最小值,并且我希望中断时间约为 0.5 个单位长。因此,我选择在 3.5 处打勾,然后用 breaks = c(3.5, 4:7) 标记每个整数。

<小时/>
ggplot(iris, aes(Species, Sepal.Length)) + geom_boxplot() + 
theme_classic() +
scale_y_continuous(limits = c(3.5,NA), expand = c(0,0),
breaks = c(3.5, 4:7), labels = c(0, 4:7)) +
theme(axis.line.y = element_blank()) +
annotate(geom = "segment", x = -Inf, xend = -Inf, y = -Inf, yend = Inf)

enter image description here

现在我们需要使用 labels = c(0, 4:7) 将 3.5 刻度重新标记为假零。

<小时/>
ggplot(iris, aes(Species, Sepal.Length)) + geom_boxplot() + 
theme_classic() +
scale_y_continuous(limits = c(3.5,NA), expand = c(0,0),
breaks = c(3.5, 4:7), labels = c(0, 4:7)) +
theme(axis.line.y = element_blank()) +
annotate(geom = "segment", x = -Inf, xend = -Inf, y = -Inf, yend = Inf) +
annotate(geom = "segment", x = -Inf, xend = -Inf, y = 3.5, yend = 4,
linetype = "dashed", color = "white")

enter image description here

现在,我们在手动绘制的轴线上绘制一条白色虚线,从假零 (y=3.5) 到最低的真实刻度线 (y=4)。

认为图形语法是一种成熟的哲学;也就是说,每个元素背后都有深思熟虑的推理。事实上,这样做是有充分理由的,你需要考虑你自己的理由是否对对方有足够的分量。

关于r - axis.break和ggplot2或gap.plot?情节可能太复杂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46403240/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com