用ggplot2概述文本 [英] Outlined text with ggplot2

查看:159
本文介绍了用ggplot2概述文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以用ggplot2绘制空心文字,例如带有白色小边框的黑色文字,以便在地图等背景上易于阅读。



理想情况下,我希望实现您可以在Google地图上看到的相同类型的标签:



预先感谢任何提示!

解决方案

下面是一个方法,它实现了从 shadowtext TeachingDemos 包。中间部分的代码可以被包装成一个函数来简化一些事情。这个例子是从Richie Cotton的回答中公然窃取的:

  d < -  diamonds [sample(nrow(diamonds),10)) ] 


p < - ggplot(d,aes(克拉,价格))
θ-seq(pi / 8,2 * pi,length.out = 16 )
xo <-diff(范围(d $ carat))/ 200
yo < - diff(范围(d $ price))/ 200
for(i inta){
p < - p + geom_text(
bquote(aes(x = carat +。(cos(i)* xo),y = price +。(sin(i)* yo),label = cut)) ,
size = 12,color ='black')
}
p < - p + geom_text(aes(label = cut),size = 12,color ='white')
p < - p + opts(panel.background = theme_rect(fill ='green'))
print(p)


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 backgrounds such as maps.

Ideally I'd like to achieve the same type of labels you can see on Google Maps :

Thanks in advance for any hints !

解决方案

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)

这篇关于用ggplot2概述文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