Skip to main content

ggplot with a highcharts taste

Giving ggplot2 the look and feel of Highcharts.
R
ggplot2
data-visualization
Author

Joshua Kunst

Published

April 28, 2014

Update 2015-11-07: This is a modification of an old post. Finally I made a pull request and it was accepted by Jeffrey Arnold.

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:

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

library("showtext")
library("ggplot2")

# Use a local copy of the same font so the example also renders offline.
sysfonts::font_add("myfont", regular = "blog/posts/2014-04-28-ggplot-with-a-highcharts-taste/fonts/OpenSans-Regular.ttf")
showtext_auto()

data(diamonds)

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(){
  theme(
    text                = element_text(family = "myfont", size = 12),
    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', linewidth = .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()
  )
}

The theme is ready. Now to plot.

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 = scales::comma) +
  theme_hc()

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.

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

Finally,

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 = scales::percent) +
  scale_fill_manual(values = colors_hc) +
  xlim(4, 8) +
  theme_hc()

In my humble opinion, it look great. What do you think?

References

  • Arnold, J. B. ggthemes — additional themes for ggplot2.
  • Highsoft. Highcharts demos — the visual inspiration for this theme.
  • Qiu, Y. showtext — font rendering in R graphics.