0

I am trying to change some of the font style in point labels while leaving other parts unchanged using ggplot. For example, in the code below I would like the word "Italic" in the label to be italicized and the word "red" to be the color red.

For my specific example I dont know how many points will be in the dataframe.

This also has to eventually run in Power BI so some packages, like ggtext, might not work.

library(ggrepel)
library(ggplot2)

df<- mtcars

funlab <- function(y){ 
  paste("Italic","\n","Normal", "red", y)
}
df$label <- mapply(funlab,df$mpg)

ggplot(data =df)+geom_point(data = df, aes(x= wt, y = disp)) +
  geom_label_repel(data = df, aes(x= wt, y = disp), label= df$label )
  • 1
    have a look at `ggtext` package - https://github.com/wilkelab/ggtext – Richard Telford Apr 20 '21 at 18:32
  • I was just about to suggest `ggtext`. Using `geom_richtext()` will get you the formatting; however, I don't have an option that would utilize the spacing from `ggrepel` with that geom. – chemdork123 Apr 20 '21 at 18:47
  • Sorry I forgot to mention that this is used in Power BI so some libraries are not available and I think ggtext is one of them. I will edit the question. – Michael Spillane Apr 20 '21 at 19:14
  • @MichaelSpillane there is a plotmath based solution for partial - italic - labels which also seems to wrrk in text_repels: https://stackoverflow.com/questions/41528953/how-do-i-include-italic-text-in-geom-text-repel-or-geom-text-labels-for-ggplot – Ray Apr 20 '21 at 19:36

0 Answers0