Embolden substring of object passed through geom_text()

前端 未结 2 454
情歌与酒
情歌与酒 2021-01-03 17:12

I\'m trying to produce a graph and I\'d like to bold part of a string of a geom_text(). Here\'s some sample data and a graph:

temp <- data.f         


        
相关标签:
2条回答
  • 2021-01-03 17:57

    Try this:

    my_text <- expression(paste("small ", bold("bullet"), " point of text"))
    ggplot() +
      lims(x = c(0,100), y = c(0, 100)) + 
      annotate('text', x=25, y=25, label=my_text)
    

    0 讨论(0)
  • 2021-01-03 18:08

    Working with plotmath in ggplot2 confounds me every time, especially when trying to pass in a variable.

    I think I got things to work how you want them, complete with one bold word with a separate color passed from a variable. It involves deparse() and bquote() and bold() along with phantom(). Phew.

    The phantom() holds the place of the words you don't want to show with the specific color, so it takes two annotate() layers to make the whole expression using different colors.

    a = "bullet"
    ggplot(mtcars, aes(x = wt, y = mpg)) + 
        annotate("text", x = 4, y = 25, 
                 label = deparse(bquote(phantom(small)~bold(.(a))~phantom(point~of~text))),
                 colour = "red", parse = TRUE) +
        annotate("text", x = 4, y = 25, 
                 label = deparse(bquote(small~phantom(bold(.(a)))~point~of~text)),
                 colour = "black", parse = TRUE)
    

    Adding facets

    I feel certain there is an easier approach to facets and "a" as a vector but this is what I've come up with on a Friday afternoon. :-D

    My approach is to loop through the (character) vector and make a new variable with all the deparsing and bquoting and such. The faceting variable is part of the text dataset.

    dat = data.frame(x = c(4, 4),
                     y = c(25, 25),
                     a = c("bullet", "shell"),
                     am = c(0, 1) )
    
    dat$a1 = sapply(as.character(dat$a), 
                           function(x) deparse(bquote(phantom(small)~bold(.(x))~phantom(point~of~text))), 
                    simplify = TRUE)
    dat$a2 = sapply(as.character(dat$a), 
                           function(x) deparse(bquote(small~phantom(bold(.(x)))~point~of~text)), 
                    simplify = TRUE)
    
    ggplot(mtcars, aes(x = wt, y = mpg) ) +
        geom_text(data = dat, aes(x, y, label = a1), color = "red", parse = TRUE) +
        geom_text(data = dat, aes(x, y, label = a2), color = "black", parse = TRUE) +
        facet_wrap(~am)
    

    Working with multiple variables

    Last, if you need to get characters pasted together with colors and font style and such using multiple columns in a vector you an do so. My approach involves loping through every row in the dataset, working with the columns that contain the label info. Note I'm working with characters (not factors) to avoid problems.

    I pull in the helper function pmap() from package purrr for rowwise work.

    If colors need to be different for the highlighted word in each facet you can define the colors in the data.frame and use scale_color_identity to use those colors.

    dat = data.frame(x = c(4, 4),
                     y = c(25, 25),
                     a = c("bullet", "shells"),
                     b = c("point of text", "on the beach"),
                     am = c(0, 1),
                     color = c("red", "purple"),
                     stringsAsFactors = FALSE)
    
    library(ggplot2)
    library(purrr)
    
    dat$a1 = pmap_chr(dat, function(a, b, ...) 
                    deparse(bquote(phantom(small)~bold(.(a))~phantom(.(b))) ) )
    
    dat$a2 = pmap_chr(dat, function(a, b, ...) 
         deparse(bquote(small~phantom(bold(.(a)))~.(b))))
    
    ggplot(mtcars, aes(x = wt, y = mpg) ) +
         geom_text(data = dat, aes(x, y, label = a1, color = color), parse = TRUE) +
         geom_text(data = dat, aes(x, y, label = a2), color = "black", parse = TRUE) +
         facet_wrap(~am) +
         scale_color_identity()
    

    0 讨论(0)
提交回复
热议问题