Combining New Lines and Italics in Facet Labels with Ggplot2

Combining new lines and italics in facet labels with ggplot2

To get italics, you need the formatting described in plotmath (and then for that to be parsed as an expression). However, the plotmath syntax does not have a line break operation. You can get something similar with atop, though. With your given example, you can set the labels to

levels(length_subject$CONSTRUCTION) <- 
c("atop(textstyle('THAT'),textstyle('Extraposed'))",
"atop(textstyle('THAT'),textstyle('Post-predicate'))",
"atop(atop(textstyle('TO'),textstyle('Extraposed')),italic('for')*textstyle('-subject'))",
"atop(atop(textstyle('TO'),textstyle('Post-predicate')),italic('for')*textstyle('-subject'))",
"atop(atop(textstyle('THAT'),textstyle('Extraposed')),italic('that')*textstyle('-omission'))",
"atop(atop(textstyle('THAT'),textstyle('Post-predicate')),italic('that')*textstyle('-omission'))")

and then adding labeller=label_parsed to the facet_grid call

ggplot( length_subject, aes( x = SUBJECT ) ) +
geom_histogram(binwidth=.6, colour="black", fill="grey") +
ylab("Frequency") +
xlab("Subject length") +
scale_x_discrete(breaks=c(2,4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30)) + #
facet_grid( SUBJECT_TYPE~CONSTRUCTION, scales="free_x", space="free",
labeller=label_parsed) +
theme(strip.text.x = element_text(size = 8))

gives

Sample Image

It's not perfect (the spacing between lines is not the same, and the disparity would only get worse the more lines there are), but that is the only way I've found to combine the two (newlines in plotmath expressions).

How to use italics for facet labels in ggplot2?

Something along these lines should work:

... + theme(strip.text = element_text(face = "italic"))

See the docs for more detail about theme().

Combining new lines and plotmath in facet labels with ggplot2

I think the plotmath bit you might be looking for is the atop() function.

library(ggplot2)
data("mtcars")
mtcars$am<-factor(
mtcars$am,levels = c("1","0"),
labels = c("atop('Amerikee C','(mg C kg tuna^-1)')",
"atop('Is watchin','(mg CO'[2]*' kg tuna'^-1*'d'^-1*')')"))

ggplot(data=mtcars,aes(x=mpg,y=disp))+
geom_point()+
facet_wrap(am~.,scales=("free_y"),
strip.position = "left",
labeller = label_parsed)

Sample Image

Created on 2021-02-11 by the reprex package (v1.0.0)

How to italicize part (one or two words) of x-axis labels using multiple lines

You can cheat using atop:

my_x_titles <- c(
expression(atop(paste("4 cylinders"), (italic(N) == 11))),
expression(atop(paste("6 cylinders"), (italic(N) == 7))),
expression(atop(paste("8 cylinders"), (italic(N) == 14)))
)

base + scale_x_discrete(breaks = c(4, 6, 8),
labels = my_x_titles)

How to combine italic and non-italic components of a `geom_text` call. Add `geom_text` labels to a series of `facet_grid` scatter plots

You can do

  geom_text(x = 200, y = 30, 
label = paste0("italic(r) == ", df$correlation), parse = TRUE)

To display a rounded correlation:

  geom_text(x = 200, y = 30, 
label = paste0("italic(r) == ", round(df$correlation,2)), parse = TRUE)


Related Topics



Leave a reply



Submit