Why it says problem with ggplot2?

Hi everyone! Today me and my colleagues had some problem with ggplot. Actually we have to do an assignment and the answer says 'Make the scatter plots in ggplot2 as in the script on openness' and so we wrote on R '

p <- qplot(x = gravity.ita$ldistw, y = gravity.ita$lflow, data=gravity.ita, colour=gravity.ita$lgdp_d) + theme_bw() + labs(title="gravity.ita", x="log of distance", y="log of exports",color="lgdp_d")
p
p + stat_smooth(method = "gam", formula = y ~ s(x, k = 10), size = 1)
#6-7
p <- qplot(x = gravity.ita$lgdp_d, y = gravity.ita$lflow, data=gravity.ita, colour=1/gravity.ita$ldistw) + theme_bw() + labs(title="gravity.ita", x="log of GDP of importing country", y="log of exports",color="1/ldistw")
p
p + stat_smooth(method = "gam", formula = y ~ s(x, k = 10), size = 1)

but then, when we went to knit the all work with word, we have this message from R Markdown

'Quitting from lines 130-137 (Homework1.Annmarkdown.ultimo.Rmd)
Error in qplot(x = gravity.ita$ldistw, y = gravity.ita$lflow, data = gravity.ita, :
non trovo la funzione "qplot"
Calls: ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Esecuzione interrotta'

I already have this package.

What can we do to solve this problem?

Thank you!

Can you post a reproducible example of gravity.ita?

p <- qplot(x = gravity.ita$ldistw, y = gravity.ita$lflow, data=gravity.ita, colour=gravity.ita$lgdp_d) + theme_bw() + labs(title="gravity.ita", x="log of distance", y="log of exports",color="lgdp_d")

using ggplot2 would look like

p <- ggplot(gravity.ita,aes(x = ldistw, y = lflow)

to make a base plot, which can then be elaborated with other features.

library(ggplot2)

p <- ggplot(mtcars,aes(x = mpg, y = hp))
p + geom_point(color = "red") + 
  ggtitle("Scatterplot of hp against mpg in mtcars data") +
  theme_bw()

Are you sure that you hase

library(ggplot2)

in the .Rmd file?

I don't have it I've just check it! Thank you so much! How can I do to add it to my files? I have already done quite all my job, is there a way to add it on my file without start again from the beginning?

If you have a

knitr::opts_chunk$set(echo = TRUE)

section, I would just put it there.

knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)

Personal preference. You can put it in the ggplot chunk if you want.

It should make a minor change to the file.

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.