Ggplots are not displayed after knitring to html/word/etc.

Hi everyone,

first up, it is my first coding assignment and I am already desperate. I downloaded the latest version of Rstudio for Mac 3 days ago and I am now trying to submit an assignment in word format that displays graphs about life expectancies. When I run the code in Rstudio, the plot is shown correctly. However, once I knitr it to html or other formats, it only shows the code, not the graphs. Below, you can see what I did.

---
title: "assignment"
author: "mimler"
date: "23.07.2019"
output:
  html_document:
    highlight: zenburn
    theme: flatly
    toc: yes
    toc_float: yes
  pdf_document:
    toc: yes
  word_document:
    toc: yes
---


```{r load-libraries, warning=FALSE, message=FALSE, echo=FALSE}
library(tidyverse)  # Load ggplot2, dplyr, and all the other tidyverse packages
library(gapminder)  # gapminder dataset
library(dplyr)
library(ggplot2)
```

```{r}
country_data <- gapminder %>% 
            filter(country == "Germany") # I come from Germany.

continent_data <- gapminder %>% 
            filter(continent == "Europe")
```

```{r, lifeExp_one_country}
plot1 <- ggplot(country_data,aes(x = year, y = lifeExp))+
   geom_point() +
   geom_smooth(se = FALSE) +

print(plot1)
```

This is just copied out of my Rstudio session. Most of it was already given and I just had to put in the aesthetics etc. I tried to find an answer online, and sometimes I thought I found the correct thread, but then technical terms made it impossible for me to understand, as I started R just 3 days ago. So, I would be very greatful for an easy explanation. If you require any further information, I will try to provide it.
Thanks.

Hi,

The forum's homework policy encourages us to not simply give you fixed code, but help you get there yourself:

In your case, I'd first see if this is an issues with RMarkdown in general, or with your code. If you create a new markdown and use the default sample that's given, can you generate the plots once you knit it? If not, there's an issue with R-studio/R, if you can, it's your code.

In your code, look at the plot1 line. If you run it as you put it above, I get an error:

Error: Don't know how to add print(plot1) to a plot

The error message pretty much gives you the fix for the issue, but I doubt this was your original issue as you wouldn't have been able to plot it locally in R with that error either. If I fix it, my R-markdown works.

One last tip: Click on the options button on the top right of your code chunk to check if you are rendering both code and output

PJ

1 Like

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