so I have df like this
df <- read.table(text="
amount nr date
50 1 2017-01-01
150 1 2017-01-03
1500 2 2017-01-04
1450 2 2017-01-04
1250 2 2017-01-04
950 1 2017-02-05
120 3 2017-02-06
300 3 2017-04-06
", header=TRUE)
I created an plot from this df
ggplot(test, aes(x = date, y = amount, fill = nr, group = 1)) +
geom_bar(stat = "identity")
Everything works well until I save this plot with code below
ggsave(filename="D:/Documents/units_plot.png", width = 4, height = 2)
I can't figure out how should I properly set image dimensions (it should be 1000x500px) and how to set text size for legends, axes text sizes. Usually I do it by theme() option i ggplot but for some reason legend is so huge in exported file.
ggplot bases its size calculations around the physical size of the image once it's printed. To get a certain pixel size, you need to pick a good value for DPI (number of pixels per inch), and use that to calculate the size:
ggplot(test, aes(x = date, y = amount, fill = nr, group = 1)) +
geom_bar(stat = "identity") +
theme_grey(base_size = 12)
dpi = 96
ggsave(filename="units_plot.png", width = 1000 / dpi, height = 500 / dpi,
dpi = dpi)
If you pick different DPI values, you'll have to test which font sizes work with that DPI, without making the labels too big or small - I don't think there's any way around that.
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.