简体   繁体   中英

Ggplot font size and plot size

Let's say I have any kind of plot as ggplot(). I want to plot it such that the width and height of the diagram are fixed (in cm) and that the font size is given in pt such as it is in Microsoft Word.

How do I do this? I tried +theme (text=element_text(family="Arial", face="plain", size=12/.pt) to make the font 12pt high but this makes it unreadably small.

You can set base_size in theme for font and use ggsave to save the height and width or use egg set_panel_size See this example with iris dataset:

p <- ggplot(iris, aes(Sepal.Length,Sepal.Width)) +
  geom_point() +
  theme_bw(base_size=12, base_family='Times New Roman') + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  labs(title="Iris")


#to view the plot
gridExtra::grid.arrange(egg::set_panel_size(p=p, width=unit(5, "cm"), height=unit(8, "cm")))

#to save the plot
ggsave("figure1.png", dpi=300, dev='png', height=8, width=5, units="cm")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM