ggplot facet_grid x 轴步进
ggplot facet_grid x axis stepped
我在使用 facet_grid 对齐分类 x 轴标签时遇到问题
它没有对齐,我不知道如何强制它对齐。
我的代码:
ggplot(data=dataset, aes(x=Question)) +
facet_grid(~Indicator,scales = "free_x")+
scale_x_discrete(labels = function(x) lapply(strwrap(x, width = 45, simplify = FALSE),paste, collapse="\n"))+
coord_cartesian(ylim = c(20, 90))+
geom_line(aes(y = Result))+ geom_point(aes(y = Result))+
geom_line(aes(y = Result_2))+ geom_point(aes(y = Result_2))+
theme_minimal()+
theme(
axis.title.x=element_blank(),
axis.text.x=element_text(angle = 90, hjust = 0, vjust = 0.5))
数据:
structure(list(Question = structure(c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 32L, 33L, 34L, 35L, 36L, 37L,
38L, 39L, 27L, 28L, 29L, 30L, 31L, 40L, 44L, 45L, 46L, 47L, 48L,
49L, 50L, 51L, 41L, 42L, 43L), .Label = c("A.1 long label", "A.2 long label",
"A.3 long label", "A.4 long label", "A.5 long label", "A.6 long label",
"A.7 long label", "A.8 long label", "B.1 very long label", "B.2 very long label",
"B.3 very long label", "B.4 very long label", "B.5 very long label",
"B.6 very long label", "B.7 very long label", "B.8 very long label",
"C.1 very long label", "C.2 very long label", "C.3 very long label",
"C.4 very long label", "C.5 very long label", "C.6 very long label",
"C.7 very long label", "C.8 very long label", "C.9 very long label",
"D.1 very very long label", "D.10 very very long label", "D.11 very very long label",
"D.12 very very long label", "D.13 very very long label", "D.14 very very long label",
"D.2 very very long label", "D.3 very very long label", "D.4 very very long label",
"D.5 very very long label", "D.6 very very long label", "D.7 very very long label",
"D.8 very very long label", "D.9 very very long label", "E.1 very very long label",
"E.10 very very long label", "E.11 very very long label", "E.12 very very long label",
"E.2 very very long label", "E.3 very very long label", "E.4 very very long label",
"E.5 very very long label", "E.6 very very long label", "E.7 very very long label",
"E.8 very very long label", "E.9 very very long label"), class = "factor"),
Indicator = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("A",
"B", "C", "D", "E"), class = "factor"), Index = c(47L, 44L,
48L, 46L, 50L, 45L, 49L, 51L, 37L, 36L, 38L, 39L, 40L, 41L,
43L, 42L, 30L, 28L, 32L, 27L, 29L, 31L, 33L, 34L, 35L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 23L, 13L, 22L, 25L, 26L,
24L, 4L, 5L, 2L, 3L, 1L, 7L, 9L, 8L, 10L, 12L, 6L, 11L),
Result = c(67.1, 62.9, 50.7, 59, 56.4, 50.4, 41.6, 65.9,
82.4, 84.8, 83.2, 87.1, 85.6, 79.3, 86.8, 78.9, 62, 73.7,
70.3, 81, 77, 55.1, 64.2, 62.2, 64.1, 62.7, 64.8, 62.3, 60.5,
49.4, 50.6, 72.6, 43.7, 49.3, 73.4, 56, 54.4, 56.1, 58.4,
72.1, 66.7, 78.2, 79.6, 80.7, 65.6, 59.5, 66.4, 53.5, 68,
76.3, 70.8), Result_2 = c(54.8, 61.4, 52.1, 58.5, 63.8, 47.4,
50.4, 69.8, 77.7, 79.3, 78.8, 82.1, 80.3, 72.6, 84.1, 74.2,
59.8, 68.6, 65.3, 75.8, 71.1, 49.1, 61.5, 53.3, 55.4, 54.4,
57.2, 57.5, 53.7, 41.2, 40.2, 65, 31.2, 37.3, 62.2, 47.3,
45.2, 47.2, 42.8, 65.5, 58.7, 70.6, 72.5, 73.9, 57.5, 52.6,
60.7, 44.3, 63.6, 69.7, 63.5)), class = "data.frame", row.names = c(NA,
-51L))
无论我怎么格式化,标签似乎都是阶梯式的
根据要求 - 使用来自
的适度 hack
我也稍微修改了你的剧情代码。我建议看看并了解优势是什么 - 最重要的是将数据转换为长格式。其他基于便利的更改是在 ggplot 主调用中定义美学。
geom_text
调用的数据框需要相同的值/变量来进行分面!
我也可以提出一个建议 - 如果你的每个类别的每个 x 都有相同的长标签 - 你为什么不把这个标签放到 facet strip 中,然后每个 x 只能有索引作为一个标签。长 x 标签并不能真正帮助可视化。如果翻转图表,长标签通常更容易阅读,所以它们会是水平的,但在大多数情况下,人们真的不需要长 x 标签。
library(tidyverse)
ann_x <- data.frame(Question = unique(dataset$Question), Indicator = str_sub(unique(dataset$Question), 1, 1))
dataset_l <- dataset %>% pivot_longer(names_to = "key", values_to = "value", cols = contains("result"))
ggplot(data = dataset_l, aes(x = Question, y = value)) +
geom_line(aes(group = key)) +
geom_point() +
geom_text(data = ann_x, aes(y = -35, label = Question), angle = 90, hjust = 0, size = 5 / 14 * 10) +
facet_grid(~Indicator, scales = "free_x") +
coord_cartesian(ylim = c(20, 90), clip = "off") +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
plot.margin = margin(0.1, 0.1, 2, 0.1, "in")
)
由 reprex package (v0.3.0)
于 2020 年 3 月 15 日创建
我在使用 facet_grid 对齐分类 x 轴标签时遇到问题 它没有对齐,我不知道如何强制它对齐。
我的代码:
ggplot(data=dataset, aes(x=Question)) +
facet_grid(~Indicator,scales = "free_x")+
scale_x_discrete(labels = function(x) lapply(strwrap(x, width = 45, simplify = FALSE),paste, collapse="\n"))+
coord_cartesian(ylim = c(20, 90))+
geom_line(aes(y = Result))+ geom_point(aes(y = Result))+
geom_line(aes(y = Result_2))+ geom_point(aes(y = Result_2))+
theme_minimal()+
theme(
axis.title.x=element_blank(),
axis.text.x=element_text(angle = 90, hjust = 0, vjust = 0.5))
数据:
structure(list(Question = structure(c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 32L, 33L, 34L, 35L, 36L, 37L,
38L, 39L, 27L, 28L, 29L, 30L, 31L, 40L, 44L, 45L, 46L, 47L, 48L,
49L, 50L, 51L, 41L, 42L, 43L), .Label = c("A.1 long label", "A.2 long label",
"A.3 long label", "A.4 long label", "A.5 long label", "A.6 long label",
"A.7 long label", "A.8 long label", "B.1 very long label", "B.2 very long label",
"B.3 very long label", "B.4 very long label", "B.5 very long label",
"B.6 very long label", "B.7 very long label", "B.8 very long label",
"C.1 very long label", "C.2 very long label", "C.3 very long label",
"C.4 very long label", "C.5 very long label", "C.6 very long label",
"C.7 very long label", "C.8 very long label", "C.9 very long label",
"D.1 very very long label", "D.10 very very long label", "D.11 very very long label",
"D.12 very very long label", "D.13 very very long label", "D.14 very very long label",
"D.2 very very long label", "D.3 very very long label", "D.4 very very long label",
"D.5 very very long label", "D.6 very very long label", "D.7 very very long label",
"D.8 very very long label", "D.9 very very long label", "E.1 very very long label",
"E.10 very very long label", "E.11 very very long label", "E.12 very very long label",
"E.2 very very long label", "E.3 very very long label", "E.4 very very long label",
"E.5 very very long label", "E.6 very very long label", "E.7 very very long label",
"E.8 very very long label", "E.9 very very long label"), class = "factor"),
Indicator = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("A",
"B", "C", "D", "E"), class = "factor"), Index = c(47L, 44L,
48L, 46L, 50L, 45L, 49L, 51L, 37L, 36L, 38L, 39L, 40L, 41L,
43L, 42L, 30L, 28L, 32L, 27L, 29L, 31L, 33L, 34L, 35L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 23L, 13L, 22L, 25L, 26L,
24L, 4L, 5L, 2L, 3L, 1L, 7L, 9L, 8L, 10L, 12L, 6L, 11L),
Result = c(67.1, 62.9, 50.7, 59, 56.4, 50.4, 41.6, 65.9,
82.4, 84.8, 83.2, 87.1, 85.6, 79.3, 86.8, 78.9, 62, 73.7,
70.3, 81, 77, 55.1, 64.2, 62.2, 64.1, 62.7, 64.8, 62.3, 60.5,
49.4, 50.6, 72.6, 43.7, 49.3, 73.4, 56, 54.4, 56.1, 58.4,
72.1, 66.7, 78.2, 79.6, 80.7, 65.6, 59.5, 66.4, 53.5, 68,
76.3, 70.8), Result_2 = c(54.8, 61.4, 52.1, 58.5, 63.8, 47.4,
50.4, 69.8, 77.7, 79.3, 78.8, 82.1, 80.3, 72.6, 84.1, 74.2,
59.8, 68.6, 65.3, 75.8, 71.1, 49.1, 61.5, 53.3, 55.4, 54.4,
57.2, 57.5, 53.7, 41.2, 40.2, 65, 31.2, 37.3, 62.2, 47.3,
45.2, 47.2, 42.8, 65.5, 58.7, 70.6, 72.5, 73.9, 57.5, 52.6,
60.7, 44.3, 63.6, 69.7, 63.5)), class = "data.frame", row.names = c(NA,
-51L))
无论我怎么格式化,标签似乎都是阶梯式的
根据要求 - 使用来自
我也稍微修改了你的剧情代码。我建议看看并了解优势是什么 - 最重要的是将数据转换为长格式。其他基于便利的更改是在 ggplot 主调用中定义美学。
geom_text
调用的数据框需要相同的值/变量来进行分面!
我也可以提出一个建议 - 如果你的每个类别的每个 x 都有相同的长标签 - 你为什么不把这个标签放到 facet strip 中,然后每个 x 只能有索引作为一个标签。长 x 标签并不能真正帮助可视化。如果翻转图表,长标签通常更容易阅读,所以它们会是水平的,但在大多数情况下,人们真的不需要长 x 标签。
library(tidyverse)
ann_x <- data.frame(Question = unique(dataset$Question), Indicator = str_sub(unique(dataset$Question), 1, 1))
dataset_l <- dataset %>% pivot_longer(names_to = "key", values_to = "value", cols = contains("result"))
ggplot(data = dataset_l, aes(x = Question, y = value)) +
geom_line(aes(group = key)) +
geom_point() +
geom_text(data = ann_x, aes(y = -35, label = Question), angle = 90, hjust = 0, size = 5 / 14 * 10) +
facet_grid(~Indicator, scales = "free_x") +
coord_cartesian(ylim = c(20, 90), clip = "off") +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
plot.margin = margin(0.1, 0.1, 2, 0.1, "in")
)
由 reprex package (v0.3.0)
于 2020 年 3 月 15 日创建