I've currently got a script that generates multiple figures of the same type when run (plotting the effect of different treatments on the same categories in my data set) which are all dodged bar graphs with the same values in the x-axis). An example would be:
d <- data.frame(x = runif(1000), y = runif(1000)^2, very_long_label_name = runif(1000)^3)
d <- round(d, 1)
d <- melt(d)
qplot(data = d[d$variable != "very_long_label_name",], factor(value), position = "dodge",
geom = "histogram", fill = variable)
ggsave("test1.png", height = 3.5, width = 4.5)
qplot(data = d[d$variable != "y",], factor(value), position = "dodge",
geom = "histogram", fill = variable)
ggsave("test2.png", height = 3.5, width = 4.5)
Since my figures are dodged bar charts, I've got a legend on the side for bar colors. But since each figure compares different treatments, the labels on the legends are of different lengths, so the figures end up coming out with differing aspect ratios and text sizes. Is there a way for me to control the size of text and the width of the x-axis across multiple figures?
I've tried looking at How to control ggplot's plotting area proportions instead of fitting them to devices in R? but coord_fixed()
seems to get ignored by ggsave()
. How can I make consistent-width plots in ggplot (with legends)? is a very similar problem, but the answers all seem to assume that I'll be putting the figures together rather than spreading them out across a paper. Using theme_set()
would seem promising for the font size issue at least, except that the ultimate size of the text is then affected by the size specified in ggsave()
.
It seems like the feature I'm actually looking for would be the ability to determine an image's output width by setting the width of the plotting region, rather than the width of the entire drawing area as ggsave("test.png", width = 3)
does. Does such a feature exist in ggplot2?