How to change the default font size in ggplot2 - r

I'd like to know if it is possible to change some default parameters of ggplot2 graphics, like font size for instance, for a whole R session. The idea is to avoid setting them for each plot.

Use theme_set()
theme_set(theme_gray(base_size = 18))
qplot(1:10, 1:10)

Use theme_set if you want to update for the remainder of your active session:
theme_set(theme_grey(base_size = 18))
If you only want to change one graph you can set the base_size in the theme:
qplot(1:10, 1:10) + theme_grey(base_size = 18)
ggplot(mtcars, aes(x = mpg, y = cyl)) +
geom_point() +
theme_grey(base_size = 18)

Related

Can you vary text size within the same ggplot2 axis?

I'd like to use ggplot2 to make a plot where the axis text size varies between labels- for example, larger font every five ticks with smaller font for the intervening ticks. I looked at using minor_breaks in scale_x_continuous, but I couldn't find a way to label the minor breaks.
The best I've got to work so far is a modification from this answer where I use bquote to pass an expression for the axis labels:
label_span <- 1:40
ShrinkIf <- Vectorize(function(val) {
if (val %% 5 == 0) return(as.character(val))
return(bquote(scriptstyle(.(as.character(val)))))
})
x_labels <- ShrinkIf(label_span)
x_labels <- purrr::invoke(expression, x_labels)
ggplot(mtcars, aes(x = mpg , y = hp)) +
geom_point() +
scale_x_continuous(breaks = label_span, labels = x_labels)
Is there a better way to go about this, or maybe a way with a little more control of the label size (or even font choice / text decoration, etc)? Thanks in advance for your help!
You can create a vector of text sizes and add it using element_text()
library(ggplot2)
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point() +
theme(
axis.text.x = element_text(size = rep(c(12,24), 3))
)

How to let geom_text inherit theme specifications? (ggplot2)

Is there an elegant way in ggplot2 to make geom_text/geom_label inherit theme specifications like a base_family?
Or asked the other way round: Can I specify a theme that also applies to geom_text/geom_label?
Example:
I want text/labels to look exactly like the axis.text as specified in the theme...
Obviously I could add the specifications manually as optional arguments to geom_text, but I want it to inherit the specifications "automatically"...
library("ggplot2")
ggplot(mtcars, aes(x = mpg,
y = hp,
label = row.names(mtcars))) +
geom_point() +
geom_text() +
theme_minimal(base_family = "Courier")
Addition: A solution that works with ggrepel::geom_text_repel/geom_label_repel as well would be perfect...
You can
Setting Overall font
Firstly, depending on the system you will need to check which fonts are available. As I am running on Windows I am using the following:
install.packages("extrafont")
library(extrafont)
windowsFonts() # check which fonts are available
The theme_set function lets you specify the overall themes for ggplot. So therefore theme_set(theme_minimal(base_family = "Times New Roman")) lets you define the fonts for the plot.
Make Labels Inherit Font
To make the labels inherit this text, there are two things we need to use:
update_geom_defaults lets you update the geometry object styling for future plots in ggplot: http://ggplot2.tidyverse.org/reference/update_defaults.html
theme_get()$text$family extracts the font of the current global ggplot theme.
By combining these two, the label styles can be updated as follows:
# Change the settings
update_geom_defaults("text", list(colour = "grey20", family = theme_get()$text$family))
update_geom_defaults("text_repel", list(colour = "grey20", family = theme_get()$text$family))
Results
theme_set(theme_minimal(base_family = "Times New Roman"))
# Change the settings
update_geom_defaults("text", list(colour = "grey20", family = theme_get()$text$family))
# Basic Plot
ggplot(mtcars, aes(x = mpg,
y = hp,
label = row.names(mtcars))) +
geom_point() +
geom_text()
# works with ggrepel
update_geom_defaults("text_repel", list(colour = "grey20", family = theme_get()$text$family))
library(ggrepel)
ggplot(mtcars, aes(x = mpg,
y = hp,
label = row.names(mtcars))) +
geom_point() +
geom_text_repel()

Crop out ggplot2 whitespace around plot

Is there a way to remove the white space surrounding a ggplot2 plot when the shape has been changed using coord_fixed()? I would like the white space above and below to be cropped away so that only the plotting area and axis labels remain. I am rendering the plot output in an R markdown file without saving.
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + coord_fixed(ratio = 1)
The code below produces the following plot:
When you use:
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
coord_fixed(ratio = 1) +
ggsave('plot.jpg', width = 6, height = 1.5, dpi = 300)
You get a plot with less white space:
Another option could be to use the png or jpeg device:
p <- ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
coord_fixed(ratio = 1)
jpeg('plot.jpg', width = 600, height = 150)
p
dev.off()
If you are looking for a solution which also works in R markdown (i.e. output as PDF/HTML), this solved it for me: first set the aspect ratio and then remove the additional margin on the top via the theme() settings.
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, shape = Species, color = Species)) +
geom_point(size = 5) +
coord_fixed(ratio = 1/2) +
theme(plot.margin=unit(c(-0.30,0,0,0), "null")) # remove margin around plot
See also this blog post for more details.
Session info:
MacOs 10.13.6, R 3.6.3, ggplot2_3.3.1

ggplot geom_text font size control

I tried to change the font to 10 for the labels of my bar plot in ggplot2 by doing something like this:
ggplot(data=file,aes(x=V1,y=V3,fill=V2)) +
geom_bar(stat="identity",position="dodge",colour="white") +
geom_text(aes(label=V2),position=position_dodge(width=0.9),
hjust=1.5,colour="white") +
theme_bw()+theme(element_text(size=10))
ggsave(filename="barplot.pdf",width=4,height=4)
but the resulting image has super big font size for the bar plot labels.
Then I thought of modifying in geom_text() with this:
geom_text(size=10,aes(label=V2),position=position_dodge(width=0.9),
hjust=1.5,colour="white")
The label font is even bigger...
I can change the size within geom_text to something like 3 and now it looks like font 10, similar to the axis labels.
I'm wondering what's going on? Does theme(text=element_text(size=10)) doesn't apply to labels?
And why size of 10 in geom_text() is different from that in theme(text=element_text()) ?
Here are a few options for changing text / label sizes
library(ggplot2)
# Example data using mtcars
a <- aggregate(mpg ~ vs + am , mtcars, function(i) round(mean(i)))
p <- ggplot(mtcars, aes(factor(vs), y=mpg, fill=factor(am))) +
geom_bar(stat="identity",position="dodge") +
geom_text(data = a, aes(label = mpg),
position = position_dodge(width=0.9), size=20)
The size in the geom_text changes the size of the geom_text labels.
p <- p + theme(axis.text = element_text(size = 15)) # changes axis labels
p <- p + theme(axis.title = element_text(size = 25)) # change axis titles
p <- p + theme(text = element_text(size = 10)) # this will change all text size
# (except geom_text)
For this And why size of 10 in geom_text() is different from that in theme(text=element_text()) ?
Yes, they are different. I did a quick manual check and they appear to be in the ratio of ~ (14/5) for geom_text sizes to theme sizes.
So a horrible fix for uniform sizes is to scale by this ratio
geom.text.size = 7
theme.size = (14/5) * geom.text.size
ggplot(mtcars, aes(factor(vs), y=mpg, fill=factor(am))) +
geom_bar(stat="identity",position="dodge") +
geom_text(data = a, aes(label = mpg),
position = position_dodge(width=0.9), size=geom.text.size) +
theme(axis.text = element_text(size = theme.size, colour="black"))
This of course doesn't explain why? and is a pita (and i assume there is a more sensible way to do this)
Take a look at the relevant entry in ggplot2's customization FAQ: https://ggplot2.tidyverse.org/articles/faq-customising.html#what-is-the-default-size-of-geom_text-and-how-can-i-change-the-font-size-of-geom_text
You can modify the default size of geom_text() by placing update_geom_defaults("text", list(size = X), where X is your choice of new size, at the beginning of your script.

ggplot font size for different elements

I know that after I create a ggplot graph I can use theme_get() to return detail of all the theme elements. This has been very helpful in figuring out things like strip.text.x and the like. But I have two things I can't figure out:
1) In the following ggplot graphic, what is the name of the theme item representing the phrase "Percent of wood chucked by the woodchuck" as I want to resize it to a larger font:
2) How do I reformat the y axis labels to read 10%, 20, ... instead of .1, .2, ...
For 1), it is $axis.title.y
p + theme(axis.title.x = element_text(size = 25))
where p is an existing ggplot object.
I don't know about 2) off hand.
For (2) what you want is to use a formatter:
dat <- data.frame(x=1:10,y=1:10)
#For ggplot2 0.8.9
ggplot(dat,aes(x = x/10,y=y/10)) +
geom_point() +
scale_x_continuous(formatter = "percent")
#For ggplot2 0.9.0
ggplot(dat,aes(x = x/10,y=y/10)) +
geom_point() +
scale_x_continuous(labels = percent_format())

Resources