在 ggplot2 中使用 scale_color_manual 时,绘图中的中断和值不匹配

Posted

技术标签:

【中文标题】在 ggplot2 中使用 scale_color_manual 时,绘图中的中断和值不匹配【英文标题】:breaks and values not matching up in plot when using scale_color_manual in ggplot2 【发布时间】:2019-09-15 09:39:41 【问题描述】:

我正在尝试为我使用 scale_color_manual 绘制的每个组使用相同的颜色(值)绘制多个图形。它似乎适用于许多图表,但不是全部,我似乎无法弄清楚为什么。

我已经为它们在我的 .csv(数据框)中的确切名称设置了中断,并为我的颜色值和标签创建了一个向量。 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, breaks= breakscyano, labels=labscyano)+
  theme_classic()```

并非所有图表都列出了所有组,但由于它们与中断匹配,我认为图表之间的颜色应该匹配? 但是,当我绘制图表时,它并没有正确匹配。

数据之间的唯一区别是这个因素在其中一个因素中少了一个点,但我觉得这无关紧要,因为中断(.csv 中的名称)是相同的,并且应该仍然与值匹配。 ..

【问题讨论】:

您可能需要limits,而不是breaks(因为我经常在离散数据上犯这个错误:))。您可能还需要为颜色传递一个命名向量。喜欢names(colorscyano) = breakscyano,然后将其传递给values 是的!限制有效。非常感谢! 后续问题...如何从图例中删除未使用的限制,为什么这不是默认值(我尝试 drop=TRUE 没有成功)? @MeganLadds 请提出一个新问题。后续问题在 cmets 中获得足够关注的机会相当低。还请注意 MrFlick 发布的建议,并提供一个可重现的示例。 【参考方案1】:

这似乎是您在数据字段中存在错误的额外空白的问题。如果它们被删除,您的情节似乎像预期的那样:

library(ggplot2)

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, breaks= 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, breaks= breakscyano, labels=labscyano)+
  theme_classic()

由reprex package (v0.3.0) 于 2020 年 10 月 26 日创建

【讨论】:

以上是关于在 ggplot2 中使用 scale_color_manual 时,绘图中的中断和值不匹配的主要内容,如果未能解决你的问题,请参考以下文章

在 ggplot2 中使用自定义 OTF 字体

如何在ggplot2中使用希腊符号?

R语言ggplot2可视化使用ggplot2::ggsave函数和gridextra::marrangegrob函数将多个可视化结果保存在pdf文件中

如何使用 ggplot2 在 R 中制作具有透明背景的图形?

在 R 中使用 ggplot2 复制图形

在ggplot2中更改条形图轮廓