我有這個樣本資料。
structure(list(type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 6L, 6L, 6L, 6L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 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("1", "2", "3",
"4", "5", "6"), class = "factor"), category = c("fact", "fact",
"fact", "fact", "fact", "fact", "fake", "fake", "fake", "fake",
"fake", "fake", "fact", "fact", "fact", "fact", "fact", "fact",
"fake", "fake", "fake", "fake", "fake", "fake", "fact", "fact",
"fake", "fake", "fact", "fact", "fact", "fact", "fact", "fact",
"fake", "fake", "fake", "fake", "fake", "fake", "fact", "fact",
"fact", "fact", "fact", "fact", "fake", "fake", "fake", "fake",
"fake", "fake", "fact", "fact", "fact", "fact", "fact", "fact",
"fake", "fake", "fake", "fake", "fake", "fake"), group = c("TG1",
"TG1", "TG2", "TG2", "TG3", "TG3", "TG1", "TG1", "TG2", "TG2",
"TG3", "TG3", "TG1", "TG1", "TG2", "TG2", "TG3", "TG3", "TG1",
"TG1", "TG2", "TG2", "TG3", "TG3", "TG2", "TG2", "TG2", "TG2",
"TG1", "TG1", "TG2", "TG2", "TG3", "TG3", "TG1", "TG1", "TG2",
"TG2", "TG3", "TG3", "TG1", "TG1", "TG2", "TG2", "TG3", "TG3",
"TG1", "TG1", "TG2", "TG2", "TG3", "TG3", "TG1", "TG1", "TG2",
"TG2", "TG3", "TG3", "TG1", "TG1", "TG2", "TG2", "TG3", "TG3"
), cred = c("dislikes", "likes", "dislikes", "likes", "dislikes",
"likes", "dislikes", "likes", "dislikes", "likes", "dislikes",
"likes", "dislikes", "likes", "dislikes", "likes", "dislikes",
"likes", "dislikes", "likes", "dislikes", "likes", "dislikes",
"likes", "dislikes", "likes", "dislikes", "likes", "dislikes",
"likes", "dislikes", "likes", "dislikes", "likes", "dislikes",
"likes", "dislikes", "likes", "dislikes", "likes", "dislikes",
"likes", "dislikes", "likes", "dislikes", "likes", "dislikes",
"likes", "dislikes", "likes", "dislikes", "likes", "dislikes",
"likes", "dislikes", "likes", "dislikes", "likes", "dislikes",
"likes", "dislikes", "likes", "dislikes", "likes"), sum_possible = c(1256L,
1256L, 1236L, 1236L, 1238L, 1238L, 4396L, 4396L, 4326L, 4326L,
4333L, 4333L, 9420L, 9420L, 9270L, 9270L, 9285L, 9285L, 6908L,
6908L, 6798L, 6798L, 6809L, 6809L, 0L, 0L, 1236L, 1236L, 2512L,
2512L, 2472L, 2472L, 2476L, 2476L, 1884L, 1884L, 1854L, 1854L,
1857L, 1857L, 11304L, 11304L, 11124L, 11124L, 11142L, 11142L,
9420L, 9420L, 9270L, 9270L, 9285L, 9285L, 2512L, 2512L, 2472L,
2472L, 2476L, 2476L, 1884L, 1884L, 1854L, 1854L, 1857L, 1857L
), rate = c(0.02, 0.02, 0.02, 0.03, 0.05, 0.02, 0.05, 0.01, 0.05,
0.01, 0.08, 0, 0.08, 0.17, 0.07, 0.16, 0.08, 0.18, 0.14, 0.1,
0.11, 0.09, 0.18, 0.09, NaN, NaN, 0.08, 0.13, 0.05, 0.17, 0.04,
0.15, 0.05, 0.18, 0.26, 0.14, 0.22, 0.13, 0.36, 0.12, 0.02, 0.13,
0.02, 0.15, 0.03, 0.21, 0.11, 0.05, 0.13, 0.05, 0.19, 0.07, 0,
0.02, 0, 0.03, 0, 0.06, 0, 0, 0, 0, 0, 0), sum_clicks = c(27,
30, 19, 34, 57, 29, 221, 39, 196, 26, 331, 15, 718, 1591, 607,
1491, 710, 1694, 983, 696, 736, 578, 1196, 630, 0, 0, 103, 155,
126, 416, 91, 381, 129, 437, 495, 266, 414, 235, 671, 223, 235,
1515, 201, 1634, 379, 2373, 1083, 450, 1241, 418, 1740, 649,
3, 61, 1, 62, 10, 158, 2, 1, 2, 0, 5, 3)), row.names = c(NA,
-64L), class = c("tbl_df", "tbl", "data.frame"))
并想為每個類別和組繪制一個圖。
可以使用 facet_wrap 做到這一點:
library(ggplot2)
library(gridExtra)
library(gtable)
library(grid)
ggplot(sample, aes(x = type, y = rate, fill = cred))
geom_col()
labs(y = "Rate")
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
axis.title.x = element_blank(),
legend.title = element_blank())
facet_grid(rows = vars(category), cols = vars(group),
switch = 'y')
scale_fill_discrete(labels = c("Dislikes", "Likes"))
ylim(0, 0.5)
這使:

