Figure text in R was suddenly turned to italic - r

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"))

Related

Specify default font for entire ggplot?

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`)

How to use bold font combined with plain font and line breaks in ggplot2?

In a plot made with the ggplot2 package I want to put a subtitle with a part of the text in bold and the other in plain fonts. Also, I would like to make 2 lines of text.
So far I've been trying with the expression() and bold() functions, and the \n; but I can't get the expected result:
Item 1: Text
Item 2: Text
library(ggplot2)
ggplot(data = mpg,
aes(x = cyl,
y = year)) +
geom_point() +
labs(subtitle = expression(bold("Item 1:")*" Text\n"*bold("Item 2:")*" Text"))
As suggested by #p0bs in his comment one option to achieve your desired result would be to make use of the ggtext package which allows to style text via markdown and/or HTML/CSS, e.g. to switch to bold fontface wrap text inside **, to add a line break use the HTML tag <br>:
library(ggplot2)
library(ggtext)
ggplot(data = mpg, aes(x = cyl, y = year)) +
geom_point() +
labs(subtitle = "**Item 1:** Text<br>**Item 2:** Text") +
theme(plot.subtitle = ggtext::element_markdown())

ggplot2 custom legend combining shape and fill

I am trying to combine fill and color in a ggplot2 legend. Because there are several colors for the x axis, it seems logic that ggplot2 do not know which color to pick in the legend.
For exemple:
library(ggplot2)
ggplot(mpg, aes(fl, hwy)) +
geom_point(aes(color = fl, shape = factor(year), fill = fl)) +
scale_shape_manual(values = c("circle filled", "circle open"))
My goal would be to manually edit the factor(year) legend to look like this:
I played around the guides() function without success.
Edit:
Values for shape can be found by running vignette("ggplot2-specs").
you already had the nearly correct answer with the scale_shape_manual. But somehow the "circle filled" argument is invalid. Since i'm not sure where those values can be looked up, i took the values from a table of a similar question (source):
so with value 20 and 79 you can get the desired result.
ggplot(mpg, aes(fl, hwy)) +
geom_point(aes(color = fl, shape = factor(year), fill = fl)) +
scale_shape_manual(values = c(16,79))
output:
Ok, so here is a very roundabout way of making it look like the image above. Maybe someone else can come up with a more intuitive version:
ggplot(mpg, aes(fl, hwy)) +
geom_point(aes(color = fl, shape = factor(year), fill = factor(year))) +
scale_shape_manual(values = c(16,79), guide = FALSE) +
scale_fill_manual("Year", values=c("grey","white"))+
guides(fill = guide_legend(override.aes = list(shape = c(21,21),
color = c("black", "black"))))
Output:

Cannot italicize facet labels with labeller = label_parsed

I have a ggplot
s = ggplot(final, aes(y = avg,x=factor(dose),fill = factor(mo))) +
facet_grid(.~gene, labeller = label_parsed) +
geom_bar(position = "dodge", stat = "identity", color = "black")
where final is a data.frame containing 4 columns: mo, dose, gene and avg.
I've been trying to italicize the facet labels with
s + theme(strip.text.x = element_text(face = "italic", size = 10, colour = "white"))
to no avail. Size and colour can be changed with no problems.
However, once I remove "labeller" argument from facet_grid(), the font face can be changed accordingly. Is this a bug?
Although I'll work around this problem by setting them individually as suggested here, I'm sure theme() was there for a very good reason.
As you said, you need something like what has been shown here
In fact you have to change directly your levels with italic()
then you use labeller=label_parsed in ggplot
an example:
factor1=rep(letters[1:3], each=3)
factor2=rep(1:3,times=3)
x=rep(1,9)
y=1:9
df=cbind.data.frame(factor1,factor2,x,y)
i revalue the levels:
levels(df$factor1)= c("a"=expression(paste("factor_", italic("a"))),
"b"=expression(paste("factor_", italic("b"))),
"c"=expression(paste("factor_", italic("c"))))
And adapt ggplot:
ggplot(df, aes(x=x, y=x))+facet_grid(factor2~factor1, labeller=label_parsed)+geom_point()

How to change fontsize in direct.label?

I can't change the fontsize in a direct.label (from the directlabels package) ggplot2 plot. See below for a reproducible example - there's no problem in rotating the labels 45 degrees, making them bold, serif, and 50% transparent (all the other arguments in the list at the end of the code below) - but I can't control the fontsize. (I don't really want them to be 25, this is just for testing....)
Is there something I'm missing, or is this a bug?
library(ggplot2)
library(scales)
library(directlabels)
df <- data.frame(x = rnorm(26), y=rnorm(26), let=letters)
p <- ggplot(df, aes(x, y, color=let)) + geom_point()
direct.label(p,
list("top.points", rot=45, fontsize=25,
fontface="bold", fontfamily="serif", alpha=0.5))
I figured it out, you use cex to change the font size.
df <- data.frame(x = rnorm(26), y=rnorm(26), let=letters)
p <- ggplot(df, aes(x, y, color=let)) + geom_point()
direct.label(p,
list("top.points", rot=45, cex=6,
fontface="bold", fontfamily="serif", alpha=0.5))
That would give you,
It's kind of a different route, but would you consider doing it all in ggplot2?
ggplot(df, aes(x, y, color=let)) +
geom_point() +
geom_text(df, mapping=aes(x, y, label=let, colour=let),
size=5, vjust=-.55, hjust=.55, angle = 45, fontface="bold",
family ="serif", alpha=0.5) + opts(legend.position = "none")
This would give you this, and you can adjust the fontsize using size

Resources