如何从ggplot2图例中删除scale_color_manual中未使用的图例限制

时间:2019-04-26 15:10:41

标签: r ggplot2 legend

我需要制作多个具有相似数据的图,使所有组在图之间保持相同的颜色。 我可以使用scale_fill_manual为我的组定义颜色,并设置valueslimits。 使用breaks不会在图形之间保留相同的颜色,但是级别解决了该问题。

但是现在我想从图例中删除未使用的组,因为并非所有图形都包含所有组,并且似乎limits会覆盖我添加的任何breaksdrop=TRUE选项

是否有一种方法可以删除这些未使用的值,同时还可以使用限制来保留组之间的颜色,而不必手动将其明确删除?

已附上我要创建的其中一张图的图像,但图例中仍然有未使用的限制。

[图例中超出限制的图形] [1]

下面的代码与我正在尝试的类似。 使用此功能,我希望将“未知球菌” y 图的图例中自动删除,而将“未知振荡球菌科” 从这两个<< strong> x 和 y

我希望它删除未使用的限制,无论该限制是什么(即不必事先指定/知道未使用的限制)。

x <- data.frame ("Genus" =c("unknown Cyanobacteria","Dolichospermum" ,"Planktothrix" ,"Other", "Microcystis"
,"unknown Synechococcaceae" ,"unknown Chroococcales","unknown Oscillatoriophycideae","Pseudanabaena","Synechococcus", "unknown Cyanobacteria","Dolichospermum" ,"Planktothrix" ,"Other", "Microcystis"
,"unknown Synechococcaceae"), "RelativeAB"= c( 22, 35, 46, 71, 44, 27, 17, 87, 67, 44, 17, 88, 15, 77, 99, 20), factor= c( "control", "control", "control", "control", "pulex", "pulex", "pulex", "pulex" ,"magna", "magna", "magna", "magna", "dilution", "dilution", "dilution", "dilution"))

y <- data.frame ("Genus" =c("unknown Cyanobacteria","Dolichospermum" ,"Planktothrix" ,"Other", "Microcystis"
,"unknown Synechococcaceae" ,"unknown Oscillatoriophycideae","Pseudanabaena","Synechococcus", "unknown Cyanobacteria","Dolichospermum" ,"Planktothrix" ,"Other", "Microcystis","unknown Synechococcaceae"), "RelativeAB"= c( 22, 35, 46, 71, 44, 27, 17, 87, 67, 44, 17, 15, 77, 99, 20), factor= c( "control", "control", "control", "pulex", "pulex", "pulex", "pulex" ,"magna", "magna", "magna", "magna", "dilution", "dilution", "dilution", "dilution"))

colorscyano <-c("#CC0000", "#FF6000", "#CC9900", "#336600", "#3333FF", "#9933CC", "#FF66CC", "#66FFFF", "#99FF33", "#FFFF00")
breakscyano <- c("unknown Cyanobacteria"
                 ,"Dolichospermum"
                 ,"Planktothrix"
                 ,"Pseudanabaena"
                 ,"Synechococcus"
                 ,"Other"
                 ,"Microcystis"
                 ,"unknown Synechococcaceae"
                 ,"unknown Chroococcales"
                 ,"unknown Oscillatoriophycideae")

labscyano <-c(expression("Unknown" ~"cyanobacteria")
              ,expression(paste(italic("Dolichospermum")))
              ,expression(paste(italic("Planktothrix")))
              ,expression(paste(italic("Pseudanabaena")))
              ,expression(paste(italic("Synechococcus")))
              ,"Other"
              , expression(paste(italic("Microcystis")))
              , expression (paste("unknown", ~italic("Synechococcaceae")))
              , expression (paste("unknown",~italic("Chroococcales")))
              , expression (paste("unknown",~italic("Oscillatoriophycideae"))))

ggplot(x, aes(x=factor, y=RelativeAB, fill=Genus)) + 
  geom_bar(stat="identity", size=0.5, color="black")+
scale_fill_manual(values = colorscyano, limits= breakscyano, labels=labscyano)+
  theme_classic()

ggplot(y, aes(x=factor, y=RelativeAB, fill=Genus)) + 
  geom_bar(stat="identity", size=0.5, color="black")+
scale_fill_manual(values = colorscyano, limits= breakscyano, labels=labscyano)+
  theme_classic()```


  [1]: https://i.stack.imgur.com/akiVF.png

0 个答案:

没有答案