I would like to specify a font for all text when making a ggplot. I found I can set the base size under ggplot's selected theme but cannot find a clear example of setting to a monospaced font such as say Courier or preferably Roboto Mono for the entire plot.
This solution looked like it should work:
Can't change fonts in ggplot/geom_text
But no joy in my attempt below
install.packages("extrafont")
library(extrafont)
loadfonts(device = "win")
require(tidyverse)
ggplot(mtcars, aes(x = mpg, y = cyl, group = carb)) +
geom_point() +
theme_light(base_size = 15, base_family = "Roboto Mono")
Going along with #Allan Cameron 's response.
windowsFonts('Roboto Mono'=windowsFont("Roboto Mono"))
ggplot(mtcars, aes(x = mpg, y = cyl, group = carb)) +
geom_point() +
theme_light(base_size = 15, base_family = windowsFonts()$`Roboto Mono`)
Related
may be easy but can't figure it out:
I want to add two subtitles to my plot and make the first bold but I can't find a way to do it without scrambling everything around. I tried this:
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
labs(title = expression(""*bold("Title")),
subtitle = expression(""*bold("First subtitle")*"\nSecond subtitle"))
But for the second subtitle the \n does not work and stays on the same line. How do I put it below without making it bold? Thanks in advance!
Alternatively, you can use element_markdown from ggtext. There, you need to use <br> to create a line break.
library(ggplot2)
library(ggtext)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme(
plot.title = element_markdown(),
plot.subtitle = element_markdown()
) +
labs(title = "**Title**",
subtitle = "**First subtitle**<br>Second subtitle")
Created on 2023-01-17 by the reprex package (v1.0.0)
How about this:
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
labs(title = expression(bold(Title)),
subtitle = expression(atop(bold(First~subtitle),~~~~Second~subtitle)))
Created on 2023-01-17 by the reprex package (v2.0.1)
This answer proposed the use of atop() instead of a line break.
I've got a problem with the text in R figure.
my code is
library("ggplot2")
ggplot(df, aes(x = class, y = Proportion)) +
geom_point(aes(color = class)) +
scale_color_viridis_d() +theme_minimal()+
theme(text=element_text(size=16,face = "plain"))
However, the text in the figure is italic. I add the "face = "plain",
but it didn't work.
I don't know what is the reason,
I will appreciate an answer.
face argument have 4 options: plain, italic, bold & bold.italic.
If you don't want the figure is italic, you can remove face argument in element_text or you another value "bold".
class class(df$class) may suppose to be a factor?
mtcars$cyl <- factor(mtcars$cyl)
library("ggplot2")
ggplot(mtcars, aes(x = cyl, y =mpg)) +
geom_point(aes(color = cyl)) +
scale_color_viridis_d() +theme_minimal()+
theme(text=element_text(size=16,face = "plain"))
Please consider the following minimal example:
library(ggplot2)
library(ggrepel)
ggplot(mtcars) +
aes(x = mpg, y = qsec) +
geom_line() +
geom_text(x = 20, y = 20, label = "(20,20)")
I guess you can see pretty easily that the text "(20,20)" is heavily overplotted (actually, I don't know whether that's the correct word. I mean that the text is plotted several times at one location).
If I use annotate(), this does not happen:
ggplot(mtcars) +
aes(x = mpg, y = qsec) +
geom_line() +
annotate("text", x = 20, y = 20, label = "(20,20)")
"So, why don't you use annotate() then?" you might ask. Actually, I don't want to use text for annotation but labels. And I also want to use the {ggrepel} package to avoid overplotting. But look what happens, when I try this:
ggplot(mtcars) +
aes(x = mpg, y = qsec) +
geom_line() +
geom_label_repel(x = 20, y = 20, label = "(20,20)")
Again, many labels are plotted and {ggrepel} does a good job at preventing them from overlapping. But I want only one label pointing at a specific location. I really don't understand why this happens. I only supplied one value for x, y and label each. I also tried data = NULL and inherit.aes = F and putting the values into aes() within geom_label_repel() to no effect. I suspect that there are as many labels as there are rows in mtcars. For my real application that's really bad because I have a lot of rows in the respective dataset.
Could you help me out here and maybe give a short explanation why this happens and why your solution works? Thanks a lot!
Add "check_overlap = TRUE" to geom_text to prevent overplotting.
library(ggplot2)
ggplot(mtcars) +
aes(x = mpg, y = qsec) +
geom_line() +
geom_text(x = 20, y = 20, label = "(20,20)", check_overlap = TRUE)
geom_text or geom_label_repel adds one label per row. Therefore you can submit a separate dataset for annotation geom. For example:
library(ggplot2)
library(ggrepel)
ggplot(mtcars, aes(mpg, qsec)) +
geom_line() +
geom_label_repel(aes(20, 20, label = "(20,20)"), data.frame())
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()
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)