Outlined text with ggplot2

前端 未结 4 726
走了就别回头了
走了就别回头了 2020-12-23 16:55

I\'d like to know if there is a way to draw \"outlined text\" with ggplot2, for example black text with a small white border, in order to make it easily readable on backgrou

相关标签:
4条回答
  • 2020-12-23 17:02

    Not ideal or very flexible but you can get the effect by drawing bold mono text, then standard mono text on top.

    I've used a green panel background to simulate the map.

    d <- diamonds[sample(nrow(diamonds), 10), ]
    
    (p <- ggplot(d, aes(carat, price)) +
      geom_text(
        aes(label = cut, family = "mono", fontface = "bold"), 
        size = 12, 
        colour = "black"
      ) +
      geom_text(
        aes(label = cut, family = "mono"), 
        size = 12, 
        colour = "white"
      ) +
      opts(panel.background = theme_rect(fill = "green"))
    )
    

    text-on-bold-text with the diamonds dataset

    0 讨论(0)
  • 2020-12-23 17:04

    Much simplier solution is to use shadowtext library and use geom_shadowtext instead of geom_text

    0 讨论(0)
  • 2020-12-23 17:06

    Here is an approach that implements the general idea from the shadowtext function in the TeachingDemos package. The code for the middle part could be wrapped into a function to simplify some things. The example is blatantly stolen from Richie Cotton's answer:

    d <- diamonds[sample(nrow(diamonds), 10), ]  
    
    
    p <- ggplot(d, aes(carat, price) ) 
    theta <- seq(pi/8, 2*pi, length.out=16)
    xo <- diff(range(d$carat))/200
    yo <- diff(range(d$price))/200
    for(i in theta) {
        p <- p + geom_text( 
            bquote(aes(x=carat+.(cos(i)*xo),y=price+.(sin(i)*yo),label=cut)), 
                        size=12, colour='black' )
    }
    p <- p + geom_text( aes(label=cut), size=12, colour='white' )
    p <- p + opts( panel.background=theme_rect(fill='green' ) )
    print(p)
    

    enter image description here

    0 讨论(0)
  • 2020-12-23 17:13

    The accepted answer by Greg Snow doesn't work anymore with ggplot2@2.2.1 because of the call of aes instead of aes_q.

    Use

    for(i in theta) {
      p <- p + geom_text( 
        aes_q(x = bquote(carat+.(cos(i)*xo)),
              y = bquote(price+.(sin(i)*yo)),
              label = ~cut), 
        size=12, colour='black' )
    }
    

    instead.

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