I found the following example that explains how to add patterns to the geom_tile plot but I cannot get my plot to show by following the steps. Attached is a few rows of the data:
DATA:
> dput(coupler.graph)
structure(list(Category = c("HBC", "TC", "BSC", "GSC", "GSC",
"SSC", "SSC", "GSC", "GSC", "SSC", "SSC", "SSC", "HBC", "TC",
"BSC", "BSC", "GSC", "GSC", "SSC", "HBC", "HBC", "TC", "TC",
"BSC", "GSC", "GSC", "GSC", "GSC", "GSC", "GSC", "TC", "BSC",
"BSC", "GSC", "GSC"), `Bar Size` = c("No. 5", "No. 5", "No. 5",
"No. 5", "No. 5", "No. 6", "No. 6", "No. 6", "No. 6", "No. 8",
"No. 8", "No. 8", "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. 10", "No. 10", "No. 11", "No. 11", "No. 14", "No. 18",
"No. 18", "No. 18", "No. 18", "No. 18"), `No. Bars` = c(3, 9,
3, 4, 2, 42.5, 42.5, 3, 4, 30, 30, 30, 7, 9, 5, 1, 10, 11, 9,
11, 11, 7, 20, 13, 5, 10, 10, 4, 4, 0, 4, 2, 2, 3, 1), Failure = c("Bar fracture",
"Bar fracture", "Bar fracture", "Bar pullout", "Bar fracture",
"Bar pullout", "Bar fracture", "Coupler failure", "Bar fracture",
"Bar fracture inside grip", "Bar pullout", "Bar fracture", "Bar fracture",
"Bar fracture", "Bar fracture", "Bar fracture", "Bar fracture",
"0", "Bar fracture inside grip", "Bar fracture inside grip",
"Bar fracture", "Coupler failure", "Bar fracture", "Bar fracture",
"Bar pullout", "Bar fracture", "Coupler failure", "Bar fracture",
"Coupler failure", NA, "Coupler failure", "Bar fracture", "Bar pullout",
"Bar fracture", "Coupler failure"), x = c("1-3", "7-9", "1-3",
"3-5", "1-3", "30-90", "30-90", "1-3", "3-5", "20-30", "20-30",
"20-30", "5-7", "7-9", "3-5", NA, "9-11", "9-11", "7-9", "9-11",
"9-11", "5-7", "15-20", "11-15", "3-5", "9-11", "9-11", "3-5",
"3-5", NA, "3-5", "1-3", "1-3", "1-3", NA)), row.names = c(NA,
-35L), class = c("tbl_df", "tbl", "data.frame"))
I am using the ggpattern
as explained in the example
Original code to get plot with no patterns:
plot1 <- ggplot(coupler.graph) + aes(x = Category, y = fct_inorder(`Bar Size`), fill = factor(x,
levels = c("0", "1-3", "3-5", "5-7", "7-9",
"9-11", "11-15", "15-20","20-30", "30-90"))) +
geom_tile(width=0.9, height=0.9) + theme_classic() + scale_fill_manual(labels = factor(labels), values = values) +
labs(x = "Splicer Type", y = "Bar Size") +
theme(plot.title = element_blank(), axis.text = element_text(color = "black", size = 12), axis.ticks.x = element_blank(),
axis.line = element_line(color = "black", size = 0.2), axis.ticks.y = element_line(color = "black", size = 0.2),
axis.title.y = element_text(color = "black", size = 16, margin = margin(0,40,0,0)),
axis.title.x = element_text(color = "black", size = 16, margin = margin(35,0,0,0)),
legend.title = element_blank(), legend.text = element_text(color = "black", size = 12))
The plot without the patterns added results in:
Attempt to get the patterns but only returns blank plot every time (note i am using ggplotly
to make these plots interactive for HTML output).
plot1 <- ggplot(coupler.graph) + aes(x = Category, y = fct_inorder(`Bar Size`), fill = factor(x,
levels = c("0", "1-3", "3-5", "5-7", "7-9",
"9-11", "11-15", "15-20","20-30", "30-90"))) +
geom_tile_pattern(width=0.9, height=0.9, pattern_color = NA,
pattern_fill = "black",
pattern_angle = 45,
pattern_density = 0.5,
pattern_spacing = 0.025,
pattern_key_scale_factor = 1) + theme_classic() +
scale_pattern_manual(values = c(`Bar Fracture` = "none", `Bar pullout` = "none", `Coupler Failure` = "circles")) +
scale_fill_manual(labels = factor(labels), values = values) +
labs(x = "Splicer Type", y = "Bar Size") +
theme(plot.title = element_blank(), axis.text = element_text(color = "black", size = 12), axis.ticks.x = element_blank(),
axis.line = element_line(color = "black", size = 0.2), axis.ticks.y = element_line(color = "black", size = 0.2),
axis.title.y = element_text(color = "black", size = 16, margin = margin(0,40,0,0)),
axis.title.x = element_text(color = "black", size = 16, margin = margin(35,0,0,0)),
legend.title = element_blank(), legend.text = element_text(color = "black", size = 12)) +
guides(pattern = guide_legend(override.aes = list(fill = "white")))
I am trying to make the Failure
column to mapped to the patterns of the tile plot. There are only three categories for the Failure
Column.