Site icon R-bloggers

Three ggplot2 Themes Optimized for the Web

[This article was first published on Data Enthusiast's Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This will be a very short post compared to the detailed stuff I usually write. Just what it says on the tin – I made some tweaks to my three favorite {ggplot2} themes – theme_bw(), theme_classic(), and theme_void() – to make the graphics more readable and generally look better when posted online, particularly in blog posts. Please feel free to borrow and use.

Also, I will be frequently using these themes in subsequent posts, and I’d like to be able to point readers here with a hyperlink instead of repeatedly posting the whole theme_web_…() code every time I am writing a post.

theme_web_bw

# theme_bw => theme_web_bw
theme_web_bw <- function() {
  theme_bw() + # note ggplot2 theme is used as a basis
    theme(plot.title = element_text(size = 16, face = "bold",
                                    hjust = .5,
                                    margin = margin(t = 5, b = 25)),
          plot.caption = element_text(size = 12, hjust = 0, 
                                      margin = margin(t = 15)),
          panel.grid.major = element_line(colour = "grey88"),
          panel.grid.minor = element_blank(),
          legend.title = element_text(size = 14, face = "bold"),
          legend.text = element_text(size = 14),
          strip.text = element_text(size = 14, face = "bold"),
          axis.text = element_text(size = 14),
          axis.title.x = element_text(margin = margin(t = 10), size = 15),
          axis.title.y = element_text(margin = margin(r = 10), size = 15))
}

theme_web_classic

# theme_classic => theme_web_classic
theme_web_classic <- function() {
  theme_classic() + 
    theme(plot.title = element_text(size = 16, face = "bold",
                                    hjust = .5,
                                    margin = margin(t = 5, b = 25)),
          plot.caption = element_text(size = 12, hjust = 0, 
                                      margin = margin(t = 15)),
          legend.title = element_text(size = 14, face = "bold"),
          legend.text = element_text(size = 14),
          strip.text = element_text(size = 14, face = "bold"),
          axis.text = element_text(size = 14),
          axis.title.x = element_text(margin = margin(t = 10), size = 15),
          axis.title.y = element_text(margin = margin(r = 10), size = 15))
}

theme_web_void

# theme_void => theme_web_void
theme_web_void <- function() {
  theme_void() + 
    theme(plot.title = element_text(size = 16, face = "bold",
                                    hjust = .5,
                                    margin = margin(t = 5, b = 25)),
          plot.caption = element_text(size = 12, hjust = 0, 
                                      margin = margin(t = 15)),
          legend.title = element_text(size = 14, face = "bold"),
          legend.text = element_text(size = 14),
          strip.text = element_text(size = 14, face = "bold"))
}

The post Three ggplot2 Themes Optimized for the Web appeared first on Data Enthusiast's Blog.

To leave a comment for the author, please follow the link and comment on their blog: Data Enthusiast's Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.