r - Fill bar chart with choosed Color and Error: Aesthetics must be either length 1 or the same as the data -


is there know how set color of bars? example, set so2 in red , nox in blue.i used code below :

emission.l <- melt(emission.aerosol, id.vars = 'year',                     measure.vars = c('so2.tons', 'nox.tons'))  gp <- ggplot(emission.l, aes(x = year, y = value, fill = variable)) gp <- gp + theme(axis.text.x = element_text(angle = 90, hjust = 1)) gp <- gp + geom_bar(stat = 'identity', position = 'dodge', fill=c("red","blue")) gp <- gp + ggtitle(emission.aerosol$facility.name) gp <- gp + ylab("emission(tons)") gp <- gp + scale_fill_discrete( name = '',                                  labels = c('so2 (tons)', 'nox (tons)')) gp <- gp + scale_x_continuous(breaks = 2003:2015)   ggsave(filename= paste0(emission.aerosol$facility.name,".png"),device = "png",         width = 15, height = 10,units = "cm", dpi = 100) 

but error message : error: aesthetics must either length 1 or same data (26): fill lot !

lee yee,

can give pointer dataset using? emissions dataset, can accessed online?

in reference ggplot, read linked book reference , read section "modifying text of legend titles , labels" in

http://www.cookbook-r.com/graphs/legends_(ggplot2)/

see below:

  require(ggplot2)   gp <- ggplot(emission.l, aes(x = year, y = value, fill = variable))   gp <- gp + theme(axis.text.x = element_text(angle = 90, hjust = 1))   gp <- gp + geom_bar(stat = 'identity', position = 'dodge')    gp <- gp + ggtitle(emission.aerosol$facility.name)   gp <- gp + ylab("emission(tons)")   gp <- gp + scale_fill_manual(labels = c('so2 (tons)', 'nox (tons)'), values = c("red", "blue"))   gp <- gp + scale_x_continuous(breaks = 2003:2015)    powerplant <- gp   print(powerplant)     dev.off() 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -