Home > other >  Removing the legend from ggpattern
Removing the legend from ggpattern

Time:06-16

While I using geom_bar_pattern from ggpattern in r, two kinds of legend were generated as below:

#remotes::install_github("coolbutuseless/ggpattern")
#library(ggpattern)
#library(ggplot2)

ex<-data.frame(Product=c("Apple","Apple","Apple","Banana","Banana","Banana","Carrot","Carrot","Carrot"),
               Price=c(1,2,3,2,5,3,4,2,3),
               Category=c("Fruit","Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable"),
               Location=c("A1","A1","B1","B1","A1","B1","A1","B1","A1"))
ex
#  Product Price  Category Location
#1   Apple     1     Fruit       A1
#2   Apple     2     Fruit       A1
#3   Apple     3     Fruit       B1
#4  Banana     2     Fruit       B1
#5  Banana     5     Fruit       A1
#6  Banana     3     Fruit       B1
#7  Carrot     4 Vegetable       A1
#8  Carrot     2 Vegetable       B1
#9  Carrot     3 Vegetable       A1

ggplot(ex, aes(x = Location, y=Price, fill = Product, pattern = Category))  
  geom_bar_pattern(stat='identity', position = position_dodge(preserve = "single"),
                   color = "black", 
                   pattern_fill = "black",
                   pattern_angle = 45,
                   pattern_density = 0.05,
                   pattern_spacing = 0.025,
                   pattern_key_scale_factor = 0.8)   
  scale_pattern_manual(values = c(Vegetable = "stripe", Fruit = "none"))  
  guides(pattern = guide_legend(override.aes = list(fill = "white")),
         fill = guide_legend(override.aes = list(pattern = "none"))) 
  theme_bw() 
  theme(text=element_text(size=13))

enter image description here

(Please ignore the horizontal line and left-aligned bar in bar plot.)

In this case, I want to remove the legend for Category.
And if possible, I'd like to combine the dash in product legend.
How can I do this work? enter image description here

CodePudding user response:

Perhaps like this?

library(tidyverse)
library(ggpattern)

ex<-data.frame(Product=c("Apple","Apple","Apple","Banana","Banana","Banana","Carrot","Carrot","Carrot"),
               Price=c(1,2,3,2,5,3,4,2,3),
               Category=c("Fruit","Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable"),
               Location=c("A1","A1","B1","B1","A1","B1","A1","B1","A1"))
ex
#>   Product Price  Category Location
#> 1   Apple     1     Fruit       A1
#> 2   Apple     2     Fruit       A1
#> 3   Apple     3     Fruit       B1
#> 4  Banana     2     Fruit       B1
#> 5  Banana     5     Fruit       A1
#> 6  Banana     3     Fruit       B1
#> 7  Carrot     4 Vegetable       A1
#> 8  Carrot     2 Vegetable       B1
#> 9  Carrot     3 Vegetable       A1
#  Product Price  Category Location
#1   Apple     1     Fruit       A1
#2   Apple     2     Fruit       A1
#3   Apple     3     Fruit       B1
#4  Banana     2     Fruit       B1
#5  Banana     5     Fruit       A1
#6  Banana     3     Fruit       B1
#7  Carrot     4 Vegetable       A1
#8  Carrot     2 Vegetable       B1
#9  Carrot     3 Vegetable       A1

ggplot(ex, aes(x = Location, y=Price, fill = Product, pattern = Category))  
  geom_bar_pattern(stat='identity', position = position_dodge(preserve = "single"),
                   color = "black", 
                   pattern_fill = "black",
                   pattern_angle = 45,
                   pattern_density = 0.05,
                   pattern_spacing = 0.025,
                   pattern_key_scale_factor = 0.8)   
  scale_pattern_manual(values = c(Vegetable = "stripe", Fruit = "none"), 
                       guide = "none")  
  guides(fill = guide_legend(override.aes = list(pattern = c("none", "none", "stripe"))))  
  theme_bw() 
  theme(text=element_text(size=13))

Created on 2022-06-16 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related