I have below ggplot
library(ggplot2)
library(stringr)
library(tidyverse)
# Create long labels to be wrapped
iris$Species = paste(iris$Species,
"random text to make the labels much much longer than the original labels")
ggplot(iris, aes(Sepal.Length, Sepal.Width, colour=str_wrap(Species,20)))
geom_point()
labs(colour="Long title shortened\nwith wrapping")
theme(legend.key.height=unit(2, "cm"))
I am wondering if I can apply some CSS
styling in the legend title
. For example, I want to change font size of the word wrapping
to 12, whereas remaining words with font size 10.
Any pointer will be very helpful.
CodePudding user response:
You can use the package ggtext
and the element_markdown
for your legend.title
with some CSS
styling. You can modify to whatever you want. You can use the following code:
library(ggplot2)
library(stringr)
library(tidyverse)
library(ggtext)
# Create long labels to be wrapped
iris$Species = paste(iris$Species,
"random text to make the labels much much longer than the original labels")
ggplot(iris, aes(Sepal.Length, Sepal.Width, colour=str_wrap(Species,20)))
geom_point()
labs(colour="<span style='font-size:10pt'>Long title shortened with </span><span style='font-size:12pt'>wrapped</span>")
theme(
legend.title = element_markdown()
)
Output:
As you can see the word wrapped
has a font size of 12
and the other words have 10