Suppose I download 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)
):
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:
Now lets say I download 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)
):
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:
Please explain why scale_fill_manual
works in part 1 but not in part 2?
Many thanks!
Try changing fill
by color
. 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: