plot - How to change barplot in ggplot for R -
i have plot in ggplot
shows me each category of "rating" level of price in "bank" , "sistem". code:
##fict a<-c("rating1","rating2","rating3") b<-c(1.2,1.2,1.3) c<-c(1.6,1.4,1.6) gg<-cbind('rating'=rep(a,2),'price'=c(b,c),'tipo'=rep(c("bank","sistem"),3)) gg<-as.data.frame(gg) a<-rgb(red=150, green=191, blue=37, maxcolorvalue = 255) b<-rgb(red=80, green=113, blue=14, maxcolorvalue = 255) ggplot(gg, aes(x=tipo, y=price,width=1)) + geom_bar(position='stack', stat='identity', fill=c(b,a), color='black') + facet_wrap( ~ rating)+ theme_bw() + theme(axis.line = element_line(colour = "black"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), strip.background = element_rect(colour = 'white', fill = 'white', size = 3), axis.title.y=element_text(vjust=0.19), axis.title.x=element_text(vjust=0.19) #strip.text.x = element_text(colour = 'red', angle = 45, size = 10, hjust = 0.5, vjust = 0.5, face = 'bold') ) + xlab("my x label") + ylab("my y label") + labs(title = 'difference')
this code generates plot.
i'd change 3 things:
- i'd labels rating shows in bottom
- i'd "bank" , "sistem" labels disappear , change legend colors bank , sistem.
- if it's possible put legend under x-axis title in horizontal way
thank you
upgrade comment answer.
library(ggplot2) # data - tweaked code - there no need cbind within data.frame # , names not need in quotes gg <- data.frame(rating=rep(c("rating1","rating2","rating3"),2), price=c(c(1.2,1.2,1.3),c(1.6,1.4,1.6)), tipo=rep(c("bank","sistem"),3)) <- rgb(red=150, green=191, blue=37, maxcolorvalue = 255) b <- rgb(red=80, green=113, blue=14, maxcolorvalue = 255) # plot # use position dodge bars side-by-side # theme_classic removes grid lines , uses theme_bw() # scale_fill_manual manually specify colours - using fill = tipo in # aesthetic call of ggplot legend generated # scale_y_continuous - using expand starts axis @ 0 ggplot(gg, aes(x=rating, y=price, fill=tipo)) + geom_bar(position='dodge', stat='identity', color='black') + theme_classic() + scale_fill_manual(values = c(b,a)) + scale_y_continuous(limit=c(0,2), expand=c(0,0)) + labs(title = 'difference', x = "my x label", y = "my y label") + theme( axis.title.y=element_text(vjust=0.19), axis.title.x=element_text(vjust=0.19) , legend.position = "bottom", legend.title=element_blank())
Comments
Post a Comment