我试图在ggplot2中使用斜体名称的斜体。我首先订购并重命名方面。我遇到了potential solution,但这需要labeller=label_parsed
我正在使用labeller=name_labeller
来重命名我的方面。
在下面的例子中,我希望facet名称“one”“two”“three”“four”和“five”用斜体字表示。以下是一个示例数据框:
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()
来完成这项工作。有谁知道我怎么能这样做?
答案 0 :(得分:12)
答案 1 :(得分:0)
您可以使用krlmlr中的代码并修改
来分离轴图形... + theme(strip.text = ...
到
... + theme(strip.text.x = ...
OR
... + theme(strip.text.y = ...