作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了一个多面图(使用 ggplot2::facet_grid
),我在其中切换了 y 轴,所以 y 条文本绘制在图的左侧并且已经使用了 theme(strip.placement = "outside")
将 strip 文本放置在 y 轴值之外。但是, strip 标题绘制在 y 轴名称和 y 轴值之间。我想在 y 轴名称外部 绘制 strip 文本,以便将 y 轴名称绘制在 y 轴值旁边。这可能吗?
这是我的部分数据:
df<-structure(list(place = c("Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site1", "Site1", "Site1", "Site1", "Site1",
"Site1", "Site1", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2", "Site2", "Site2", "Site2",
"Site2", "Site2", "Site2", "Site2"), year = c(2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 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8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L), perc = c(0.01, 0.16, 0.06, 0.02, 0,
0, 0.01, 0.05, 0.17, 0.01, 0, 0.02, 0.17, 0.05, 0.02, 0, 0, 0.07,
0.15, 0.01, 0, 0, 0, 0.04, 0.03, 0.14, 0.02, 0.01, 0, 0, 0.01,
0.03, 0.14, 0.06, 0, 0, 0, 0, 0.02, 0.12, 0.03, 0.03, 0, 0, 0,
0, 0.02, 0.11, 0.05, 0.01, 0, 0, 0.02, 0.03, 0.13, 0.01, 0.01,
0, 0, 0, 0, 0.05, 0.08, 0.05, 0.01, 0, 0, 0, 0.04, 0.08, 0.05,
0.02, 0, 0, 0, 0.08, 0.09, 0.01, 0.01, 0, 0, 0, 0, 0.03, 0.14,
0.04, 0.03, 0.01, 0.01, 0, 0, 0.1, 0.1, 0.05, 0, 0, 0, 0, 0.11,
0.11, 0.02, 0.01, 0, 0, 0, 0, 0.03, 0.14, 0.07, 0.02, 0, 0, 0,
0.05, 0.02, 0.16, 0, 0.01, 0, 0, 0, 0, 0.08, 0.1, 0.06, 0.01,
0, 0, 0, 0.01, 0.04, 0.02, 0.03, 0.08, 0.02, 0.01, 0, 0, 0, 0,
0, 0, 0, 0, 0.02, 0.01, 0.05, 0.06, 0.04, 0, 0, 0, 0, 0, 0, 0,
0, 0.02, 0.06, 0.09, 0.02, 0, 0, 0, 0, 0, 0, 0.01, 0.02, 0.05,
0.06, 0.04, 0, 0, 0, 0.01, 0.01, 0.07, 0.07, 0.04, 0.01, 0, 0,
0, 0, 0.01, 0.04, 0.07, 0.04, 0.03, 0, 0, 0, 0, 0, 0.16, 0.03,
0.06, 0, 0, 0.15, 0.01, 0.08, 0.01, 0.11, 0.07, 0.07, 0, 0.09,
0.11, 0.04, 0, 0, 0.04, 0.11, 0.08, 0.02, 0, 0, 0, 0.06, 0.12,
0.05, 0.02, 0.01, 0, 0, 0.05, 0.08, 0.05, 0.02, 0.01, 0, 0.08,
0.07, 0.04, 0.01, 0, 0, 0, 0.04, 0.09, 0.05, 0.02, 0, 0, 0, 0.02,
0.06, 0.07, 0.03, 0.01, 0, 0, 0.01, 0.05, 0.07, 0.05, 0.02, 0,
0, 0, 0.01, 0.07, 0.05, 0.05, 0.01, 0, 0, 0, 0.01, 0.07, 0.08,
0.06, 0.03, 0, 0, 0, 0.04, 0.07, 0.06, 0.06, 0.02, 0, 0, 0.03,
0.09, 0.08, 0.03, 0.01, 0, 0, 0.04, 0.09, 0.08, 0.03, 0, 0, 0,
0.06, 0.08, 0.07, 0.03, 0.01, 0, 0, 0.03, 0.08, 0.07, 0.05, 0.01,
0, 0, 0, 0.03, 0.06, 0.05, 0.03, 0.02, 0.01, 0, 0, 0, 0, 0.02,
0.06, 0.05, 0.04, 0.02, 0.01, 0, 0, 0, 0, 0.03, 0.08, 0.05, 0.02,
0.01, 0, 0, 0, 0, 0.02, 0.05, 0.05, 0.03, 0.03, 0.02, 0.01, 0,
0, 0, 0, 0, 0, 0.02, 0.04, 0.05, 0.04, 0.02, 0.01, 0.01, 0, 0,
0, 0, 0.01, 0.03, 0.05, 0.05, 0.03, 0.02, 0, 0, 0, 0, 0, 0),
category = c("0", "1", "2", "3", "4", "0", "1", "2", "3",
"4", "5", "0", "1", "2", "3", "4", "0", "1", "2", "3", "4",
"5", "6", "0", "1", "2", "3", "4", "5", "0", "1", "2", "3",
"4", "5", "6", "7 - 14", "0", "1", "2", "3", "4", "5", "6",
"0", "1", "2", "3", "4", "5", "6", "7 - 14", "0", "1", "2",
"3", "4", "5", "6", "0", "1", "2", "3", "4", "5", "6", "7 - 14",
"0", "1", "2", "3", "4", "5", "6", "0", "1", "2", "3", "4",
"5", "6", "7 - 14", "0", "1", "2", "3", "4", "5", "6", "7 - 14",
"0", "1", "2", "3", "4", "5", "6", "0", "1", "2", "3", "4",
"5", "6", "0", "1", "2", "3", "4", "5", "6", "7 - 14", "0",
"1", "2", "3", "4", "5", "6", "7 - 14", "7 - 14", "0", "1",
"2", "3", "4", "5", "6", "0", "1", "2", "3", "4", "5", "6",
"7 - 14", "7 - 14", "7 - 14", "7 - 14", "7 - 14", "7 - 14",
"7 - 14", "0", "1", "2", "3", "4", "5", "6", "7 - 14", "7 - 14",
"7 - 14", "7 - 14", "7 - 14", "7 - 14", "0", "1", "2", "3",
"4", "5", "6", "7 - 14", "7 - 14", "7 - 14", "7 - 14", "1",
"2", "3", "4", "5", "6", "7 - 14", "7 - 14", "7 - 14", "7 - 14",
"1", "2", "3", "4", "5", "6", "7 - 14", "7 - 14", "1", "2",
"3", "4", "5", "6", "7 - 14", "7 - 14", "7 - 14", "7 - 14",
"7 - 14", "0", "1", "2", "3", "4", "0", "1", "2", "3", "0",
"1", "2", "3", "0", "1", "2", "3", "4", "0", "1", "2", "3",
"4", "5", "6", "0", "1", "2", "3", "4", "5", "6", "0", "1",
"2", "3", "4", "5", "0", "1", "2", "3", "4", "5", "6", "0",
"1", "2", "3", "4", "5", "6", "0", "1", "2", "3", "4", "5",
"6", "0", "1", "2", "3", "4", "5", "6", "7 - 14", "0", "1",
"2", "3", "4", "5", "6", "7 - 14", "0", "1", "2", "3", "4",
"5", "6", "7 - 14", "0", "1", "2", "3", "4", "5", "6", "0",
"1", "2", "3", "4", "5", "6", "0", "1", "2", "3", "4", "5",
"6", "0", "1", "2", "3", "4", "5", "6", "0", "1", "2", "3",
"4", "5", "6", "7 - 14", "0", "1", "2", "3", "4", "5", "6",
"7 - 14", "7 - 14", "7 - 14", "0", "1", "2", "3", "4", "5",
"6", "7 - 14", "7 - 14", "7 - 14", "0", "1", "2", "3", "4",
"5", "6", "7 - 14", "7 - 14", "0", "1", "2", "3", "4", "5",
"6", "7 - 14", "7 - 14", "7 - 14", "7 - 14", "0", "1", "2",
"3", "4", "5", "6", "7 - 14", "7 - 14", "7 - 14", "7 - 14",
"7 - 14", "7 - 14", "0", "1", "2", "3", "4", "5", "6", "7 - 14",
"7 - 14", "7 - 14", "7 - 14", "7 - 14")), row.names = c(NA,
-379L), class = c("tbl_df", "tbl", "data.frame"))
这是我用来构建绘图的代码。
library(ggplot2)
library(lemon)
ggplot(df,aes(x = month,
y = perc,
fill = category)) +
geom_col(position = "fill") +
facet_rep_grid(place ~ year ,
repeat.tick.labels = T,
switch = "y") +
theme_minimal() +
theme(strip.text.y = element_text(angle = 180),
strip.placement = "outside")
由此产生的情节:
最后,这是想要的情节:
最佳答案
我认为 ggplot2 内没有办法, 但它可以通过额外的包来完成。
library(ggplot2)
library(lemon)
library(cowplot)
library(grid)
g <- ggplot(df,aes(x = month,
y = perc,
fill = category)) +
geom_col(position = "fill") +
facet_rep_grid(place ~ year ,
repeat.tick.labels = T) +
theme_minimal() +
theme(strip.text.y = element_blank())
a <- textGrob('Site 1', gp=gpar(fontsize=10))
b <- textGrob('Site 2', gp=gpar(fontsize=10))
plot_grid(plot_grid(a, b, ncol = 1), g, rel_widths = c(0.1, 1))
关于r - 有没有办法在 facet_grid 图中的 y 轴名称之外绘制小平面条名称(切换 y)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60009107/
我是一名优秀的程序员,十分优秀!