'ggpaired() and ggpar() axis limits not working
I'm having trouble changing the y-axis limits and tick mark spacing on a ggpaired() plot. Below is a reproducible example, where the cood_cartesian(ylim=c(), expand=TRUE) command doesn't seem to be doing anything. I get the same result regardless of whether I use coord_cartesian(). If I try putting ylim=c() in the ggpar() function, I also get the same result.
reprex <- data.frame(c(9, 14, 17, 5, 1, 8, 12), c(17, 21, 20, 9, 4, 9, 10), sample(100:1000, size=7, replace=FALSE))
colnames(reprex) <- c("SDQ.tot.W1", "SDQ.tot.W2", "id")
set.seed(321)
reprex$SDQ.tot.W1j <- jitter(reprex$SDQ.tot.W1, amount=.1)
reprex$SDQ.tot.W2j <- jitter(reprex$SDQ.tot.W2, amount=.1)
SDQ.plot.data <- data.frame(Baseline = reprex$SDQ.tot.W1j, `One year later` = reprex$SDQ.tot.W2j, id=reprex$id)
SDQ.paired.j <- ggpaired(SDQ.plot.data, cond1 = "Baseline", cond2 = "One.year.later", id="id",
fill = "condition", palette = "Blues", coord_cartesian(ylim=c(0,40), expand=TRUE))
SDQ.paired.j <- ggpar(SDQ.paired.j, xlab= "Wave", ylab = "SDQ score (parent-report)", panel.labs = c("Baseline", "1 year later"), yticks.by = 5, legend="none")
SDQ.paired.j
Here's an image of the plot I get from this code. Based on the code, I expect the y-axis to display a range from 0-40.
Solution 1:[1]
Instead of passing coord_cartesian as an argument, "add" it to your final plot:
reprex <- data.frame(c(9, 14, 17, 5, 1, 8, 12), c(17, 21, 20, 9, 4, 9, 10), sample(100:1000, size = 7, replace = FALSE))
colnames(reprex) <- c("SDQ.tot.W1", "SDQ.tot.W2", "id")
set.seed(321)
library(ggpubr)
#> Loading required package: ggplot2
reprex$SDQ.tot.W1j <- jitter(reprex$SDQ.tot.W1, amount = .1)
reprex$SDQ.tot.W2j <- jitter(reprex$SDQ.tot.W2, amount = .1)
SDQ.plot.data <- data.frame(Baseline = reprex$SDQ.tot.W1j, `One year later` = reprex$SDQ.tot.W2j, id = reprex$id)
SDQ.paired.j <- ggpaired(SDQ.plot.data,
cond1 = "Baseline", cond2 = "One.year.later", id = "id",
fill = "condition", palette = "Blues"
)
#> Warning: `gather_()` was deprecated in tidyr 1.2.0.
#> Please use `gather()` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
SDQ.paired.j <- ggpar(SDQ.paired.j, xlab = "Wave", ylab = "SDQ score (parent-report)", panel.labs = c("Baseline", "1 year later"), yticks.by = 5, legend = "none")
SDQ.paired.j + coord_cartesian(ylim = c(0, 40), expand = TRUE)

Or using ylim:
SDQ.paired.j + ylim(0, 40)
#> Scale for 'y' is already present. Adding another scale for 'y', which will
#> replace the existing scale.

Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | stefan |

