ggplot with a highcharts taste

(This article was first published on Posts for category R, and kindly contributed to R-bloggers)

At work I use ggplot2 almost for everything. I really like the mid term between high level (highcharts) and low-level (like d3 for example). The deafult theme for ggplot it’s good, and really good if you compare with the old looking R base graphics, and there is more: the ggthemes package which have some themes for ggplot objects. However, I miss the elegant and modern touch, for example in highcharts.

So I decide to play around the theme function to replicate the look and feel of highcharts. The main tasks were:

  1. Change the font to a more modern one.
  2. Remove grid lines (minor ones).
  3. Use a more plain color palette.
  4. Reduce the width of bars.
  5. Put a white background.

The chosen font was Open Sans (an ultra used font nowadays) which you can use in R with extrafont package.

In the next images we can see some samples of this theme.

As you can see, the plot look more clean without the gridlines and the background. This cause less confusion (and maybe less detail) because generate more space.


In my humble option, it look great. What do you think? Here the R code to do the magic ;). The first part defines the theme and colors, the second one we make the plots.

data <- subset(diamonds, color %in% c("E", "F", "G") & cut %in% c("Ideal", "Premium", "Good"))
data$indicator <- ifelse(data$color %in% c("G" ), 1, 0)
colors_hc <- c("#7CB5EC", "#313131", "#F7A35C",
               "#90EE7E", "#7798BF", "#AAEEEE",
               "#FF0066", "#EEAAEE", "#55BF3B",
               "#DF5353", "#7798BF", "#AAEEEE")


theme_hc <- function(){
    text                = element_text(family="Open Sans", size = 10),
    title               = element_text(hjust=0), 
    axis.title.x        = element_text(hjust=.5),
    axis.title.y        = element_text(hjust=.5),
    panel.grid.major.y  = element_line(color='gray', size = .3),
    panel.grid.minor.y  = element_blank(),
    panel.grid.major.x  = element_blank(),
    panel.grid.minor.x  = element_blank(),
    panel.border        = element_blank(),
    panel.background    = element_blank(),
    legend.position     = "bottom",
    legend.title        = element_blank()

p1 <- ggplot(data) +
  geom_bar(aes(cut), width =.4, fill = colors_hc[1]) +
  ggtitle("An interesting title for a bar plot") +
  xlab("Cut") + ylab("Amount") +
  scale_y_continuous(labels = comma) +

p2 <- ggplot(data) +
  geom_bar(aes(color, fill=cut), position="dodge", width=.4) +
  ggtitle("Another interesting title") +
  xlab("Cut") + ylab("Amount") +
  scale_y_continuous(labels = comma) +
  scale_fill_manual(values=colors_hc) +

p3 <- ggplot(data) + geom_density(aes(x, fill=cut, color=cut), alpha=I(0.5)) +
  ggtitle("Density plot") +  xlab("x") + ylab("Density") +
  scale_y_continuous(labels = percent) +
  scale_fill_manual(values=colors_hc) +
  xlim(4, 8) +

Bonustrack: More is less! This is a good lesson from Darkhorse Analytics.

To leave a comment for the author, please follow the link and comment on their blog: Posts for category R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)