need help with ggplot

ggplot(mydata, aes(year, mydata$crop, mydata$pesticides = emis_pest) +
stat_summary(geom = 'line') +
geom_vline(xintercept = 2012) +
theme_minimal()

can any one help with the error

Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?
Run rlang::last_error() to see where the error occurred.

rlang::last_error()

This is not valid syntax, can you explain what are you trying to accomplish with this code? Ideally,
Can you provide a proper REPRoducible EXample (reprex) illustrating your issue?

This is a screen print of my data,

head(mydata)
countryregion c_id locality year tessac crop
1 Ghana 0 Doggoh 0 2012 Before Millet
2 Ghana 0 Doggoh 0 2012 Before Millet
3 Ghana 0 Doggoh 0 2012 Before Millet
4 Ghana 0 Doggoh 0 2012 Before Millet
5 Ghana 0 Doggoh 0 2012 Before Millet
6 Ghana 0 Doggoh 0 2012 Before Millet
emissionskgco2eqha1 pesticides crmburned crmincorporated crmremoved crmexported inorganicf
1 323 0 0 25 0 0 0
2 331 0 100 0 0 0 0
3 326 0 0 100 0 0 0
4 330 0 100 0 0 0 0
5 0 0 0 0 0 0 0
6 502 0 0 0 100 0 0

Here, i want to run a difference in Difference estimator,
mydata %>%
mutate(after_2012 = ifelse(year > mean(mydata$year), "yes", "No")) %>%
treated<- cbind(crop)
y<- cbind(emissionskgco2eqha1)
x<- cbind(pesticides, crmburned, crmexported, crmincorporated, crmremoved, inorganicf, organicf)
so i defined the treated and control groups as well as the dependent and independent variables. In the ensuing code, i estimated the impact with the following lines of argument
emis1 <- lm(y~ xyear, data=mydata) #management practices on emissions.
emis3 <- lm(y~ crop
year, data=mydata)# for crop specific effect on emissions

this is my coefficient plot for both effects,
plot_summs(emis1, scale=TRUE)
plot_summs(emis3, scale=TRUE).

Now i want to plot the overall effect for the DiD estimator and I came across this code that i adapted and hence the error. I will be glad for an assistance with my lines of codes.

mydata$post12 <- as.numeric(mydata$year >= 2012)
ggplot(mydata, aes(year, crop, pesticides = emis_pest) +
stat_summary(geom = 'line') +
geom_vline(xintercept = 2012) +
theme_minimal()

Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?
Run rlang::last_error() to see where the error occurred.

this is the error and i will kindly request help with my lines of code.
Thank you

Sorry but what you have posted is still not clear nor reproducible. Please read the link I gave you before and try to make a proper reproducible example.

About this line of code

You are missing a closing parentheses and more importantly, there is no pesticides aesthetic, what do you expect this part to do?

Library(ggplot2)

dput(head(mydata, 10)[c("year", "crop", "emissionskgco2eqha1", "pesticides", "crmburned", "crmincorporated", "crmremoved", "crmexported", "inorganicf", "organicf")])
structure(list(year = c(2012, 2012, 2012, 2012, 2012, 2012, 2012,
2012, 2012, 2012), crop = c("Millet", "Millet", "Millet", "Millet",
"Millet", "Millet", "Millet", "Millet", "Millet", "Millet"),
emissionskgco2eqha1 = c(323, 331, 326, 330, 0, 502, 311,
324, 490, 342), pesticides = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0), crmburned = c(0, 100, 0, 100, 0, 0, 0, 0, 0, 0), crmincorporated = c(25,
0, 100, 0, 0, 0, 30, 20, 0, 100), crmremoved = c(0, 0, 0,
0, 0, 100, 0, 0, 70, 0), crmexported = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0), inorganicf = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
organicf = c(0, 0, 2, 3, 0, 2, 4, 5, 7, 5)), row.names = c(NA,
10L), class = c("tbl_df", "tbl", "data.frame"))

model <- lm(y ~ crop, data = mydata)
emis_organ <-emissionskgco2eqha1*organicf
mydata$post12 <- as.numeric(mydata$year >= 2012)
ggplot(mydata, aes(year, crop, organicf = emis_organic) +
stat_summary(geom = 'line') +
geom_vline(xintercept = 2012) +
theme_minimal()

reprex::reprex() # unfortunately my reprex argument does not seem to work.
So I have attached a copy of the data with the dput function and the lines of my code for execution.

i want to plot a Difference in Difference estimator plot.

Your code is not reproducible and it is still not clear what you want to achieve.

In this line the aes() function, works for mapping your variables to aesthetic elements, for example, if I make this explicit it would be

aes(x = year, y = crop, organicf = emis_organic)

Here you are mapping the year variable to the x-axis and the crop variable to the y-axis, but there is no aesthetic element called organicf that you can map a variable to, and it is not clear, How do you expect to this variable to be graphically represented?

I think you need to work in your basic ggplot2 skills, so it would be better if you start by reading this

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