简体   繁体   中英

Changing colour of particular bars in bar-chart using ggplot2

I have this code to create a graph shown below. I found most of this code in an answer to a previous question in which the graph is very similar to mine (at the bottom of the page).

> x = ggplot(df, aes(x=Year, y=NAO_Index, width=.8)) +
geom_bar(stat="identity", aes(fill=NAO_Index>0), position='identity') +
theme_bw() + scale_fill_discrete(guide='none') +
ylab("NAO Index") +
ggtitle("NAO Index between 1860 and 2050") +
scale_x_continuous(breaks=c(seq(1860,2050,10)))

How do I adapt this so the bars with a positive NAO index (ie currently blue) are red, and those below blue?

I'm sure this is likely an easy solution but I'm very new to R and can't figure it out! Many thanks in advance. 在此处输入图片说明

Use something like

scale_fill_manual(values=c("blue", "red"), guide=FALSE)

instead of

scale_fill_discrete(guide='none')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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