Reputation: 1453
I'm trying to have this plot:
library(ggplot2)
testdata <- data.frame(x=c(1:5),a=c(1:5),b=c(10:6))
ggplot(testdata, aes(x = x)) +
geom_bar(aes(y=b), stat = "identity", fill="darkgrey")+
geom_bar(aes(y=a), linetype="solid", colour="black", stat = "identity", fill=NA)
with a legend. Since I can't get a legend going here (this would be a nice workaround if you know how), I tried to approach the 'correct' way to plot this in ggplot, namely with long data such as:
testdata <- data.frame(x = c(1:5,1:5), y = c(1:5,10:6), group = c(rep("a",5), rep("b",5)))
ggplot(testdata, aes(x = x, y = y, group = group, fill = group)) +
geom_bar(stat = "identity", linetype = "solid", colour = "black", position = position_dodge(width = 0))+
scale_fill_manual(values = c(NA, "darkgrey"))
While I do have a legend here, the bars are much further apart. The usual parameter to change this is the width
inside position_dodge
but I need that equal to 0 for the 100% overlay. So my question in the ideal world is: can I decrease the distance of the bars in the second plot? If this is not feasible, can I add a legend to the fist plot? Any help would be appreciated!
Upvotes: 1
Views: 441
Reputation: 581
Try changing the width
outside position_dodge
.
ggplot(testdata, aes(x = x, y = y, group = group, fill = group)) +
geom_bar(width=1.5,stat = "identity", linetype = "solid", colour = "black", position = position_dodge(width = 0))+
scale_fill_manual(values = c(NA, "darkgrey"))
Upvotes: 2