gpt4 book ai didi

r - 使用 R 绘图在 R 中绘制点图

转载 作者:行者123 更新时间:2023-12-02 15:56:29 25 4
gpt4 key购买 nike

生成具有这样两个因素的点图的最佳方法是什么,最好使用标准 R 图(而不是 ggplot)并从 2x2 数据框生成。水平线应该是手段。我尝试过克利夫兰点图,但无法弄清楚如何获取两个数据系列并使点抖动:

example dotplot

最佳答案

下面的代码应该可以解决问题:

set.seed(1)
t1 = rnorm(10); t2 = rnorm(10, 2)
t1_g2 = rnorm(10, 4);t2_g2 = rnorm(10)

##Don't print the axes labels
par(ann=FALSE)

##Plot first set of data.
##Need to check for sensible ranges
##Use the jitter function to spread data out.
plot(jitter(rep(0,10),amount=0.2), t1,
xlim=range(-0.5,3.5), ylim=range(-3,8),
axes=FALSE,frame.plot=TRUE)
points(jitter(rep(1,10), amount=0.2), t1_g2, col=2)
points(jitter(rep(2,10), amount=0.2), t2)
points(jitter(rep(3,10), amount=0.2), t2_g2, col=2)

##Add in the y-axis
axis(2, seq(-4,8,by=2))

##Add in the x-axis labels
mtext("Treatment 1", side = 1, at=0.5)
mtext("Treatment 2", side = 1, at=2.5)

##Add in the means
segments(-0.25, mean(t1), 0.25, mean(t1))
segments(0.75, mean(t1_g2), 1.25, mean(t1_g2))
segments(1.75, mean(t2), 2.25, mean(t2))
segments(2.75, mean(t2_g2), 3.25, mean(t2_g2))

##Add in the legend
legend(0, 8, c("Group 1", "Group 2"), col=1:2, pch=1)

给出:

enter image description here

关于r - 使用 R 绘图在 R 中绘制点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7418386/

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