Geom_point in marginal_effects

Hi everyone,

Forgive me if I am missing something obvious, but I cannot figure out or find information regarding changing geom_point() values in marginal_effects plots. I can freely change the other parameters.

Quick example

dat <-tibble(x = rnorm(20, mean = 2, sd = 2),y = rnorm(20, mean = 10, sd = 2))
fit1 <- brm (x ~ y, data = dat)
pme <- marginal_effects(fit1) 
p1 <- plot(pme, plot = F, points = T)[[1]]
p1

image

change geom_line() color and size. Other usual ggplot parameters work just like geom_line in this example.

p2 <- plot(pme, plot = F, points = T)[[1]] + 
geom_line(color="black", size=1)
p2

image

attempt to change geom_point() color and size

p3 <- plot(pme, plot = F, points = T)[[1]] + 
geom_line(color="black", size=5) + 
geom_point(color = "red", size = 1)
p3

image

another approach

p4 <- plot(pme, plot = F, points = F)[[1]] + 
geom_line(color="black", size=5)  + 
geom_point(aes(x = x, y=y), color = "red", size = 1)
p4

image

I also tried geom_point(aes(data = dat, x=x, y=y)) and various other combinations

Any suggestions would be appreciated!
Again, sorry if this is just is simple user error or a lack of ggplot knowledge.

Please also provide the following information in addition to your question:

  • Operating System: osx 10.14.2
  • brms Version: 2.8.5

The marginal effect plot is actually working with two datasets, one controlling the regression line and one controlling the points.

When you create a ggplot, the initial dataframe is used as the default for all the layers in the plot. In this case, the dataframe containing the regression line provides the default dataset in the subsequent layers. That’s why your calls to geom_point() are only drawing the regression line data.

The solution would be to manually add the points yourself. geom_ functions in ggplot2 accept a data argument, so you can tell geom_point() to draw the data in your dat argument.

library(tidyverse)
library(brms)

data <- readr::read_rds("~/discourse.rds")
fit1 <- data$fit1
dat <- data$dat

pme <- marginal_effects(fit1) 
p1 <- plot(pme, plot = FALSE)[[1]]

p1

p1 + 
  geom_point(
    aes(x = y, y = x), 
    # this is the key!
    data = dat, 
    color = "orange",
    size = 4,
    # This tells it to ignore the ymin and ymax settings used elsewhere
    inherit.aes = FALSE
  )

Created on 2019-05-16 by the reprex package (v0.2.1)

6 Likes

Great! Thank you for the help.

The marginal effect plot is actually working with two datasets, one controlling the regression line and one controlling the points.

This was the part I couldn’t quite understand.