【问题标题】:Discrete x-axis labels beginning at far left of x axis in ggplot2 [duplicate]从ggplot2中x轴的最左侧开始的离散x轴标签[重复]
【发布时间】:2016-06-05 12:23:00
【问题描述】:

这个问题之前以不同的形式提出过,指的是连续变量,而不是离散变量。因此,我认为一个新问题是合理的。

我有一个 ggplot 折线图,x 轴为时间点,y 轴为分数。

Dose <- rep(c("Time1", "Time2", "Time3", "Time4", "Time5"), 4)
ID <- rep(1:4, each = 5)
set.seed(123)
Score <- rnorm(20, 10, 3)
df <- data.frame(ID, Dose, Score)

p <- ggplot(df, aes(df$Dose, df$Score, group = df$ID, colour = df$ID)) + geom_smooth(method = lm, se = FALSE) + stat_smooth(aes(group = 1), se = FALSE, colour = "red", size = 1)

p

我希望第一个 x 轴标签/刻度出现在 x 轴和 y 轴的连接处。我希望这样做,因为我希望 OLS 拦截真正拦截 y 轴。所以在这种情况下,时间 1 将出现在 x 和 y 轴上 0 的位置。我如何做到这一点?

【问题讨论】:

标签: r ggplot2


【解决方案1】:

如果您使用scale_x_discrete(expand = c(0, 0)),您将按时切断您的标签 5. 使用scale_x_discrete(expand = c(0, 0.05)) 给自己一点空间我还重新格式化了您的代码以清理它。

p <-ggplot(df, aes(Dose, Score, group = ID, colour = ID)) + 
  geom_smooth(method = lm, se = FALSE) + 
  stat_smooth(aes(group = 1), se = FALSE, colour = "red", size = 1) +
  theme(
    legend.position = 'none'
  ) +
  scale_x_discrete(expand = c(0,.05))

p

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多