与GGPLOT2文字概述(Outlined text with ggplot2)

2019-06-24 15:57发布

我想知道是否有画“概括文本”与GGPLOT2的方式,例如用一个白色的小边框黑色文字,以使其在背景易于阅读,如地图。

理想情况下,我想达到同样的类型标签,你可以在谷歌地图上看到:

感谢您的任何提示!

Answer 1:

这是实现从总体思路的方法shadowtext在功能TeachingDemos包。 对于中间部分的代码可以包装成一个功能,以简化一些事情。 这个例子是公然从里奇棉花的回答被盗:

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)



Answer 2:

许多simplier解决方案是使用shadowtext库,并使用geom_shadowtext代替geom_text



Answer 3:

不理想或非常灵活的,但你可以通过绘制大胆单文本,在上面则标单的文字效果。

我用了一个绿色的面板背景来模拟图。

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"))
)



Answer 4:

由Greg雪接受的答案不工作了ggplot2@2.2.1因为调用aes ,而不是aes_q

使用

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' )
}

代替。



文章来源: Outlined text with ggplot2
标签: r ggplot2