Reputation: 478
I am creating a tile plot for my data shared below. For each tile, there could multiple reasons of failure and I want the tiles to show multiple colors indicating that in a grid.
> 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
I use the following code:
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))
Right now they are just overlapping each other. How can I fix this?
> 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"))
Thank you
Upvotes: 2
Views: 798
Reputation: 124148
One approach to get at least close to a solution is to facet by category. Try this:
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())
Created on 2020-06-21 by the reprex package (v0.3.0)
Upvotes: 2