我目前正在使用 R 中的 ggpubr 包(基于 ggplot2)绘制数据。当我绘制包括标准误差在内的两个条件的均值时,y 轴应限制在 1 到 7 之间,我使用以下方式表示:
p <- ggline(data, x = "condition", y = "measure",
add = c("mean_se"),
ylab = "Measure")
ggpar(y, ylim = c(1, 7), ticks=T, yticks.by = 1)
Run Code Online (Sandbox Code Playgroud)
然而,在最终图中,y 轴仅显示 1 到 6 之间的值
我尝试使用本机 ggplot2 绘制相同的数据,但一旦我更改布局,问题仍然存在。对于 ggplot2 我使用:
p <- ggplot(data, aes(x=condition, y=measure)) +
geom_line() +
geom_point()+
geom_errorbar(aes(ymin=measure-se, ymax=measure+se), width=.2, position=position_dodge(0.05)) +
ylab("measure") +
xlab("Condition")
p + scale_y_continuous(name="measure", limits=c(1, 7), breaks=c(1:7))
p + theme_classic()
Run Code Online (Sandbox Code Playgroud)
如果有人能帮助我解决这个问题,那就太好了。
编辑:根据评论中的建议,这是我尝试使用 ggplot2 绘制的数据:
structure(list(condition = structure(3:4, .Label = c("IC", "SC",
"ILC", "SLC"), class = "factor"), measure = c(4.10233918128655, 3.83040935672515
), se = c(0.235026318386523, 0.216811675834834)), class = "data.frame", row.names = c(NA,
-2L))
Run Code Online (Sandbox Code Playgroud)
解决方案要简单得多。你做的一切都是对的!除了一处笔误。这是发生的事情:
首先,你生成你的初始情节,很好。
p <- ggplot(data, aes(x=condition, y=measure)) +
geom_line() + geom_point() +
geom_errorbar(aes(ymin=measure-se, ymax=measure+se),
width=.2, position=position_dodge(0.05)) +
ylab("measure") +
xlab("Condition")
Run Code Online (Sandbox Code Playgroud)
这个情节没有限制。当您添加限制并显示它时,比例是正确的:
p + scale_y_continuous(name="measure", limits=c(1, 7), breaks=c(1:7))
Run Code Online (Sandbox Code Playgroud)
但是,请注意p 没有改变!您没有存储将限制添加到 p 的结果。因此,p仍然没有scale_y_连续。难怪当你打字时
p + theme_classic()
Run Code Online (Sandbox Code Playgroud)
……限制消失了。但是,如果你尝试
p <- p + scale_y_continuous(name="measure", limits=c(1, 7), breaks=c(1:7))
p + theme_classic()
Run Code Online (Sandbox Code Playgroud)
一切都会正确的。