0

I have been working with R for a few months now and am finally ready to start publishing some of my work. The only issue is that much of my analysis centers around presenting visualizations I generated with ggplot2 and mapview.

Using Rstudio, I choose the "knit to html" button, but notice that none of my visualizations actually appear in the html document.

I have a feeling this has to do with my YAML header, but I am having a difficult time finding a straight answer on what I need to add in order to automatically generate my plots.

My header is as follows:

title: "###"
author: "###"
date: "###"
output:
  html_document:
    toc: yes
    toc_depth: 2
    toc_float: yes
  pdf_document:
    toc: yes
    toc_depth: '2'
knitr::opts_chunk$set(echo = TRUE)

Here's an example of the code chunk in markdown where I would like the visualization to appear directly below the code:

viz_trips_by_weekday <- ggplot(data = trips_by_weekday)+
  geom_col(mapping = aes(x = weekday,
                         y = ride_count,
                         fill = customer_type),
           position = 'dodge')+
  labs(title = "Annual Ride Count By Weekday", 
       x = "Day of Week", 
       y = "Number of Rides",
       )
       
print(viz_trips_by_weekday)

Any help would be greatly appreciated!

  • 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. It seems unlikely to be due to the header. Are you setting other chunk options? How exactly are your chunks formatted? – MrFlick Jan 12 '23 at 19:19
  • Assuming you are using RStudio, if you just create a new markdown document it will provide a sample document. If you build that to HTML, do the images appear? – MrFlick Jan 12 '23 at 19:21
  • Thank you both for your quick replies. I created a new markdown file and yes, the plots appear as they should. My question is this: how do I indicate to rstudio that I want a plot to be shown? Does it have to do with the information inside the {} brackets? A better understanding of the syntax required would be very helpful. – Pat Tarantino Jan 12 '23 at 19:28
  • There are many code chunk options you can provide that will change what's rendered in the document. Common ones are `echo` and `eval` but see a complete list here: https://bookdown.org/yihui/rmarkdown/r-code.html – MrFlick Jan 12 '23 at 19:35
  • I dont think you need to use the print, to output in html, you can just use viz_trips_by_weekday instead of print(viz_trips_by_weekday). also check the chunk options if you are suppressing the output. – jkatam Jan 12 '23 at 19:44
  • Now we're getting somewhere. Using the example above (viz_trips_by_weekday), what should I be putting into the code chunk to display my visualization? ```{r _______} viz_trips_by_weekday <- ggplot(data = trips_by_weekday)+ geom_col(mapping = aes(x = weekday, y = ride_count, fill = customer_type), position = 'dodge')+ labs(title = "Annual Ride Count By Weekday", x = "Day of Week", y = "Number of Rides", ) ``` – Pat Tarantino Jan 12 '23 at 19:50

2 Answers2

0

check these options in the code chunk

enter image description here

jkatam
  • 2,691
  • 1
  • 4
  • 12
  • I can now see the visualization in markdown, but now I cannot knit. Instead, I get an error message: "Error in ggplot(data = trips_by_weekday) : could not find function "ggplot" – Pat Tarantino Jan 12 '23 at 20:00
  • i think you forgot to call the library(ggplot2) before – jkatam Jan 12 '23 at 20:02
0

library(ggplot2)

Create a chunk in the beginning of your document that holds all the libraries you are using in the code. Like here, where you use ggplot2 package.

Amandine
  • 13
  • 5