gpt4 book ai didi

r - 生成每个图 block 具有多个图例类别(填充)的 geom_tile 图

转载 作者:行者123 更新时间:2023-12-04 09:37:34 25 4
gpt4 key购买 nike

我正在为下面共享的数据创建一个平铺图。对于每个磁贴,可能有多种失败原因,我希望磁贴在网格中显示多种颜色。

> coupler.graph
# A tibble: 34 x 3
`Bar Size` Category `Mode of Failure`
<chr> <chr> <chr>
1 No. 4 SSC SMA bar fractured inside grip
2 No. 4 SSC Bar fracture
3 No. 6 SSC Bar pullout
4 No. 6 SSC Bar fracture
5 No. 6 SSC Bar fracture
6 No. 6 GSC Bar fracture
7 No. 6 GSC GC fracture
8 No. 6 GSC Thread failure
9 No. 8 SSC Bar pullout
10 No. 8 SSC Bar fracture
# ... with 24 more rows
我使用以下代码:
ggplot(coupler.graph) +
aes(x = Category, y = fct_inorder(`Bar Size`), fill = `Mode of Failure`) +
geom_tile(size = 1L) + theme_classic() + scale_fill_hue() +
labs(x = "Splicer Type", y = "Bar Size", title = "Mechanical Coupler Research Summary") +
theme(plot.title = element_text(hjust = 0.5,
margin = margin(10,0,20,0), face = "bold", size = 24),
axis.title.y = element_text(size = 14, margin = margin(t = 0, r = 20, b = 0, l = 0)),
axis.title.x = element_text(size = 14, margin = margin(t = 15, r = 0, b = 0, l = 0)),
legend.title = element_text(size = 16))
现在它们只是相互重叠。我怎样才能解决这个问题?
enter image description here
> dput(coupler.graph)
structure(list(Category = c("SSC", "SSC", "SSC", "SSC", "GSC",
"SSC", "SSC", "HBC", "GSC", "GSC", "BSC", "SSC", "HBC", "GSC",
"TC", "BSC", "HBC", "GSC", "GSC", "GSC", "TC", "BSC"), `No. Bars` = c(4,
4, 80, 5, 7, 80, 10, 4, 9, 6, 3, 9, 9, 9, 18, 10, 10, 10, 8,
4, 4, 4), `Bar Size` = c("No. 4", "No. 4", "No. 6", "No. 6",
"No. 6", "No. 8", "No. 8", "No. 8", "No. 8", "No. 8", "No. 8",
"No. 10", "No. 10", "No. 10", "No. 10", "No. 10", "No. 10", "No. 10",
"No. 11", "No. 18", "No. 18", "No. 18")), row.names = c(NA, -22L
), class = c("tbl_df", "tbl", "data.frame"))
谢谢

最佳答案

至少接近解决方案的一种方法是按类别分面。尝试这个:

library(ggplot2)
coupler.graph <- read.table(text='row "Bar Size" Category "Mode of Failure"
1 "No. 4" SSC "SMA bar fractured inside grip"
2 "No. 4" SSC "Bar fracture"
3 "No. 6" SSC "Bar pullout"
4 "No. 6" SSC "Bar fracture"
5 "No. 6" SSC "Bar fracture"
6 "No. 6" GSC "Bar fracture"
7 "No. 6" GSC "GC fracture"
8 "No. 6" GSC "Thread failure"
9 "No. 8" SSC "Bar pullout"
10 "No. 8" SSC "Bar fracture"', header = TRUE)

ggplot(coupler.graph, aes(x = Mode.of.Failure, y = forcats::fct_inorder(Bar.Size), fill = Mode.of.Failure)) +
geom_tile(size = 1L, color = "white") +
theme_classic() +
scale_fill_hue() +
scale_x_discrete(expand = expansion(mult = 0.01)) +
facet_wrap(~Category, nrow = 1, scales = "free_x") +
theme(panel.spacing.x = unit(0, "pt"),
axis.text.x = element_blank(),
axis.ticks.x = element_blank())

创建于 2020-06-21 由 reprex package (v0.3.0)

关于r - 生成每个图 block 具有多个图例类别(填充)的 geom_tile 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62493665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com