0

I'm using R Markdown to write a report and using this code to create a graph:

ggplot(dd, aes(x =timestamp, y =eventType)) + 
geom_line()

I am getting the error message below:

Error in ggplot(dd, aes(x = timestamp, y = eventType)): could not find function "ggplot"

I have tried to specify the library(ggplot2) but that didn't help. Can anybody suggest anything? If I run the r code on its own it works fine. If I tried to knit as pdf it doesn't work. Thanks

user5576922
  • 103
  • 1
  • 1
  • 7
  • 2
    Where exactly did you put the `library(ggplot2)` command? Do you have it in the markdown document somewhere? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Jan 07 '19 at 19:51
  • I have tried to add the library(ggplot2) just above the code. Also, I have another r script file where I run my r code before adding to the r markdown file. How do i copy a sample of my data into the reply? The link you have provided doesn't show this – user5576922 Jan 07 '19 at 20:16

1 Answers1

0

Have you tried running this code

     ggplot2::ggplot(dd, ggplot2::aes(x =timestamp, y =eventType)) + 
     ggplot2::geom_line()
Sharon M
  • 194
  • 5
  • 15
  • Thanks! I am still getting an error message: Error in ggplot2::ggplot(dd, ggplot2::aes(x = timestamp, y = eventType)): object 'dd' not found – user5576922 Jan 07 '19 at 21:57
  • @user5576922 try it without ggplot2::aes just aes. This worked for me when I had a similiar issue – Sharon M Jan 08 '19 at 00:03
  • Sorry, this still doesn't work. I am using ProjectTemplate does the location of the rmd file anything to do with this? The error suggests that the dd (data file cannot be found) I have tried inserting the code for the dd in rmd but thta didn't work either. – user5576922 Jan 08 '19 at 08:15
  • I found that my r markdown file was saved in the incorrect directory. It should have been in the folder called Reports in my projectTemplate directory. Also, I should have included this code in my r markdown: #knitr::opts_knit$set(root.dir= normalizePath('..')) – user5576922 Jan 08 '19 at 11:54