Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplots2 ggsave text size not changing

Tags:

r

ggplot2

I'm having issues with changing the size of the title, X-Y labels, X-Y axis text for my ggplot2. I'm using ggsave to save the plot as a jpg.

p <- ggplot()
p + theme(axis.title = element_text(size=30), axis.text.y = element_text(size=30),
          axis.text.x = element_text(size=30))

but changing the sizes of these texts doesn't change anything on the plot. Would anyone know how to properly change the text sizes?


So I fixed the issue I was having so the changes I make to theme are not affecting the plot (I've tested with changing text color), however the size of the axis text still does not change.

p <- ggplot(d[d$user==i,], aes(x=date, y=url, group=user, label=user)) + geom_line() + geom_point() +
  labs(list(title=i, x="Date and Time", y = "URL")) +   # Plot labels
  axis.POSIXct(1, at=seq(daterange[1], daterange[2], by="hour")) # Set x axis range to first and last date-time in data
p <- p + modifiedtheme
ggsave(plot = p, filename = "sample.jpg", height=2, width=6)
like image 566
Kim Ngo Avatar asked Jun 02 '15 00:06

Kim Ngo


People also ask

How do I make text bigger in ggplot2?

How can I change the default font size in ggplot2? Set base_size in the theme you're using, which is theme_gray() by default. The base font size is 11 pts by default. You can change it with the base_size argument in the theme you're using.

What are the default dimensions that Ggsave ()`?

When TRUE (the default), ggsave() will not save images larger than 50x50 inches, to prevent the common error of specifying dimensions in pixels.

How do I increase font size in Xlabel in R?

To change the font size of text, use cex (character expansion ratio). The default value is 1. To reduce the text size, use a cex value of less than 1; to increase the text size, use a cex value greater than 1.

How do I increase the width of a ggplot2?

To increase the width of axes (both X-axis and Y-axis at the same time) using ggplot2 in R, we can use theme function with axis. line argument where we can set element_line argument to a larger value.


1 Answers

Here is a minimal, fully reproducible version of the problem (or lack of any problem, as comments have pointed out). Your own posted code appears to be correct, but maybe this example will help you solve whatever the real problem is:

library(ggplot2)

p1 = ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, colour=Species)) + 
     geom_point()

p2 = ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, colour=Species)) + 
     geom_point() + 
     theme(axis.title=element_text(size=30))

ggsave("figure1.jpg", plot=p1, height=3, width=4, units="in", dpi=150)
ggsave("figure2.jpg", plot=p2, height=3, width=4, units="in", dpi=150)

enter image description here

like image 131
bdemarest Avatar answered Oct 27 '22 12:10

bdemarest