Inception
Inception

Reputation: 103

Saving 600 resolution .JPG box plot using dev.off()

I am new to R and trying to save a box plot after using ggplot2. I can successfully make the box plot in R display window. However, the quality of the figure is not good for publication. Therefore, I am using the function dev.off() to save the figure(box plot) in .jpg format in the working directory. After running the below code, I can see a blank figure in the directory without any box plot. I would appreciate your inputs to solve this issue.

My Code:

options(scipen = 500)
library(ggplot2)   
library(RColorBrewer)

dat <- read.csv(file="Streamflow_4.5.csv",head=TRUE,sep=",")

jpeg(paste('P1.jpg',sep=''), quality=100, height=1800, width=3000,
     pointsize=14, res=600)
dat$Scenario <- factor(dat$Scenario, 
  labels=c("Base","Climate Change", "No Till", "Cover Crop", "Filter Strip"))
P1 <- ggplot(dat, aes(x=Scenario, y=Streamflow, fill=Scenario)) + 
    geom_boxplot()
dev.off()

Upvotes: 0

Views: 596

Answers (1)

Kim
Kim

Reputation: 4328

You have made your plot but not called it into the pane.

jpeg('P1.jpg', quality=100, height=1800, width=3000, pointsize=14, res=600)
P1 <- ggplot(dat, aes(x=Scenario, y=Streamflow, fill=Scenario)) + geom_boxplot()
P1
dev.off()

Simple addition of P1. See if that works.

If you don't need the plot stored, you could also simplify it as following:

jpeg('P1.jpg', quality=100, height=1800, width=3000, pointsize=14, res=600)
ggplot(dat, aes(x=Scenario, y=Streamflow, fill=Scenario)) + geom_boxplot()
dev.off()

FYI, setting the working directory and doing rm(list=ls()) is one of the fiery pits of hell in R coding! Try to not do it!

Upvotes: 1

Related Questions