[英]ggplot2: axis does not show all ticks/breaks
I am currently plotting data using the ggpubr package in R (based on ggplot2).我目前正在使用 R 中的 ggpubr 包(基于 ggplot2)绘制数据。 When I plot the means of two conditions including standard errors, the y-axis should be limited from 1 to 7, which I indicate using:当我绘制包括标准误差在内的两个条件的均值时,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)
In the final plot, however, the y-axis shows only values from 1 to 6然而,在最终图中,y 轴仅显示 1 到 6 之间的值
I tried to plot the same data using native ggplot2, but the problem persists, once I change the layout.我尝试使用本机 ggplot2 绘制相同的数据,但是一旦更改布局,问题仍然存在。 For ggplot2 I used:对于 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()
It would be great if someone could help me with this issue.如果有人能帮助我解决这个问题,那就太好了。
Edit: as suggested in the comments, here is the data I am trying to plot using ggplot2:编辑:正如评论中所建议的,这是我尝试使用 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))
I think I got something resembling your plot with correct y-axes with the following code:我想我得到了一些类似于你的情节的东西,带有正确的 y 轴,代码如下:
ggplot(data, aes(x = condition, y = measure)) +
geom_point() +
geom_errorbar(aes(ymin = measure-se, ymax = measure+se),
width = .2, position = position_dodge(0.05)) +
# Group prevents geom_line interpreting each x-axis point as it's own group
geom_line(aes(group = rep(1, nrow(data)))) +
xlab("Condition") +
# Expand is optional, it prevents padding beyond 1 and 7
scale_y_continuous(name = "measure",
limits = c(1, 7),
breaks = 1:7,
expand = c(0,0)) +
theme_classic()
The solution is much more trivial.解决方案要简单得多。 You were doing everything right!你做的一切都是对的! Except for one clerical error.除了一处笔误。 Here is what was happening:这是发生的事情:
First, you generate your initial plot, fine.首先,您生成初始图,很好。
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")
This plot does not have the limits.这个情节没有限制。 When you add the limits and display it, the scales are correct:添加限制并显示时,比例是正确的:
p + scale_y_continuous(name="measure", limits=c(1, 7), breaks=c(1:7))
However, note that p did not change !但是,请注意p 没有改变! You did not store the result of adding the limits to p.您没有存储将限制添加到 p 的结果。 Therefore, p is still without the scale_y_continuous.因此,p 仍然没有 scale_y_continuous。 No wonder then that when you type难怪当你打字的时候
p + theme_classic()
...the limits are gone. ......限制消失了。 However, if you try但是,如果您尝试
p <- p + scale_y_continuous(name="measure", limits=c(1, 7), breaks=c(1:7))
p + theme_classic()
everything will be correct.一切都会正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.