Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save plots in R and have them nice looking

Tags:

r

ggplot2

cowplot

I'm trying to make a plot in R with some data and a table beneath it. In R, it looks good (Picture 1), however, when I export the picture (Picture 2), it looks really ugly and is not in same format.

library(tidyverse)
library(cowplot)


p <- ggplot(iris, aes(Sepal.Length, Petal.Length, col = Species))  + geom_point()

info <- iris %>% group_by(Species) %>% summarise_all(mean)

table_plot <- tableGrob(info, theme = ttheme_default(base_size = 8), rows = NULL)

plot_total <- plot_grid(p, table_plot, nrow = 2, rel_heights = c(4 / 5, 1 / 5))
plot_total

save_plot("iris.png", plot_total)

Picture1

Picture2

like image 455
MLEN Avatar asked Oct 22 '25 04:10

MLEN


2 Answers

Another solution is using ggsave:

ggsave("plotname.png", plot = p, width = 30, height = 20, units = "cm")

You might have to play around with the dimension a bit to get it right. You can also specify the format you want the plot in (i.e. .png or .tiff, etc.) and you can specify the units as well.

like image 140
Bjorn Avatar answered Oct 24 '25 15:10

Bjorn


Try:

png('iris.png', width = 1920,height = 1080)
print(plot_total)
dev.off()
like image 31
Balter Avatar answered Oct 24 '25 14:10

Balter