Cannot display graphs with knitr to pdf

This is how I am using knitr to display a plot. I get the message "df_max cannot be found" so it seems that df_max is not globally used by the knitr code. Could you help with this?

# Initialize device
dev.new(width =20000, height = 10000, unit = "px")

# Plot the event types that cause the largest number of injuries across all the states in the USA. Include the summary statistics table on the graph
p1 <- ggplot(df_max, aes(EVTYPE, INJURIES)) + geom_point(color = "blue") + theme(legend.position="top") + 
 		  theme(axis.text.x = element_text(color = "grey20", size = 7.0, angle = 90, hjust = .5, vjust = .5, face = "plain"),     
   		  axis.text.y = element_text(color = "grey20", size = 5, angle = 50, hjust = 0.5, vjust = 0, face = "plain")) +
              ggtitle("Health Inpact - INJURIES - of Severe Storms Across the USA") +
		  theme(plot.title = element_text(size = 10, face = "bold")) +
		  xlab("STORM EVENT TYPE") +
		  theme(axis.title.y = element_text(size = rel(0.5), angle = 90), axis.title=element_text(size=14,face="bold")) +
    		  theme(axis.title.x = element_text(size = rel(0.5), angle = 0), axis.title=element_text(size=14,face="bold")) +
              annotation_custom(tableGrob(round(stats, digits = 3), theme=ttheme_minimal(base_size = 7)))


# Plot the events causing the maximum amount of property damage across cost1a states. Include the summary statistics table on the graph.
p2 <- ggplot(cost1a_max, aes(EVTYPE, PROPDMG)) + geom_point(color = "blue") + theme(legend.position="top") + 
 		  theme(axis.text.x = element_text(color = "grey20", size = 7.0, angle = 90, hjust = .5, vjust = .5, face = "plain"),     
  		  axis.text.y = element_text(color = "grey20", size = 5, angle = 50, hjust = 0.5, vjust = 0, face = "plain")) +
              ggtitle("Economic Inpact - PROPERTY DAMAGE - of Severe Storms across the states of GU, TX, AK, TN, CA, KY, VI, IL, WI,MS, UT, CO, ND") +
		  theme(plot.title = element_text(size = 10, face = "bold"), axis.title=element_text(size=14,face="bold")) +
		  xlab("STORM EVENT TYPE") +
              theme(axis.title.y = element_text(size = rel(0.5), angle = 90), axis.title=element_text(size = 14, face = "bold")) +
		  theme(axis.title.x = element_text(size = rel(0.5), angle = 0), axis.title=element_text(size=14,face="bold")) +
		  annotation_custom(tableGrob(round(statsa, digits = 3), theme=ttheme_minimal(base_size = 7))) 

Plot 2 graphs on the same panel

grid.arrange(p1, p2)

When you knit an Rmd document, the process is done in a fresh session, so you are correct in saying that df_max would not be found.

You therefore need to include a code chunk within your Rmd file that imports the data used within the document.

Thank you. What Knitr chunk wouldI use to import the
data withing the document?

You could use one single chunk to do all your data import, after your chunk where you load all the packages you'll be using. For example:

```{r setup, include = FALSE}
library(dplyr)
library(readr)
library(ggplot2)
```

```{r data-import, include = FALSE}
data_max <- # whatever you do to import data_max

cost1a_max  <- # whatever you do to import cost1a_max
```

The rest of your document here

So, let's say that i read data from a file, ex; data <- read.table.................
after that, I can write

