gpt4 book ai didi

r - 在 ggplot2 中使用 scale_color_manual 时,中断和值在图中不匹配

转载 作者:行者123 更新时间:2023-12-05 07:23:39 25 4
gpt4 key购买 nike

我正在尝试为我使用 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 中的名称)是相同的并且应该仍然与值匹配。 ..

最佳答案

这似乎是数据字段中错误的额外空格的问题。如果它们被删除,您的地 block 似乎会像预期的那样:

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 创建于 2020-10-26 (v0.3.0)

关于r - 在 ggplot2 中使用 scale_color_manual 时,中断和值在图中不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55858034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com