如何在画布的边界内绘制geom_text图 [英] How to make geom_text plot within the canvas's bounds

查看:344
本文介绍了如何在画布的边界内绘制geom_text图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用geom_text标记散点图的外围点。
根据定义,这些点趋于接近画布边缘:通常至少有一个单词与画布边缘重叠,使其无用。

显然这可以在下面的例子中用 + xlim(c(1.5,4.5))手动解决:

#test 
df < - data.frame(word = c(bicycle,tricycle,quadricycle),
n.wheels = c 2,3,4),
utility = c(10,6,7))
ggplot(data = df,aes(x = n.wheels,y = utility,label = word))+ geom_text()+ xlim(c(1.5,4.5))



这并不理想,因为


  1. 它不是自动化的,因此如果要生成多个图块,则会减慢过程。

  2. 这不准确,意味着边缘在任何情况下,单词和画布的边缘都不相同。

搜索此内容问题没有解决方案,Hadley Wickham似乎满足于ggplot2的


Using geom_text to label outlying points of scatter plot. By definition, these points tend to be close to the canvas edges: there is usually at least one word that overlaps the canvas edge, rendering it useless.

Clearly this can be solved manually in the case below using + xlim(c(1.5, 4.5)):

# test
df <- data.frame(word = c("bicycle", "tricycle", "quadricycle"),
                 n.wheels = c(2,3,4),
                 utility = c(10,6,7))
ggplot(data=df, aes(x=n.wheels, y=utility, label=word))  + geom_text() + xlim(c(1.5, 4.5))

This is not ideal though, as

  1. It's not automated, so slows down the process if many plots are to be produced
  2. It's not accurate, meaning the distance between the edge of the word and the edge of the canvas is not equal in every case.

Searches for this problem reveal no solutions, and Hadley Wickham seems to be content with labels being cut in half in ggplot2's help page (I know Hadley, they're just an examples ;)

解决方案

ggplot 2.0.0 introduced new options for hjust and vjust for geom_text() that may help with clipping, especially "inward". We could do:

ggplot(data=df, aes(x=n.wheels, y=utility, label=word))  + 
  geom_text(vjust="inward",hjust="inward")

这篇关于如何在画布的边界内绘制geom_text图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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