6

I am currently plotting data using the ggpubr package in R (based on ggplot2). When I plot the means of two conditions including standard errors, the y-axis should be limited from 1 to 7, which I indicate using:

p <- ggline(data, x = "condition", y = "measure", 
            add = c("mean_se"), 
            ylab = "Measure")
ggpar(y, ylim = c(1, 7), ticks=T, yticks.by = 1)

In the final plot, however, the y-axis shows only values from 1 to 6 see graph attached

I tried to plot the same data using native ggplot2, but the problem persists, once I change the layout. For ggplot2 I used:

p <- ggplot(data, aes(x=condition, y=measure)) + 
geom_line() +
geom_point()+
geom_errorbar(aes(ymin=measure-se, ymax=measure+se), width=.2, position=position_dodge(0.05)) +
ylab("measure") +
xlab("Condition")
p + scale_y_continuous(name="measure", limits=c(1, 7), breaks=c(1:7))
p + theme_classic()

It would be great if someone could help me with this issue.

Edit: as suggested in the comments, here is the data I am trying to plot using ggplot2:

structure(list(condition = structure(3:4, .Label = c("IC", "SC", 
"ILC", "SLC"), class = "factor"), measure = c(4.10233918128655, 3.83040935672515
), se = c(0.235026318386523, 0.216811675834834)), class = "data.frame", row.names = c(NA, 
-2L))
statleo
  • 81
  • 1
  • 7
  • Changing `breaks=c(1:7)` to `breaks = seq(1,7, by = 1)` should work – aaumai Jul 16 '19 at 20:46
  • Thanks for your reply, it works until I change the theme to classic, then it stops at 6 again. – statleo Jul 17 '19 at 06:11
  • Please include some data in your questions so others can reproduce your problem. Use `dput(some_data)` to make the data into an easily read format. – Richard Telford Jul 17 '19 at 06:30
  • 1
    @aaumai: how is *that* supposed to work?! The only difference is that `seq` returns a numeric, and not integer vector, but that will not matter for ggpar. – January Jul 17 '19 at 07:02
  • Instead of `ggpar`, use the ggplot2 function `ylim`: `p + ylim(1, 7)` – January Jul 17 '19 at 07:06
  • I added some example data as suggested by @RichardTelford. It is the data I enter in the ggplot function, not the one for ggline, as ggline automatically calculates standard errors. Thanks fot your help! @ January, using ylim still does not fix the issue for me – statleo Jul 17 '19 at 08:13
  • 1
    Actually, you did everything 99% correct, except for adding `theme_classic` and `scale_y_continuous` *at the same time* to the plot. – January Jul 17 '19 at 09:11

2 Answers2

3

The solution is much more trivial. You were doing everything right! Except for one clerical error. Here is what was happening:

First, you generate your initial plot, fine.

p <- ggplot(data, aes(x=condition, y=measure)) + 
geom_line() + geom_point() +
geom_errorbar(aes(ymin=measure-se, ymax=measure+se), 
     width=.2, position=position_dodge(0.05)) + 
ylab("measure") +
xlab("Condition")

This plot does not have the limits. When you add the limits and display it, the scales are correct:

p + scale_y_continuous(name="measure", limits=c(1, 7), breaks=c(1:7))

However, note that p did not change! You did not store the result of adding the limits to p. Therefore, p is still without the scale_y_continuous. No wonder then that when you type

p + theme_classic()

...the limits are gone. However, if you try

p <- p + scale_y_continuous(name="measure", limits=c(1, 7), breaks=c(1:7))
p + theme_classic()

everything will be correct.

January
  • 16,320
  • 6
  • 52
  • 74
2

I think I got something resembling your plot with correct y-axes with the following code:

ggplot(data, aes(x = condition, y = measure)) + 
  geom_point() +
  geom_errorbar(aes(ymin = measure-se, ymax = measure+se), 
                width = .2, position = position_dodge(0.05)) +
  # Group prevents geom_line interpreting each x-axis point as it's own group
  geom_line(aes(group = rep(1, nrow(data)))) +
  xlab("Condition") + 
  # Expand is optional, it prevents padding beyond 1 and 7
  scale_y_continuous(name = "measure", 
                     limits = c(1, 7), 
                     breaks = 1:7, 
                     expand = c(0,0)) +
  theme_classic()

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • 1
    that looks perfect, thank you so much! I am struggling, however, to identify the crucial edit in your code compared to mine. Could you please give me a short feedback, what you think the issue is? – statleo Jul 17 '19 at 08:56
  • I think your `ylab()` argument overruled the `scale_y_continuous()`, but I'm not 100% sure. – teunbrand Jul 17 '19 at 09:09
  • There is no difference. Look at my answer. You made a simple clerical error – you forgot to store the result of adding the scales again in p, so when you called `p + theme_classic()` the p still did not have the limits set. – January Jul 17 '19 at 09:09
  • Or indeed what January says, that the plot was not assigned to the variable when you added the `scale_y_continuous()` – teunbrand Jul 17 '19 at 09:11