Reputation: 409
I want to create a cumulative frequency plot. This is some sample code:
library(ggplot2)
year <- c(2010, 2011, 2012, 2010, 2011, 2012)
type <- c("A", "A", "A", "B", "B", "B")
freq <- c(10, 20, 30, 15, 15, 35)
df <- data.frame(year, type, freq)
ggplot(df, aes(x = year, y = freq, fill = type)) +
geom_bar(stat = "sum")
ggplot2 creates 2 legends: an upper one "type A and B" (colored), and another one "n 1" (grey).
I can remove the the (upper) type A and B legend with + guides(fill=FALSE), but I am not able to remove the lower grey n 1 legend.
Second question: is it possible to have the variables plotted always in the same order? ggplot2 obviously starts from the bottom with the smaller number.
Upvotes: 1
Views: 409
Reputation: 23788
You can use stat="identity"
instead of stat="sum"
:
ggplot(df, aes(x = year, y = freq, fill = type)) + geom_bar(stat = "identity")
This removes the unnecessary grey "n 1" legend and places type A always below type B in the stacked bar graph.
Upvotes: 2