【问题标题】:Setting axis limits with log scale [duplicate]使用对数刻度设置轴限制[重复]
【发布时间】:2021-02-13 14:25:59
【问题描述】:

我正在尝试创建一个图表,我对此几乎感到满意。我只想更改 x 轴的限制(对数刻度)。我试图在 scale_x_continuous 中设置限制,但这会导致一条直线,而不是十条 S 曲线。到目前为止,我的代码是:

library("ggplot2")
ggplot(evaporation, aes(x = evaporation$h)) +
  geom_line(aes(y = C1_theta, color = "blue"), size = 1.1) +
  geom_line(aes(y = C2_theta, color = "blue"), size = 1.1) +
  geom_line(aes(y = C3_theta, color = "blue"), size = 1.1) +
  geom_line(aes(y = C4_theta, color = "blue"), size = 1.1) +
  geom_line(aes(y = C5_theta, color = "blue"), size = 1.1) +
  geom_line(aes(y = S1_theta, color = "green"), size = 1.1) +
  geom_line(aes(y = S2_theta, color = "green"), size = 1.1) +
  geom_line(aes(y = S3_theta, color = "green"), size = 1.1) +
  geom_line(aes(y = S4_theta, color = "green"), size = 1.1) +
  geom_line(aes(y = S5_theta, color = "green"), size = 1.1) +
  scale_x_continuous(trans = "log", breaks = c(0, 10, 100, 1000, 10000, 100000), limits = 0, 100000) +
  theme_bw() +
  labs(y = expression(theta ~ (cm^3/cm^3)), x = "Absolute Pressure Head h (cm)") +
  scale_color_manual(name = "Samples",
                     labels = c("Control sample", "Treated sample"),
                     values = c("#4472C4", "#70AD47")) +
  theme(legend.position = c(0.82, 0.72), legend.direction = "vertical", legend.background = element_rect(colour = "black", linetype = "solid"))

有谁知道如何确保 x 轴停在 100000 处?

【问题讨论】:

  • 如果您的 x 轴是对数刻度,则 log(0) 的计算结果为 -Inf。您是否尝试将限制设置为c(NA, 100000)?此外,您应该为限制分配一个两个长度的向量(不是limits = 0, 100000)。

标签: r ggplot2


【解决方案1】:

所以有两个选项,@teunbrand 指示的第一个选项是将限制设置为 c(NA, 1000000)。它看起来像:

scale_x_continuous(trans = "log", breaks = c(0, 10, 100, 1000, 10000, 100000), limits = c(NA, 100000))

检查括号和“c”。

第二个选项是从 scale_x_continuous 中删除限制,而是使用

xlim()

它看起来像:

scale_x_continuous(trans = "log", breaks = c(0, 10, 100, 1000, 10000, 100000)) +
xlim(0, 100000)

【讨论】:

  • @teunbrand 感谢您的编辑!当涉及到代码时,从移动设备中脱颖而出是一个真正的挑战
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 2013-02-21
  • 2013-01-09
  • 2021-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多