1

I made some lovely bar charts with the basic plot function in R. However, my boss told me she would like them done in ggplot using colorbrewer colors.

Well, 2 days later, I finally made the following:

cool dude bar chart

I used the following code:

ggplot(data, aes(x=edscat,y=studynum)) + geom_bar(aes(fill= colorRampPalette(brewer.pal(6,"Accent"))(6)),stat = "identity") + 
  labs(title="Study Type", x="EDS Category", y="Number of Studies") + 
  theme(plot.title = element_text(size=30,hjust = 0.5))+theme(legend.title=element_blank()) + theme(panel.background = element_rect(fill = 'white')) + 
  theme(axis.line = element_line(color="black", size = 0.5)) + theme(axis.text.x= element_text(face="bold", size=12)) + 
  theme(axis.text.y = element_text(face="bold", size=12)) + scale_y_continuous(breaks=seq(0,80,10)) + scale_x_continuous(breaks=seq(0,6,1)) + 
  theme(axis.title.x = element_text(face="bold", size=12)) + theme(axis.title.y = element_text(face="bold", size=12)) + 
  theme(legend.position="right") + scale_fill_discrete(name = "Categories", labels = c("Water Quantity","Biota and Biodiversity","Water Quality","Landcover","Geomorphology","Connectivity and Fragmentation")) + 
 theme(legend.justification=c(1,0),legend.position=c(0.95, 0.70),panel.border = element_rect(colour = "black", fill=NA),legend.box.background = element_rect(colour = "black"), legend.background = element_rect(linetype = "solid", colour = "black"),
)

I love the look of it, but I had to reorder the labels in order to get them to match the colors. The basic bar chart function will match the colors to the labels, as well as label them top to bottom, from the left to right reading of the x axis. Please see the next image:

old timey

dc37
  • 15,840
  • 4
  • 15
  • 32
  • 2
    You can change the order of factor levels. But as a general best practice, if color doesn't signify anything that the x-axis doesn't already cover, you might eliminate the color encoding and put those labels directly on the axis. The less the reader has to move back & forth between the chart and the legend, the better (at least in this example) – camille Jan 02 '20 at 20:20
  • 1
    Also, it doesn't look like you've actually used the color palette you want. You assign a variable name inside `aes`, not the scale itself. Beyond that, we'd need a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), but I'd guess this is covered by other SO posts like [this](https://stackoverflow.com/q/26872905/5325862) or [this](https://stackoverflow.com/q/38619402/5325862) – camille Jan 02 '20 at 20:28
  • So are you saying to get rid of the legend? – Matthew Aldrovandi Jan 06 '20 at 14:23
  • Not necessarily. I'm suggesting that if you can label your x-axis values directly on the axis rather than in a legend, you probably should. It seems like the color encoding isn't adding anything (besides prettiness) that isn't already handled by the axis labeling. That's an aside though; to help with this specific question, we'd need a reproducible example – camille Jan 06 '20 at 15:10
  • I used the suggestions and made a much nicer looking bar chart thanks to camille. – Matthew Aldrovandi Jan 10 '20 at 19:51

0 Answers0