Category Archives: ggplot2

Colors in ggplot

There are number of ways to control the default colors in ggplot.

The HCL Color Wheel

ggplot simplifies color choice with its default color selection, which are based on a “color wheel.”  The result is a well balanced graphic that doesn’t draw too much attention to any one color.  ggplot uses the HCL color wheel and the hue_pal() function from the scales package.  Specifically, if there are two colors, then they will be selected from opposite points on the circle; if there are three colors, they will be 120° apart on the color circle; and so on.  This ensure that discrete data has maximum contrast as a function of the number variables present.

Posted in ggplot2, R Colors | Leave a comment

Legends (ggplot)

Legends are a key component of data visualization.  ggplot format controls are defined below.


The diamonds data that ships with ggplot.

The Default Legend

The following example presents the default legend to be cusotmized.






Click to enlarge

Removing the Legend

Legends are created for different aesthetics, such as fill, color/colour, linetype, shape, etc. Each aesthetic has a scale function than can be called to remove the legend:

Posted in ggplot2 | Leave a comment

Plotting Forecast Data Objects Using ggplot

Click to enlarge

Robert Hyndman is the author of the forecast package in R. I’ve been using the package for long-term time series forecasts. The package comes with some built in methods for plotting forecast data objects in R that Ive wanted to customize for improved clarity and presentation.  The following article achieves that goal and shares two scripts for plotting forecast data objects using ggplot.

Posted in Data Science, ggplot2, Modeling, R Programming | Leave a comment

LCOE By Region and Technology

The levelized cost of energy (LCOE) is presented by region and for different power generation technologies.  The simple model ignores the impact of subsidies, financing and tax impacts to focus on relative performance by technology.  


The LCOE data is compared to annual average power prices.  In practice, price levels can vary over wide ranges, especially given winter weather.  Several noteworthy conclusions are evident when average prices are used:

Posted in Economics, ggplot2, Project Valuations, Qatar RE | Leave a comment

Correlation Plots in R

The standard function for correlation plots in R is pairs(), which generates a matrix of scatter plots based on all pairwise combinations of variables in a data object.  The standard graph looks something like this after a little color enhancement:” plot13Click to enlarge

The code behind this plot is simple:

Posted in Data Science, ggplot2, R Graphics, R Programming | Leave a comment