R ggplot2 scale_y_continuous : Combining breaks & limits

标签 r ggplot2 limit axis break

问题 :我找不到任何方法来组合 ggplot2 中的中断和限制命令. y Axis 应始终包含 0-40 和 breaks=c(5,10,15,20,25,30,35) 的范围. x Axis 应为 0-100,breaks=c(10,20,30,40,50,60,70,80,90,100) .我不想显示超出此范围的数据。

我试过 + ylim ,但这会覆盖我的休息时间。
我试过 + expand ,但这也显示了我想要的范围之外的数据(1-100)。
我尝试在第二步中添加中断和限制范围,但如果我这样做,我第一步的 y Axis 会被简单地覆盖。

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 

知道如何修复它吗?

最佳答案

正如 JasonAizkalns 评论的那样,如果没有可重复的示例,您的问题就无法解决。下面的代码对虹膜数据执行您想要的操作,并且也适用于您的示例。

library(ggplot2)

df <- iris


## all data, default breaks
ggplot(df, aes(Sepal.Length, Sepal.Width)) +
  geom_point()

## subset of data is seen in plot, breaks changed
ggplot(df, aes(Sepal.Length, Sepal.Width)) +
  geom_point() + 
  scale_x_continuous(breaks = c(5.5,6.5), limits = c(5,7)) +
  scale_y_continuous(breaks = c(3.5,2.5), limits = c(2,4))

关于R ggplot2 scale_y_continuous : Combining breaks & limits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38313204/

相关文章:

r - 根据 Shiny R 中的单选按钮显示 slider 输入

r - dplyr 根据多个条件替换列中的 na 值

r - 围绕栅格单元绘制轮廓

R ggplot2 - 在一个方面对每对执行成对测试并使用 ggsignif 显示 p 值

html - 如何限制文本区域只包含数字?

mysql - 如何查询超过2亿行的表?

sqlite - 如何在 SQLite 中为 DELETE 启用 LIMIT?

r - 如何在 R 中生成向量的所有可能的连续 n 元组?

r - 在R中的闭包中访问变量

r - 带编号的点标签加上散点图中的图例