I have following code to graph a contracts in different countries.
Country <- CCOM$Principal.Place.of.Performance.Country.Name
Val <- CCOM$Action_Absolute_Value
split <- CCOM$Contract.Category
ggplot(CCOM, aes(x = Country, y = Val, fill = levels(split))) +
geom_bar(stat = "identity")
I want a simple stacked bar chart with the bars colored by the contract category which is the variable "split" (ie. CCOM$Contract.Category).
However when I run the code it produces the graph below:
Why won't gplot separate the spending into three distinct blocks? Why do I get color sections scattered throughout the chart.? I have tried using factor(split) and levels(split) but does not seem to work. Maybe I am putting it in the wrong position.
ggplot(CCOM, aes(x = Country, y = Val, fill = Contract.Category))
– Andriesplit <- CCOM$Contract.Category
and then usinglevels(split)
as thefill
. If don't do those extra weird steps and usefill = Contract.Category
as Andie suggests, you should be fine. – Gregor Thomasggplot(CCOM, aes(x = Principal.Place.of.Performance.Country.Name, y = Action_Absolute_Value, fill = Contract.Category))
. Terribly long names, that make for a hard-to-read code, though... – PavoDive