简体   繁体   English

Scale_fill_manual 不产生条件指定的颜色填充

[英]Scale_fill_manual not producing color fill specified by a condition

1: NO ISSUE 1:没有问题

Suppose I download storm_econ_dmg.csv :假设我下载storm_econ_dmg.csv

fileUrl <- "https://raw.githubusercontent.com/MichaelSodeke/DataSets/main/storm_health_dmg.csv"
download.file(fileUrl, destfile="storm_health_dmg.csv", method = "curl", mode="wb")
df1 <- read_csv("storm_health_dmg.csv") 

Next, I plot this data with geom_bar (take note of fill=(max.fatal == 583) ):接下来,我用geom_bar plot 这个数据(注意fill=(max.fatal == 583) ):

p1 <- ggplot(df1, aes(x=reorder(state, max.fatal), y=max.fatal, fill=(max.fatal == 583) ))
p1 <- p1 + geom_bar(stat="identity")
p1 <- p1 + scale_fill_manual(values=c("TRUE"="seagreen3", "FALSE"="grey80"))
p1 <- p1 + coord_flip() + theme(legend.position="none")
srch <- df1$max.fatal > 50
p1 <- p1 + geom_text(data=df1[srch,], aes(label=evtype), angle=0, hjust=1.1, vjust=0.3, nudge_y=-0.1, size=2.5, fontface="bold")
p1 <- p1 + geom_text(data=df1[srch,], aes(label=max.fatal), angle=0, hjust=-1.3, vjust=0.3, nudge_y=0.1, size=2.5, fontface="bold")
p1 <- p1 + theme(panel.background = element_rect(fill = "white"),
         axis.line.x.bottom=element_line(colour="black"),
             axis.line.y.left=element_line(colour = "black"))
p1 %>% print()

From the image below scale_fill_manual works with TRUE/FALSE values:从下图中scale_fill_manual使用TRUE/FALSE值: 在此处输入图像描述

2: ISSUE 2:问题

Now lets say I download tornado_dmg.csv :现在假设我下载tornado_dmg.csv

fileUrl <- "https://raw.githubusercontent.com/MichaelSodeke/DataSets/main/tornado_dmg.csv"
download.file(fileUrl, destfile="tornado_dmg.csv", method = "curl", mode="wb")
df2 <- read_csv("tornado_dmg.csv") 

I then plot this data with geom_jitter (take note of fill=(f == 5) ):然后我用geom_jitter plot 这个数据(注意fill=(f == 5) ):

p2 <- ggplot(df2, aes(x=bgn.date, y=path.area, fill=(f == 5) ))
p2 <- p2 + geom_jitter(stat="identity", size=1)
p2 <- p2 + scale_fill_manual(values=c("TRUE"="red", "FALSE"="grey80"))
p2 %>% print()

However, notice that scale_fill_manual does not work in this situation for TRUE/FALSE values:但是,请注意scale_fill_manual在这种情况下对TRUE/FALSE值不起作用: 在此处输入图像描述

Please explain why scale_fill_manual works in part 1 but not in part 2 ?请解释为什么scale_fill_manual第 1 部分有效但在第 2部分无效?

Many thanks!非常感谢!

Try changing fill by color .尝试按color更改fill Bars can be filled, whereas dots as those in jitter can be colored:条可以被填充,而抖动中的点可以被着色:

library(ggplot2)
#Data
fileUrl <- "https://raw.githubusercontent.com/MichaelSodeke/DataSets/main/tornado_dmg.csv"
df2 <- read.csv(fileUrl) 
#Code
ggplot(df2, aes(x=bgn.date, y=path.area, color=(f == 5) ))+
  geom_jitter(stat="identity", size=1)+
  scale_color_manual(values=c("TRUE"="red", "FALSE"="grey80"))

Output: Output:

在此处输入图像描述

I loved this problem.我喜欢这个问题。 It was one of those you spend a lot of time trying to solve only to see that the answer was right in front of you.这是您花费大量时间试图解决的问题之一,结果发现答案就在您面前。

The problem is that you used fill for geom_point .问题是您使用fill作为geom_point If you want to change the color of the points, you have to use color or colour (and the matching scale_*_manual ).如果要更改点的颜色,则必须使用colorcolour (以及匹配的scale_*_manual )。 Therefore, your code would be:因此,您的代码将是:

p2 <- ggplot(df2, aes(x=bgn.date, y=path.area, colour=(f == 5) ))
p2 <- p2 + geom_jitter(stat="identity", size=1)
p2 <- p2 + scale_colour_manual(values=c("TRUE"="red", "FALSE"="grey80"))
p2 %>% print()

Or, the way I did, which I think is a little less bulky:或者,我这样做的方式,我认为它不那么笨重:

ggplot(df2, aes(x=bgn.date, y=path.area, colour=(f == 5) ))+
  geom_point()+
  scale_fill_manual(values=c("seagreen3", "grey80"))+
  theme_classic()

I used geom_point , but I don't how that would look like because the whole graph didn't load in time.我使用geom_point ,但我不知道那会是什么样子,因为整个图表没有及时加载。 Maybe it doesn't look as good.也许它看起来不太好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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