Adding point to ggplot

Hey everyone,

I was wondering how it is possible to add values into an existing plot. The following code plots a model I estimated.

modelplot(model, coef_omit = "^(?!.*xyz)") +
  geom_vline(xintercept = 0, linetype = "dashed", color = "red") +
  scale_y_discrete(labels = c(seq(1900, 1904, by = 1), seq(1906, 1910, by = 1))) 

Now I would like to add a point at x = 0, y = 5. If I use

  geom_point(aes(x = 0, y = 1905), color = "black")

the point is added in the very beginning of the graph at y = 1900 and the other values shift back.

How do I add the point in the middle of my graph at exactly the values I want it at?

The problem arises as I estimated a model where the year 1905 is the treatment year and thus normalized to 0. The coefficient is, therefore, also 0. There is certainly a better way of estimating the model such that the value for 1905 is included in the model, but in my current solution I have to add the point manually...

Thank you and all the best!

generally speaking , to plot different data points than you previously plotted with ggplot its as easy as providing a new data argument to a following geom.
Here is an example


library(ggplot2)


df_1 <- data.frame(x=1:20,
           y=1:20)

ggplot(data=df_1) +
  aes(x=x,y=y) + 
  geom_line() + # next add a datapoint by passing new data
  geom_point(data=data.frame(x=10,
                             y=12),
             color="red")

Thank you for your response!

I get the error: Error in check_required_aesthetics():
! geom_point requires the following missing aesthetics: x and y

modelplot(model,
          coef_omit = "^(?!.*xyz)") +
  geom_vline(xintercept = 0, linetype = "dashed", color = "red") +
  scale_y_discrete(labels = c(seq(1900, 1904, by = 1), seq(1906, 1910, by = 1))) +
  geom_point(data=data.frame(x=0, y=1905),
             color="red")

What is going wrong?

In the meantime I tried to achieve the desired result through a different channel: I created a df and loaded the coefficients as well as the confidence intervals in. I added the missing year with the coefficient 0 and conf. int. 0.

Now I can plot the values perfectly, but the error bars appear to all be the same size... Can someone explain to me why that is?

ggplot(data = coef_df,
       aes(x = coefficient_name, 
           y = coefficient)) +
  geom_point() +
  geom_errorbar(
    aes(ymin = ci_lower_bound, 
        ymax = ci_upper_bound), 
    width = 0.2, 
    color = "blue") +
  theme_minimal() +
  geom_hline(yintercept = 0, 
             linetype = "dashed", 
             color = "red")

Thank you in advance!!

I dont know what modelplot is , or how it constructs a ggplot. I assumed it would set x and y aes() , but maybe it doesnt; or maybe it does and then unsets them. if it doesnt it might be up to you to set it.

aes(x=x,y=y) 

In general its hard to answer technical questions about what code should be on data we don't have. Reprex is best practice in question answering as it reduces friction in finding solutions.

Thank you for your help!

My other approach has worked. The issue lies in the standard errors themselves... I will now try to figure out why they are calculated falsely and will most likely be back with another post...

Thank you again!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.