Problems with labs(caption =... in ggplot as well as in ggsave - error "Error in titleGrob"

783 views Asked by At

I am having a very strange problem with my ggplot2 where I am trying to save a graph as a png using ggsave that contains a caption in the labs parameter. When I used a standard example posted on the web to use as a test and simple post to replicate here, I am getting the exact same error simply trying to display the graph - which is very strange it displays on my main code and only has this error with ggsave, but in my test it won't even display the graph.

This is my test code:

library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(color = class)) +
  geom_smooth(se = FALSE, method = "loess") +
  labs(
    title = "Fuel efficiency generally decreases with engine size",
    subtitle = "Two seaters (sports cars) are an exception because of their light weight",
    caption = "Data from fueleconomy.gov"
  )

And the error I get with the code above is:

geom_smooth() using formula 'y ~ x' Error in titleGrob(label, x, y, hjust = hj, vjust = vj, angle = angle, : unused argument (expand_y = TRUE)

With my ggsave simply:

Warning: Error in titleGrob: unused argument (expand_y = TRUE)

I would be very grateful for some help. I have just updated to the latest version or R and all other packages associated with my code and this hasn't helped.

1

There are 1 answers

1
Peter Stanbridge On

I have been able to find out the problem - the code users both ggplot and ggtern and the issue is something to do with interference with ggtern and I had forgotten to ggplot2::ggsave.

Many many apologies for creating this post (it was out of desperation but I finally figured it).