4

I am working with the sjPlot package. I am plotting the predicted values of a linear model similar to the following example:

#R Version: 3.6.2
set.seed(1993)

x <- seq(1980, 2018, 2)
y <- runif(length(x), 1,100)

df <- data.frame(x,y)

library(ggplot2)
library(sjPlot)

Results <- lm(y ~ as.factor(x), data = df); summary(Results)

plot_model(Results, type = "pred", terms = c("x"))

I want to rescale the x-axis so that instead of it proceeding by every two years, i want it to proceed by every 4 years.

However, when I try to rescale the x axis using scale_x_continuous or scale_x_discrete it doesn't work, even though it is the correct code in ggplot2

#This works in ggplot:
ggplot(df, aes(x= x, y=y))+
  geom_point() +
  scale_x_continuous("Cycle", labels = seq(1980, 2018, 4), breaks = seq(1980, 2018, 4))

#But it doesn't work in sjPlot package -- as either scale_x_continuous
plot_model(Results, type = "pred", terms = c("x")) +
  scale_x_continuous("Cycle", labels = seq(1980, 2018, 4), breaks = seq(1980, 2018, 4))
  
#or  either scale_x_discrete
plot_model(Results, type = "pred", terms = c("x")) +
  scale_x_discrete("Cycle", labels = seq(1980, 2018, 4), breaks = seq(1980, 2018, 4)

How can I rescale the x-axis to proceed by 4, instead of 2?

Update

  1. The only message that pops up after I run the plot code is as follows: Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.

  2. Here is my output for plot_model code. It is the same whether I use scale_x_continuous or scale_x_discrete.

enter image description here

Sharif Amlani
  • 1,138
  • 1
  • 11
  • 25
  • 3
    Hi OP. Your code works fine for me. I do get a warning that the 'x' scale is already present and it is being overwritten, but the output plot has breaks and labels separated by 4. Are you getting a specific error/warning that is different? What does your plot output look like? – chemdork123 Jul 30 '20 at 19:57
  • Hi! Thanks so much for your response. I updated the question to illustrate what my plot output looks like when I call `plot_model` from the `sjPlot` package. It removes the axis labels altogether – Sharif Amlani Jul 30 '20 at 20:03
  • I get the same result as you when you use `scale_x_discrete` [see here for some info on that](https://github.com/tidyverse/ggplot2/issues/1589); however, if I return back to `scale_x_continuous`, the scale is back and as you would expect. – chemdork123 Jul 30 '20 at 20:12
  • I'm not sure I'm following. `scale_x_continuous` works perfectly when I call it as a part of `ggplot` command. Its when I use the `sjPlot` function `plot_model` that it does not work. Maybe I should check for updates in the `sjPlot` package? – Sharif Amlani Jul 30 '20 at 20:25

1 Answers1

0

Just wanted to add to @chemdork123's answer that your code works fine for me as well.

#R Version: 3.6.2
set.seed(1993)

x <- seq(1980, 2018, 2)
y <- runif(length(x), 1,100)

df <- data.frame(x,y)

library(ggplot2)
library(sjPlot)

Results <- lm(y ~ as.factor(x), data = df); summary(Results)

plot_model(Results, type = "pred", terms = c("x"), show.data = TRUE) + 
  scale_x_continuous("Cycle", labels = seq(1980, 2018, 4), breaks = seq(1980, 2018, 4))
ggsave('sjplot_replicate.jpeg')

which gives me this plot:

enter image description here

So it seems like it's all good!

yuan-ning
  • 537
  • 1
  • 4
  • 12