gpt4 book ai didi

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

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

我用ggplot2创建了一个图。这与牛奶中的蛋白质含量有关。我有两组和4种治疗方法。我想展示组和处理,均值和错误栏之间的相互作用。蛋白质含量从2.6%开始。现在,我的y轴从那里开始没有间隙,但是我的主管希望有一个。
我尝试了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轴。



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轴的解释范围,因此观察值之间的差异较小。如果您要强调这一点,那可能很好。但是,如果某些数据的自然范围很窄,则包括零(以及由此产生的空白空间)会产生误导。例如,如果牛奶蛋白始终在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轴。许多人认为将一个内部数据范围包括在内是不道德或误导的。但是这种特殊情况超出了实际数据的范围。我认为规则可以为此有所调整。

第一步是删除自动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/

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