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

标签 r ggplot2

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

> 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())

reprex package于2020年6月21日创建(v0.3.0)

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

相关文章:

r - 直接使用 @ 访问 S4 对象插槽是不好的做法吗?

r - Shiny 的仪表板和 Shiny 的主题?

r - 使用data.table计数和汇总/汇总列

r - 如何计算 R 中的 7 天移动平均线?

r - 绘制更平滑的多条线

r - 使用ggplot突出周末?

r - 地圈/dplyr : create matrix of distance between coordinates

r - 增加辅助 y 轴与其标题之间的间距

r - 由annotation_custom使用geom_bar图创建的移动表

r - ggplot轴标题中斜体和上标的表达