8
votes

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)
1
works for me. where is the code for ggsave you are using? Are you saving the final p, p <- p + theme(...) before jpg'ing? ggsave will save the last plot displayed, but it would be better to be explicit with the objectrawr
Works for me as well.MichaelVE
to clarify a point that rawr made - your second line of code only displays an altered version of p, but does not actually change the object p. So when you save p, you are saving the thing that was created earlier. If you want to save the version of the plot with altered text size, you need to assign it to an object, and then save that object.Matt74
Thank you @rawr, I'm new with R and I overlooked that for so long. I wasn't saving my changes back into the variable.Kim Ngo
And thanks for clarifying @Matt74.Kim Ngo

1 Answers

10
votes

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