如何操作facet_grid图的条带文本?

发布于 2024-08-31 04:12:20 字数 655 浏览 0 评论 0原文

我想知道如何操纵分面图中条带文本的大小。我的问题 类似于 关于情节标题的问题,但我特别关心 操作的不是绘图标题,而是出现在分面标题 (strip_h) 中的文本。

例如,考虑 mpg 数据集。

    library(ggplot2) 
    qplot(hwy, cty, data = mpg) + facet_grid( . ~ manufacturer)

生成的输出会生成一些不适合条带的分面标题。

我想一定有一种方法可以使用grid来处理条带文本。但我是 还是个新手,从 Hadley 的 grid 附录中不确定预订如何, 准确地说,就是去做。

I'm wondering how I can manipulate the size of strip text in facetted plots. My question
is similar to a question on plot titles, but I'm specifically concerned with
manipulating not the plot title but the text that appears in facet titles (strip_h).

As an example, consider the mpg dataset.

    library(ggplot2) 
    qplot(hwy, cty, data = mpg) + facet_grid( . ~ manufacturer)

The resulting output produces some facet titles that don't fit in the strip.

I'm thinking there must be a way to use grid to deal with the strip text. But I'm
still a novice and wasn't sure from the grid appendix in Hadley's book how,
precisely, to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一影成城 2024-09-07 04:12:20

您可以使用theme_text()修改strip.text.x(或strip.text.y),例如

qplot(hwy, cty, data = mpg) + 
      facet_grid(. ~ manufacturer) + 
      opts(strip.text.x = theme_text(size = 8, colour = "red", angle = 90))

更新:< /strong> ggplot2 版本 > 0.9.1

qplot(hwy, cty, data = mpg) + 
      facet_grid(. ~ manufacturer) + 
      theme(strip.text.x = element_text(size = 8, colour = "red", angle = 90))

You can modify strip.text.x (or strip.text.y) using theme_text(), for instance

qplot(hwy, cty, data = mpg) + 
      facet_grid(. ~ manufacturer) + 
      opts(strip.text.x = theme_text(size = 8, colour = "red", angle = 90))

Update: for ggplot2 version > 0.9.1

qplot(hwy, cty, data = mpg) + 
      facet_grid(. ~ manufacturer) + 
      theme(strip.text.x = element_text(size = 8, colour = "red", angle = 90))
薄荷梦 2024-09-07 04:12:20

如今,optstheme_text 的使用似乎已被弃用。 R 建议使用 themeelement_text。可以在这里找到答案的解决方案: http:// /wiki.stdout.org/rcookbook/Graphs/Facets%20%28ggplot2%29/#modifying-facet-label-text

qplot(hwy, cty, data = mpg) + 
      facet_grid(. ~ manufacturer) + 
      theme(strip.text.x = element_text(size = 8, colour = "red", angle = 90))

Nowadays the usage of opts and theme_text seems to be deprecated. R suggests to use theme and element_text. A solution to the answer can be found here: http://wiki.stdout.org/rcookbook/Graphs/Facets%20%28ggplot2%29/#modifying-facet-label-text

qplot(hwy, cty, data = mpg) + 
      facet_grid(. ~ manufacturer) + 
      theme(strip.text.x = element_text(size = 8, colour = "red", angle = 90))
十雾 2024-09-07 04:12:20

我想在 mpg 的示例中,更改旋转角度和字体大小很好,但在许多情况下,您可能会发现自己的变量具有相当长的标签,这可能会成为一个令人头疼的问题(从字面上看)尝试读取旋转的冗长标签。

因此,除了(或补充)改变角度和大小之外,我通常会重新格式化定义facet_grid的因素的标签,只要它们可以以有意义的方式分割。

通常,如果我有一个带有字符串的 dataset$variable ,看起来就像

c("median_something", "aggregated_average_x","error","something_else")

我所做的那样:

reformat <– function(x,lab="\n"){ sapply(x, function(c){ paste(unlist(strsplit(as.character(c) , split="_")),collapse=lab) }) }

[也许有更好的 reformat 定义,但至少这个可以正常工作。

dataset$variable <- factor(dataset$variable, labels=reformat(dataset$variable, lab='\n')

]分面,所有标签都将非常可读:

ggplot(data=dataset, aes(x,y)) + geom_point() + facet_grid(. ~ variable)

I guess in the example of mpg changing the rotation angle and font size is fine, but in many cases you might find yourself with variables that have quite lengthy labels, and it can become a pain in the neck (literally) to try read rotated lengthy labels.

So in addition (or complement) to changing angles and sizes, I usually reformat the labels of the factors that define the facet_grid whenever they can be split in a way that makes sense.

Typically if I have a dataset$variable with strings that looks like

c("median_something", "aggregated_average_x","error","something_else")

I simply do:

reformat <– function(x,lab="\n"){ sapply(x, function(c){ paste(unlist(strsplit(as.character(c) , split="_")),collapse=lab) }) }

[perhaps there are better definitions of reformat but at least this one works fine.]

dataset$variable <- factor(dataset$variable, labels=reformat(dataset$variable, lab='\n')

And upon facetting, all labels will be very readable:

ggplot(data=dataset, aes(x,y)) + geom_point() + facet_grid(. ~ variable)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文