Go to TogaWare.com Home Page. Data Science Desktop Survival Guide
by Graham Williams
Duck Duck Go



CLICK HERE TO VISIT THE UPDATED SURVIVAL GUIDE

Bar Chart in Colour and no Legend

20200428

\includegraphics[width=\textwidth]{figures/onepager/ggplot2:frequency_barchart_colour-1}

ds %>%
  ggplot(aes(wind_dir_3pm, fill=wind_dir_3pm)) +
  geom_bar() +
  scale_y_continuous(labels=comma) +
  labs(x=vnames["wind_dir_3pm"], y="Count") +
  theme(legend.position="none")

Colour can add interest to a plot, though it is usually good to use the colour to communicate further information about the data. In this example we add a spread of colour across the bars by specifying the aesthetic fill=wind_dir_3pm. Thus the colour used is controlled by the values of the variable wind_dir_3pm. This is also the variable being plotted on the x-axis and so as an example it adds no new information to the plot. A good set of colours is chosen by default and could be changed through themes.

We add a ggplot2::theme() element here to remove the legend that would be displayed by default, by indicating that the legend.position="none". An alternative to turn the legend off is to provide show.legend=FALSE to the layer (i.e., ggplot2::geom_bar()). This then allows layer specific control of the legends.


Support further development by purchasing the PDF version of the book.
Other online resources include the GNU/Linux Desktop Survival Guide.
Books available on Amazon include Data Mining with Rattle and Essentials of Data Science.
Popular open source software includes rattle and wajig.
Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 2000-2020 Togaware Pty Ltd. . Creative Commons ShareAlike V4.