但是,只有 TG2 在變數中具有變數級別“6” type。這就是為什么我不希望“6”顯示在 x 軸的構面列 1 和 3 中。
我了解到,如果軸正在變化,我可能不想使用facet_grid
并繼續分別構建每個圖并使用grid.arrange
p1 <- ggplot(sample %>% filter(category == "fact", group == "TG1"), aes(x = type, y = rate, fill = cred))
geom_col()
labs(y = "Rate")
theme(axis.title = element_blank(),
legend.title = element_blank(),
#axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)
axis.text.x = element_blank(),
axis.ticks.x = element_blank()
)
facet_grid(rows = vars(category), cols = vars(group),
switch = 'y')
scale_fill_discrete(labels = c("Dislikes", "Likes"))
ylim(0, 0.5)
p2 <- ggplot(sample %>% filter(category == "fact", group == "TG2"), aes(x = type, y = rate, fill = cred))
geom_col(show.legend = FALSE)
labs(y = "Rate")
theme(axis.title = element_blank(),
legend.title = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank()
)
facet_grid(rows = vars(category), cols = vars(group),
switch = 'y')
scale_fill_discrete(labels = c("Dislikes", "Likes"))
ylim(0, 0.5)
p3 <- ggplot(sample %>% filter(category == "fact", group == "TG3"), aes(x = type, y = rate, fill = cred))
geom_col(show.legend = FALSE)
labs(y = "Rate")
theme(axis.title = element_blank(),
legend.title = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank()
)
facet_grid(rows = vars(category), cols = vars(group),
switch = 'y')
scale_fill_discrete(labels = c("Dislikes", "Likes"))
ylim(0, 0.5)
p4 <- ggplot(sample %>% filter(category == "fake", group == "TG1"), aes(x = type, y = rate, fill = cred))
geom_col(show.legend = FALSE)
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
axis.title = element_blank(),
strip.text.x = element_blank())
facet_grid(rows = vars(category), cols = vars(group),
switch = 'y')
scale_fill_discrete(labels = c("Dislikes", "Likes"))
ylim(0, 0.5)
p5 <- ggplot(sample %>% filter(category == "fake", group == "TG2"), aes(x = type, y = rate, fill = cred))
geom_col(show.legend = FALSE)
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
axis.title = element_blank(),
strip.text.x = element_blank())
facet_grid(rows = vars(category), cols = vars(group),
switch = 'y')
scale_fill_discrete(labels = c("Dislikes", "Likes"))
ylim(0, 0.5)
p6 <- ggplot(sample %>% filter(category == "fake", group == "TG3"), aes(x = type, y = rate, fill = cred))
geom_col(show.legend = FALSE)
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
axis.title = element_blank(),
strip.text.x = element_blank())
facet_grid(rows = vars(category), cols = vars(group),
switch = 'y')
scale_fill_discrete(labels = c("Dislikes", "Likes"))
ylim(0, 0.5)
legend = gtable_filter(ggplotGrob(p1), "guide-box")
grid.arrange(arrangeGrob(p1 theme(legend.position="none"),
p2,
p3,
p4,
p5,
p6,
nrow = 2,
left = textGrob("Click Rate", rot = 90, vjust = 1)
),
legend,
widths=unit.c(unit(1, "npc") - legend$width, legend$width),
nrow = 1)
這給了我:

在這里,我只有typeTG2 的“6”。但是,在我的真實示例中axis.text,實際上是完整的單詞,它們將裁剪圖形的底行,這使得它們的縮放比例與頂行大不相同。
我進一步嘗試提取axis.text并將其添加到下面的附加行中。
axis.ticks = gtable_filter(ggplotGrob(p4), "axis-b-1")
axis.ticks_tg2 = gtable_filter(ggplotGrob(p5), "axis-b-1")
grid.arrange(arrangeGrob(p1 theme(legend.position="none"),
p2,
p3,
p4 theme(axis.text.x = element_blank()),
p5 theme(axis.text.x = element_blank()),
p6 theme(axis.text.x = element_blank()),
nrow = 2,
left = textGrob("Rate", rot = 90, vjust = 1)
),
legend,
widths=unit.c(unit(1, "npc") - legend$width, legend$width),
arrangeGrob(axis.ticks,axis.ticks_tg2,axis.ticks, nrow = 1),
nrow = 2)
但是,這與我的坐標系不太匹配。

你有什么想法我怎樣才能得到我喜歡的解決方案:
x 軸文本僅位于網格底部,如中所示facet_grid,但每列的 x 軸刻度數不同grid.arrange?
非常感謝您的幫助!
uj5u.com熱心網友回復:
而不是grid.arrange一種選擇是使用釋放 x 比例facet_grid(...scales = "free_x"):
library(ggplot2)
ggplot(sample, aes(x = type, y = rate, fill = cred))
geom_col()
labs(y = "Rate")
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
axis.title.x = element_blank(),
legend.title = element_blank())
facet_grid(rows = vars(category), cols = vars(group),
switch = 'y', scales = "free_x")
scale_fill_discrete(labels = c("Dislikes", "Likes"))
ylim(0, 0.5)
#> Warning: Removed 2 rows containing missing values (position_stack).

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/440264.html