the ```{r data_import,.........}
data_max <- data
cost1_max <- data

Is that how is done?

Correct, so it might look like

```{r data-import, include = FALSE} 
data_max <- read.table(# your arguments here)
cost1a_max <- read.table(# your arguments here)
```

Yes, that works, now the graph can access the data.
I have another problem. I have to display 3 panels with different data for each graph in the panel - please see below - and I only get to display the first one. Also the first panel is repeated. How should I do this? I guess the question is: Is there any thing I can do to overwrite file.png. If I use different file names in each chung, ie; file.png, file1.png, file2.png the program halts with an error.

file1.png cannot be found. This is very strange becuause each graph invokes a different data frame.

dev.new(width =20000, height = 10000, unit = "px")

 knitr::include_graphics("C:/Users/giuse/OneDrive/Documents/DataScience/R SCRIPTS/STORM DATA/file.png", auto_pdf = getOption("knitr.graphics.auto_pdf", FALSE),dpi = NULL)

Here are there are 3 ggplot panels:
p1 <- ggplot(...........)

p2 <- ggplot(............)

arrange(p1,p2)

dev.new(width =20000, height = 10000, unit = "px")

 knitr::include_graphics("C:/Users/giuse/OneDrive/Documents/DataScience/R SCRIPTS/STORM DATA/file.png", auto_pdf = getOption("knitr.graphics.auto_pdf", FALSE),dpi = NULL)

p3 <- ggplot(........)
p4 <- ggplot(........)
arrange(p2,p3)

dev.new(width =20000, height = 10000, unit = "px")

 knitr::include_graphics("C:/Users/giuse/OneDrive/Documents/DataScience/R SCRIPTS/STORM DATA/file.png", auto_pdf = getOption("knitr.graphics.auto_pdf", FALSE),dpi = NULL)
p5 <- ggplot(.......)
p6 <- ggplot(.......)
arrange(p5,p6)

This is the solution with library(plotly)

Initialize device

dev.new(width =20000, height = 10000, unit = "px")

library(plotly)
# Plot the event types that cause the largest number of injuries across all the states in the USA. Include the summary statistics table on the graph
p1 <- ggplot(df_max, aes(EVTYPE, INJURIES)) + geom_point(color = "blue") + theme(legend.position="top") + 
 		  theme(axis.text.x = element_text(color = "grey20", size = 7.0, angle = 90, hjust = .5, vjust = .5, face = "plain"),     
   		  axis.text.y = element_text(color = "grey20", size = 5, angle = 50, hjust = 0.5, vjust = 0, face = "plain")) +
              ggtitle("Health Inpact - INJURIES - of Severe Storms Across the USA") +
		  theme(plot.title = element_text(size = 10, face = "bold")) +
		  xlab("STORM EVENT TYPE") +
		  theme(axis.title.y = element_text(size = rel(0.5), angle = 90), axis.title=element_text(size=14,face="bold")) +
    		  theme(axis.title.x = element_text(size = rel(0.5), angle = 0), axis.title=element_text(size=14,face="bold")) +
              annotation_custom(tableGrob(round(stats, digits = 3), theme=ttheme_minimal(base_size = 7)))


# Plot the events causing the maximum amount of property damage across cost1a states. Include the summary statistics table on the graph.
p2 <- ggplot(cost1a_max, aes(EVTYPE, PROPDMG)) + geom_point(color = "blue") + theme(legend.position="top") + 
 		  theme(axis.text.x = element_text(color = "grey20", size = 7.0, angle = 90, hjust = .5, vjust = .5, face = "plain"),     
  		  axis.text.y = element_text(color = "grey20", size = 5, angle = 50, hjust = 0.5, vjust = 0, face = "plain")) +
              ggtitle("Economic Inpact - PROPERTY DAMAGE - of Severe Storms across the states of GU, TX, AK, TN, CA, KY, VI, IL, WI,MS, UT, CO, ND") +
		  theme(plot.title = element_text(size = 10, face = "bold"), axis.title=element_text(size=14,face="bold")) +
		  xlab("STORM EVENT TYPE") +
              theme(axis.title.y = element_text(size = rel(0.5), angle = 90), axis.title=element_text(size = 14, face = "bold")) +
		  theme(axis.title.x = element_text(size = rel(0.5), angle = 0), axis.title=element_text(size=14,face="bold")) +
		  annotation_custom(tableGrob(round(statsa, digits = 3), theme=ttheme_minimal(base_size = 7))) 


# Plot 2 graphs on the same panel
grid.arrange(p1, p2)
1 Like

How can I include a command to display the code and code results inside an import command/

data_max <- read.table(# your arguments here)
cost1a_max <- read.table(# your arguments here)

I guess the question is, is there a command to display all the code and code results globally? I have tried the command below but it does not work

knitr::opts_chunk$set(echo = TRUE)

The solution is

```{r data-import, include = TRUE}