How to save plots in base R and ggplot2 on an HPC

Do you wonder how to save plots in base R and ggplot2 on an HPC (High-Performance Computer)? I also searched the internet for that a few years ago! And as one of my colleague recently asked me how to do that, I decided to share my answer with you.

MobaXterm

To be able to save R plots on an HPC, it is important to check that your HPC enables X11-forwarding and that you chose to use it. Here is an example on MobaXterm:

MobaXterm screenshot to see how to activate X11-forwarding

Base R

Error

The code works fine in base RStudio but throws an error if launched on an HPC:

df = data.frame(x = seq(1,8), y = rep(0.5, 8))

jpeg("plot_base_R.jpg", width = 500, height = 500, units = "px")
plot(df[, "x"], df[, "y"], col = palette(), cex.main = 4, pch = 18, cex = 4,
     xlab = "x", ylab = "y", ylim = c(0, 1))
dev.off()
Error in .External2(C_X11, paste0("jpeg::", quality, ":", filename), g$width,  : 
  unable to start device JPEG
Calls: jpeg
In addition: Warning message:
In jpeg("plot_base_R.jpg", width = 500, height = 500, units = "px") :
  unable to open connection to X11 display ''
Execution halted

Solution

To avoid this error, you need to add the type = “cairo” parameter in the jpeg (or png) function:

df = data.frame(x = seq(1,8), y = rep(0.5, 8))

jpeg("plot_base_R.jpg", width = 500, height = 500, units = "px", type = "cairo")
plot(df[, "x"], df[, "y"], col = palette(), cex.main = 4, pch = 18, cex = 4,
     xlab = "x", ylab = "y", ylim = c(0, 1))
dev.off()
Scatter plot saved in base R on an HPC

ggplot2

Error

The same error can be observed with ggplot2 if launched on an HPC:

library(ggplot2)
df = data.frame(x = as.factor(seq(1,8)), y = rep(1, 8))

ggplot(df, aes_string(x = "x", y = "y", color = "x")) +
  geom_point(shape = 18, size = 5)+
  scale_color_manual(values = palette())+
  scale_y_continuous(limits = c(0, 1))+
  theme(legend.position = "none")
ggsave("plot_ggplot2.jpg", width = 1000, height = 1000, units = "px")
Error in .External2(C_X11, paste0("jpeg::", quality, ":", filename), g$width,  : 
  unable to start device JPEG
Calls: ggsave -> dev -> <Anonymous>
In addition: Warning message:
In grDevices::jpeg(..., res = dpi, units = "in") :
  unable to open connection to X11 display ''
Execution halted

Solution

The type = “cairo” parameter is also available in the ggsave command from the ggplot2 R package:

library(ggplot2)
df = data.frame(x = as.factor(seq(1,8)), y = rep(1, 8))

ggplot(df, aes_string(x = "x", y = "y", color = "x")) +
  geom_point(shape = 18, size = 5)+
  scale_color_manual(values = palette())+
  scale_y_continuous(limits = c(0.8, 1.2))+
  theme(legend.position = "none")
ggsave("plot_ggplot2.jpg", width = 1000, height = 1000, units = "px", type = "cairo")
Scatter plot saved in ggplot2 on an HPC

Conclusion

In conclusion, if you enable X11-forwarding and use the type = cairo parameter, you should be able to save plots on an HPC.

Related posts

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply