简体   繁体   English

ggplot2中的geom_text()大小定义

[英]geom_text() size definitions in ggplot2

I'm trying to vary the size of a geom_text() layer in a ggplot so that the labels are always narrower than a given range. 我正在尝试改变geom_text()图层的大小,以便标签总是比给定范围窄。 The ranges are defined in the data, but what I don't know is how to scale the label to be narrower than that, without a ton of trial and error. 范围是在数据中定义的,但我不知道的是如何将标签缩放到比这更窄,没有大量的试验和错误。

What I hope is that I can construct a function of label size and nchar(label) (realizing character width varies a bit) that would return a width that I could compare to the shape width, and scale down until no longer necessary. 我希望我可以构建一个标签大小和nchar(label)的函数(实现字符宽度变化一点),它将返回一个我可以与形状宽度比较的宽度,并缩小直到不再需要。

Are the ggplot label sizes defined as a number of pixels, percentage of the plot height, or something else like that? ggplot标签大小是否定义为像素数,绘图高度的百分比或类似的其他内容?

would this be a helpful place to start? 这会是一个有用的起点吗? (if not please feel free to delete my post). (如果没有,请随时删除我的帖子)。 You add your ranges to ranges = rnorm(foo, 5, 1) . 您将范围添加到ranges = rnorm(foo, 5, 1)

library(ggplot2)
library(directlabels)

set.seed(67)
foo <- 8
df <- data.frame(x = rnorm(foo, 1, .5), y=rnorm(foo, 1, .5), ranges = rnorm(foo, 5, 1), let=letters[1:foo])

p <- ggplot(df, aes(x, y, color=let)) + geom_point()  + scale_colour_brewer(palette=5)
direct.label(p, 
    list("top.points", rot=0, cex=df[,3], 
          fontface="bold", fontfamily="serif", alpha=0.8))          

小号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM