1

I would like to make a similar plot as the following one: image 1

I used the following code for this plot (name model = model_11b):

 plot_model(model_11b, type =  "int", title = "Sedentary behaviour - Domains: Pre-Corona versus Lockdown",legend.title = "Domains",
           legend.labels = c("a","b","b")) + 
  geom_line(linetype = "dashed") +
  scale_color_discrete(labels = c("Leisure-time", "Work", "Transport"))+ 
  labs(y= "Sedentary behaviour - Domains", x = "Measurement moment") +
  scale_x_continuous(labels= c("Pre-Corona","Lockdown"), breaks = c(1,2))

INFO: The above model was made based on this dataset: dataset1

Model_11b has the following code:

model_11b <- glmer(SG_domeinen~1 + Meetmoment + SG_type_domein + Meetmoment:SG_type_domein + (1|T3_Nummer), data = dataset, family = Gamma(link = "log"), nAGQ = 2, control=glmerControl(optimizer="bobyqa",optCtrl=list(maxfun=100000) ))

And the following packages were installed: library(lme4) library(lmerTest) library(haven) library(ggplot2) library(sjPlot)

The new plot I would like to make is a plot based on the following code: library(lme4) library(lmerTest) library(haven)

DATASET:

    structure(list(T3_Nummer = structure(c(1, 1, 3, 3, 5, 5, 7, 7, 
9, 9, 11, 11, 14, 14), format.spss = "F5.0"), Meetmoment = structure(c(1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("1", 
"2"), class = "factor"), sedentair_gedrag_totaal_perweek = structure(c(1972.5, 
5557.5, 2670, 5310, 3660, 4515, 3840, 4972.5, 1162.5, 3997.5, 
4755, 3682.5, 2932.5, 3075), format.spss = "F8.2", display_width = 34L), 
sedentair_gedrag_totaal_perweek_ud = structure(c(4.69642857142857, 
    13.2321428571429, 6.35714285714286, 12.6428571428571, 8.71428571428571, 
    10.75, 9.14285714285714, 11.8392857142857, 2.76785714285714, 
    9.51785714285714, 11.3214285714286, 8.76785714285714, 6.98214285714286, 
    7.32142857142857), format.spss = "F8.2", display_width = 34L), 
    total_SG = structure(c(4.69652857142857, 13.2322428571429, 
    6.35724285714286, 12.6429571428571, 8.71438571428571, 10.7501, 
    9.14295714285714, 11.8393857142857, 2.76795714285714, 9.51795714285714, 
    11.3215285714286, 8.76795714285714, 6.98224285714286, 7.32152857142857
    ), format.spss = "F8.2", display_width = 34L)), row.names = c(NA, 
-14L), class = c("tbl_df", "tbl", "data.frame"))
Dataset2$Meetmoment <- factor(Dataset2$Meetmoment)

Dataset2$sedentair_gedrag_totaal_perweek_ud <- Dataset2$sedentair_gedrag_totaal_perweek/(7*60) 

Dataset2$total_SG <- Dataset2$sedentair_gedrag_totaal_perweek_ud + 0.0001

model_20 <- glmer(total_SG~1 + Meetmoment + (1|T3_Nummer), data = Dataset2, family = Gamma(link = "log"))

I tried to make a plot of this model (library(ggplot2) & library(sjPlot) with the following code, but I don't get the plot I was looking for.

    plot_model(model_20, type = "est", title = "Total sedentary behaviour: precorona versus lockdown")+ 
  geom_line(linetype = "dashed") +
  labs(y= "Total sedentary behaviour",x = "Measurement moment")

image 2

Can someone help me further with this please?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Hi there and welcome to stack overflow. This question is hard to answer without more details. Would you consider making a minimal reproducible example? This means including a dummy dataset that has the same structure (column names, variable types) as your data, include how to build `model_20` and list all packages necessary to execute the code? You can include a dataset by copying the output of `dput(dummy_data)`. – teunbrand Feb 26 '21 at 11:37
  • @teunbrand: I edited my question with more details. Hopefully it's a bit clearer now... I don't understand how I can add a dataset? – Yanni Verhavert Feb 26 '21 at 12:15
  • [This post](https://stackoverflow.com/a/5963610/11374827) describes very well how to make questions reproducible. But briefly, you input `dput(my_dataset)` in the R console and R will print output in the console that you could copy to your question. We can then copy-paste your dput-output and we'll have the same data structure as you put into `dput()`. – teunbrand Feb 26 '21 at 12:42
  • 1
    @teunbrand Thanks! I just added this output. – Yanni Verhavert Feb 26 '21 at 13:09

1 Answers1

0

Maybe I'm not understanding entirely what you would want your plot to look like, since there are no obvious groups in the model as far as I can tell. However, if you just want to make a plot of the data (no model necessary), you could use the following:

ggplot(Dataset2, aes(Meetmoment, total_SG)) +
  geom_point() +
  geom_line(aes(group = T3_Nummer),
            linetype = 2)

enter image description here

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • In fact I would like the same kind of plot as "image 1" in my question, but instead of the different lines in this plot each representing a different domain, I need to have just one line which represents total sedentary behaviour. But I used a gamma-model with log function for this, as the distribution of my total sedentary behaviour wasn't okay. So that's why I would like to plot this model and not just the average of the variable 'total sedentary behaviour'. So on the y-axis I need to have total sedentary behaviour (the estimated values based on the gamma-model) and on the x-axis the – Yanni Verhavert Feb 26 '21 at 15:01
  • 2 measurement moments 'pre' and 'post' (= the 2 groups). – Yanni Verhavert Feb 26 '21 at 15:01