如何在ggplot2中将斜体用于构面标签? [英] How to use italics for facet labels in ggplot2?

查看:766
本文介绍了如何在ggplot2中将斜体用于构面标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对ggplot2中的方面名称使用斜体.我首先要订购和重命名构面.我遇到了潜在解决方案,但这需要labeller=label_parsed,而我正在使用labeller=name_labeller重命名我的构面.

I am trying to use italics for facet names in ggplot2. I am first ordering and renaming the facets. I have come across a potential solution, but this requires labeller=label_parsed and I am using labeller=name_labeller to rename my facets.

在以下示例中,我希望将构面名称一个",两个",三个",四个"和五个"斜体化.这是一个示例数据框:

In the following example, I would like the facet names "one" "two" "three" "four" and "five" to be italicized. Here is an example dataframe:

structure(list(Names = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 
5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L), Other = c(5L, 
3L, 2L, 6L, 5L, 4L, 5L, 4L, 5L, 3L, 2L, 6L, 5L, 4L, 5L, 4L, 4L, 
5L, 3L, 2L), X = c(0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L, 
0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L), Y = c(0L, 10L, 
0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L, 
0L, 10L, 0L, 10L)), .Names = c("Names", "Other", "X", "Y"), class = "data.frame", row.names = c(NA, 
-20L))

生成图的代码是

library(ggplot2)
library(grid)
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)



facets <- c("1", "2", "3", "4", "5")

names <- list(
  '1'="one",
  '2'="two",
  '3'="three",
  '4'="four",
  '5'="five"
)

name_labeller <- function(variable,value){
  return(names[value])
}

ggplot(FacetTestData[FacetTestData$Names %in% facets,], aes(y = Y, x = X, group = Names)) + 
  geom_point(shape = 21, size=3) + 
  scale_fill_manual(values=c("gray90","gray40")) + 
  geom_smooth(method="lm", se= FALSE, size = 1) +
  scale_color_manual(values=c("black","black")) +
  geom_smooth(method = 'lm', size = 1, colour = 'red', se = FALSE)  +
  facet_grid(Names ~ ., labeller=name_labeller)

以下代码似乎为列表names

levels(names) <- c("italic('one')", "italic('two')", "italic('three')", "italic('four')",    "italic('five')")

,但是我不太清楚如何使用facet_grid()进行此工作.有人知道我该怎么做吗?

but I can't quite figure out how to make this work with facet_grid(). Does anyone know how I can do this?

推荐答案

按以下方式工作应该可行:

Something along these lines should work:

... + theme(strip.text = element_text(face = "italic"))

有关theme()的更多详细信息,请参见文档.

See the docs for more detail about theme().

这篇关于如何在ggplot2中将斜体用于构面标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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