简体   繁体   中英

Add a 4th feature to tile plot in ggplot2 in r

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.

Here I've created a variable with the pattern so that we can use scale_pattern_identity :

labels <- factor(c("0", "1-3", "3-5", "5-7", "7-9", "9-11", "11-15", "15-20","20-90"), 
                 levels = c("0", "1-3", "3-5", "5-7", "7-9", "9-11", "11-15", "15-20","20-30","30-90")) 
values <- c("white", "#ffffd9", "#edf8b1", "#c7e9b4", "#7fcdbb", "#41b6c4", "#1d91c0", "#225ea8", "#253494","#081d58")

library(tidyverse); library(ggpattern)
coupler.graph %>%
  mutate(pattern = case_when(
    Failure %>% str_detect("Coupler") ~ "circle",
    is.na(Failure) ~ "none",
    TRUE ~ "none"
  )) %>% 

ggplot() + 
  aes(x = Category, y = fct_inorder(`Bar Size`), pattern = pattern,
      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_identity() +
  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 = "black"))),
     fill = guide_legend(override.aes = list(pattern = "none")))

在此处输入图像描述

(BTW my default plot did not look the same as yours, eg with the NA fills, so I suspect your code that produced the figure might vary somewhat from the code in the question.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM