i plotting yearly demand using ggplot
(my code below) not able put color legend plot. data.frame has "zone" , "totaldemand" (only 2 columns) , have 3 data.frames 3 years ("sales12", "sales13" , "sales14").
ggplot() + geom_point(data=sales12, aes(x=factor(zone), y=totaldemand/1000), color='green',size=6, shape=17) + geom_point(data=sales13, aes(x=factor(zone), y=totaldemand/1000), color='red',size=6, shape=18)+ geom_point(data=sales14, aes(x=factor(zone), y=totaldemand/1000), color='black',size=4, shape=19) + labs(y='demand (in 1000s)',x='zones') + scale_colour_manual(name = 'the colour', values = c('green'='green', 'black'='black', 'red'='red'), labels = c('12','13','14'))
please me identify mistake.
with small example data frame, df, melted format ggplot
.
dput(df) structure(list(zone = structure(1:4, .label = c("alpha", "baker", "charlie", "delta"), class = "factor"), totaldemand = c(90l, 180l, 57l, 159l), sales12 = c(25l, 40l, 13l, 50l), sales13 = c(30l, 60l, 16l, 55l), sales14 = c(35l, 80l, 28l, 54l)), .names = c("zone", "totaldemand", "sales12", "sales13", "sales14"), class = "data.frame", row.names = c(na, -4l)) df.m <- melt(df, id.vars = "zone", measure.vars = c("sales12", "sales13", "sales14")) ggplot(df.m, aes(x=factor(zone), y=value, color = variable )) + geom_point(size=6, shape=17) + labs(y='demand (in 1000s)',x='zones') + scale_colour_manual(values = c('green', 'black', 'red'))
you can adjust size , shape , colors of points, add title, etc.. legend can positioned on bottom, example.