问题:我找不到任何方法在
有什么办法可以修复它吗?
ggplot2
中结合断点和限制命令。y轴应始终包含0-40的范围,breaks=c(5,10,15,20,25,30,35)
。x轴应为0-100,breaks=c(10,20,30,40,50,60,70,80,90,100)
。我不想显示超出此范围的数据。
我尝试了+ ylim
,但这会覆盖我的断点。
我尝试了+ expand
,但这也显示了我不想要的范围之外的数据(1-100)。
我尝试了在第二步添加断点和限制范围,但如果这样做,我的第一步的y轴就会被简单地覆盖。
plot_Tili_Age_VS_Height <- ggplot(Tili, aes(x = Age, y = Height)) + geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "black", formula = y ~ x) +
scale_y_continuous(trans = "log10", breaks = c(5, 10, 15, 20, 25, 30, 35)) +
expand_limits(y = c(0, 35), x = c(0, 100)) +
scale_x_continuous(trans = "log10", breaks = c(10, 20, 30, 40, 50, 60,70, 80, 90, 100)) +
theme_bw(base_size = 15) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
df <- data.frame(x = log(Tili$Age), y = log(Tili$Height))
lm_eqn = function(df) {
m = lm(y ~ x, df)
eq <- substitute(ln(italic(y)) == a + b %*% ln(italic(x)) * "," ~ ~italic(r)^2 ~
"=" ~ r2, list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 2)))
as.character(as.expression(eq))
}
plot_Tili_Age_VS_Height <- plot_Tili_Age_VS_Height +
annotate("text", x = 30, y = 5, label = lm_eqn(df), hjust = 0,
size = 3, family = "Times", parse = TRUE)
plot_Tili_Age_VS_Height
有什么办法可以修复它吗?
scale_x_continuous
中使用limits
参数吗? - aosmithexpand_limits()
,不要使用ylim
或xlim
,只需使用scale_y_continuous
和scale_x_continuous
,它们都接受breaks
和limits
参数。 - Gregor Thomas