Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

r - 如何在ggplot2中使用斜体作为方面标签?(How to use italics for facet labels in ggplot2?)

I am trying to use italics for facet names in ggplot2.

(我正在尝试对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.

(我遇到了一个潜在的解决方案 ,但这需要labeller=label_parsed并且我正在使用labeller=label_parsed labeller=name_labeller重命名我的构面。)

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))

and the code to produce the plot is

(产生剧情的代码是)

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)

The following code appears to give italics attributes to the list names

(以下代码似乎为列表names赋予了斜体属性)

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

but I can't quite figure out how to make this work with facet_grid() .

(但是我不太想知道如何使用facet_grid()来完成这项工作。)

Does anyone know how I can do this?

(有人知道我该怎么做吗?)

  ask by Thomas translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Something along these lines should work:

(遵循以下原则应该可以:)

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

See the docs for more detail about theme() .

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...