I am really new to R and GGPLOT and trying to create a chart with RAG colours based on the values in the Y axis- if a specific industry value is greater than all industries, it is green, if the same as all industries it is amber and if less than all industries it is red.
After much searching online I thought I had managed to crack it, but the bars I expect to be red are coming out blue- I've left the legend in the image as it shows what is going on. The thing is, I am not sure if I might be further away from success than I think- i.e. whether it is a fluke that the blue bars at the bottom have the label red.
To get a colour vector I adapted another code I found on stack overflow (lowest number is red through to highest being green, using logical values)
Bar_Colour <- c("red", "amber", "green")[
(High_Cash_Reserves_Above0 < subset(High_Cash_Reserves_Above0,
Cash_Reserves_Industry_Wrapped == "All Industries")) +
2*(High_Cash_Reserves_Above0 == subset(
High_Cash_Reserves_Above0,
Cash_Reserves_Industry_Wrapped == "All Industries")) +
3*(High_Cash_Reserves_Above0 > subset(
High_Cash_Reserves_Above0,
Cash_Reserves_Industry_Wrapped == "All Industries"))]
Bar_Colour
I then used this is to set the fill in the horizontal bar chart built using ggplot:
ggplot(
BICS_Cash_Reserve_Above0,
aes(
reorder(
Cash_Reserves_Industry_Wrapped,
High_Cash_Reserves_Above0,
sum
),
High_Cash_Reserves_Above0, fill=Bar_Colour
)) +
geom_bar(stat = "identity", width = 0.95) +
theme(axis.text.x = element_text(angle = 90, size = 0.04)) +
coord_flip(
)
I'd appreciate any advice on getting those bars red as this is a proof of concept piece and I feel like I am so close to generating this output using R (we currently do it in Excel, which is tiresome).
Thanks!
Ed
PS- unfortunately I cannot share underlying data as it is commercially sensitive