0

This question has been asked in a different form before, referring to continuous variables, not discrete. Hence I think a new question was justified.

I have a ggplot line graph with time point on the x-axis and Score on the y-axis.

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

I would like the first x-axis label/tick to appear at the nexus of the x- and y-axes. I wish this because I would like the OLS intercept to really intercept the y axis. So in this case Time 1 will appear where 0 would be on the x and y axis. How do I achieve this?

2

1 Answer 1

4

If you do scale_x_discrete(expand = c(0, 0)) you will cut off your label on time 5. Give yourself a little room by use scale_x_discrete(expand = c(0, 0.05)) I also reformatted your code a bit to clean it up.

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
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.