amandas333
amandas333

Reputation: 21

How to change default font size in R chart

I am using the R package cooccur and cannot figure out how to change the font size in the associated graphics. The par() method does not seem to work.

Here is the example given by the package:

data(finches)
cooccur.finches <- cooccur(mat=finches,
type="spp_site",
thresh=TRUE,
spp_names=TRUE)
plot(cooccur.finches)

I am trying to change the font size of the species, the title and the legend to no avail on the heat map that is produced. Any help would be MUCH appreciated. Thanks!

Upvotes: 2

Views: 968

Answers (3)

Daniel
Daniel

Reputation: 41

Author of Cooccur here. Sorry for the hassle with the text sizes being hard to adjust. I will deal with this when I get a chance.

Not a permanent solution, but easier than changing the function each time for the species labels, is to just directly re-assign the value in the ggplot object:

p$layers[[2]]$geom_params$size <- 10

Hope that helps. I might be a bit late to the scene...

Upvotes: 0

Gregor Thomas
Gregor Thomas

Reputation: 146110

Unfortunately the author didn't use defined theme inside the function, so if you want to not mess up the other customizations in place, this should work:

p <- plot(cooccur.finches)
p + theme_bw(base_size = 28) +
    theme(axis.text = element_blank(), 
          axis.ticks = element_blank(), 
          plot.title = element_text(vjust = -4, face = "bold"), 
          panel.background = element_rect(fill = "white", colour = "white"), 
          panel.grid = element_blank()
          legend.position = c(0.9, 0.5))

You can also use this code to set the size of the legend or title independently, e.g.

p + theme(plot.title = element_text(vjust = -4, face = "bold", size = 36))

Most unfortunately, this won't change the size of the species labels because they are set with geom_text(). To alter them, you'll have to hack the function yourself cooccur:::plot.cooccur. You only need to modify the last line:

p + geom_text(data = dfids, aes(label = X1), hjust = 1, vjust = 0, 
        angle = -22.5)
# change to
p + geom_text(data = dfids, aes(label = X1), hjust = 1, vjust = 0, 
        angle = -22.5, size = 24)

Upvotes: 3

agstudy
agstudy

Reputation: 121608

It is a ggplot2 plot not a base one. So par will not work.

p <- plot(cooccur.finches)
p + theme(text = element_text(size = 10))  ## change text font size

or

p + theme_grey(base_size = 18)             ## chnage all font size.

Upvotes: 1

Related Questions