Error in ggplot (Error in FUN(X[[i]], ...) : object 'Probability' not found

Hi there,
first time posting so I hope i am doing this right. I am attempting to do a project for my university where i need to make a couple of plots. First one i managed to get but I keep getting the following error with the next one Error in ggplot (Error in FUN(X[[i]], ...) : object 'Probability' not found
It is very similar to the code i used the first time but I am baffled why this won't work. Not very experienced at all so if anyone has any hints could you break it down into simple terms please :sweat_smile: I tried googling the error but i can't understand the explanations.

ggplot(data=newdata6, 
       aes(x= EvCameron_NoNa, y= Probability, colour=Level)) +
  geom_line(size=1.5) +
  scale_x_continuous(breaks = c(1:10)) +
  labs (x= "Evaluation of David Cameron (1 = lowest opinion)") + facet_grid(. ~ rsex)

Does the data frame newdata6 have a column named Probability? You can check this by running the code

colnames(newdata6)
1 Like

Hi!
Thanks for the response. I checked and it doesn't have a column with Probability. Not sure if this will help but this is the code i had entered prior to there. Apologies but i am still in the novice stage so I appreciate the help.

newdata5 <- with(data, 
                   data.frame(EvCameron_NoNa =rep(seq(from = 1, to = 10,
                                                      length.out = 100), 4),
                              employmentdum = mean(employmentdum, na.rm = TRUE),
                              rsex_fac = factor(rep(1:2, each=100))))

head(newdata5)


newdata6 <- melt(newdata5,
                   id.vars = c("EvCameron_NoNa", "employmentdum", "rsex_fac"),
                  variable.name = "Level",
                  value.name = "Probability")

head(newdata6)

newdata6$rsex <- as.factor (mapvalues(newdata6$rsex_fac,
                                       from = c("1", "2"),
                                       to = c ("1. Male", "2. Female")))

It looks to me like the data frame newdata5 has three columns, EvCameron_NoNa, employmentdum, rsex_fac. Is that true? If so, your call to melt() to make newdata6 will just make a copy of newdata5. Is that what is happening?
Please describe the process you are trying to implement. It seems you start with a data frame named data. What do you want to do with that?

1 Like

I have got it! Once you mentioned the columns i traced back my steps from my first plot and realised i had missed some code. Thanks for sparing the time.
I hope this eventually gets easier with practice :slight_smile:

Thanks again!

I think the most powerful debugging technique is explaining to someone else what you are doing. It is amazing how often that results in seeing the code as if for the first time and the error is obvious.

1 Like

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