Giter VIP home page Giter VIP logo

ggplot2bdc's Introduction

ggplot2bdc

ggplot2bdc is a collection of themes and other things that can be used when creating plots with ggplot2. For those using the development version of ggplot, subtitles and captions are supported.

  • Themes
    • theme_bdc_grey
    • theme_bdc_paneled
    • theme_bdc_microtiter
    • theme_bdc_lattice_population
  • Modifying Plots
    • Removing Plot Elements
      • gg_remove_titles, gg_remove_title, gg_remove_subtitle, gg_remove_caption, gg_remove_title.x, gg_remove_title.y
      • gg_remove_ticks, gg_remove_ticks_x, gg_remove_ticks_y
      • gg_remove_grid, gg_remove_grid_major, gg_remove_grid_minor, gg_remove_grid_x, gg_remove_grid_y
      • gg_remove_legend, gg_remove_legend_title, gg_remove_legend_key, gg_remove_legend_text, gg_remove_legend_background
      • gg_remove_background, gg_remove_background_canvas, gg_remove_background_legend, gg_remove_background_facet
    • Rescaling
      • gg_rescale
      • gg_rescale_golden
      • gg_rescale_square
  • Modifying Files Containing Plots
    • trim_whitespace
  • Misc
    • ggsave_golden
    • span

Installation

ggplot2bdc is not on CRAN, but you can use devtools to install the latest and greatest version. To do so:

if(!require("devtools")) install.packages("devtools")
devtools::install_github("briandconnelly/ggplot2bdc")

Examples

To gain access to the following themes, etc. provided, first load the gplot2bdc package. We'll also load ggplot, dplyr, and magrittr for these examples.

library(ggplot2bdc)
library(ggplot2)
library(dplyr)
library(magrittr)

Themes

theme_bdc_grey

theme_bdc_grey is a clean theme that uses thin grey panels on a white background. Legends are placed above. Facet labels are displayed as black text on a grey background.

pUnemploy <- ggplot(data = economics, aes(x = date, y = unemploy)) +
    geom_line() +
    scale_x_date() +
    labs(x = "Year", y = "Thousands of Persons",
         title = "Monthly Unemployment", subtitle = "Seasonally Adjusted",
         caption = "Source: Federal Reserve Bank of St. Louis") +
    theme_bdc_grey()
pUnemploy

Subtle grid lines can be added to the panels with the grid.x and grid.y parameters. These also affect minor grid lines, which can further be controlled with the gridmin.x and gridmin.y parameters.

midwest$inmetro <- factor(midwest$inmetro, levels = c(0, 1),
                          labels = c("Rural", "Metro"))

ggplot(data = midwest,
       aes(x = inmetro, y = percbelowpoverty, color = state, shape = state)) +
    stat_summary(fun.y = "mean", geom = "line", aes(group = state)) +
    stat_summary(fun.y = "mean", geom = "point") +
    scale_color_hue(name = "State") +
    scale_shape_discrete(name = "State") +
    labs(x = NULL, y = "Percent",
         title = "Poverty in the Midwest",
         subtitle = "Poverty rates in rural and metro communities") +
    coord_equal(ratio = 0.5) +
    theme_bdc_grey(ticks.x = FALSE, grid.y = TRUE)

Some people prefer to display categorical data without tick marks along the axis. These are included by default, but can be removed by setting either the ticks.x or ticks.y parameters to FALSE.

We can also use facets to divide the data by state:

ggplot(data = midwest, aes(x = inmetro, y = percbelowpoverty)) +
    facet_grid(. ~ state) +
    stat_summary(fun.y = "mean", geom = "line", aes(group = state)) +
    stat_summary(fun.y = "mean", geom = "point") +
    labs(x = NULL, y = "Percent",
         title = "Poverty in the Midwest",
         subtitle = "Poverty rates in rural and metro communities") +
    coord_equal(ratio = 0.5) +
    theme_bdc_grey(grid.y = TRUE)

txcities <- txhousing %>%
    filter(city %in% c("Austin", "Dallas", "El Paso", "Fort Worth", "Houston",
                       "San Antonio"))

pTX <- ggplot(data = txcities, aes(x = year, y = median / 1000)) +
    facet_grid(city ~ .) +
    stat_summary(fun.data = "mean_cl_boot", geom = "ribbon",
                 color = NA, alpha = 0.3) +
    stat_summary(fun.y = "mean", geom = "line") +
    labs(x = "Year", y = "Thousands of Dollars",
         title = "Home Prices are Getting Bigger in Texas",
         subtitle = "Median Home Prices in Texas' Largest Cities",
         caption = "Source: TAMU Real Estate Center")

pTX + theme_bdc_grey()

theme_bdc_paneled

theme_bdc_paneled is a clean theme that displays plots without borders on grey panels. Legends are placed above. Facet labels are displayed as plan black text. Subtle grid lines can be added to the panels with the grid.x and grid.y parameters, and tick marks can be managed with ticks.x and ticks.y. By default, grid lines are used along the Y axis. This theme is best suited for plots with multiple facets.

pTX + theme_bdc_paneled()

theme_bdc_microtiter

theme_bdc_microtiter is a specialized theme for use in creating figures that represent 96-well microtiter plates.

pPlate <- ggplot(data = platemap, aes(x = Column, y = Row)) +
    geom_point(data = expand.grid(Column = seq(1,12), Row = seq(1,8)),
               color = "grey90", fill = "white", shape = 21, size = 8) +
    geom_point(aes(shape = Environment, color = Strain), size = 9) +
    coord_fixed(ratio = (13/12)/(9/8), xlim = c(0.5, 12.5), ylim = c(0.6, 8.4)) +
    scale_y_reverse(breaks = seq(1, 8), labels = LETTERS[1:8]) +
    scale_x_continuous(breaks = seq(1, 12), position = "top") +
    labs(title = "Plate Layout for My Experiment", subtitle = "25 March 2016") +
    guides(shape = guide_legend(override.aes = list(size = 3)),
           color = guide_legend(override.aes = list(size = 3))) +
    theme_bdc_microtiter()

pPlate

For more information, see the Plotting Microtiter Plate Maps.

theme_bdc_lattice_population

theme_bdc_lattice_population is a specialized theme intended to display populations of individuals structured spatially in a lattice.

ggplot(data = filter(rockpaperscissors, Time == 10),
       aes(x = X, y = Y, color = Type)) +
        geom_point(shape=15) +
        coord_equal(ratio=1) +
        scale_color_hue() +
        theme_bdc_lattice_population()

We can also show multiple population states:

ggplot(data = filter(rockpaperscissors, Time %in% c(0, 200, 400, 600)),
       aes(x = X, y = Y, color = Type)) +
    facet_wrap(~Time, nrow = 1, ncol = 4) +
    geom_point(shape = 15) +
    coord_equal(ratio = 1) +
    scale_color_hue(guide = FALSE) +
    labs(title = "Rock, Paper, Scissors",
         subtitle = "Wait, is it ON three or AFTER three?",
         caption = "Data from simulation on a 50x50 lattice") +
    theme_bdc_lattice_population()

This is also a great opportunity to create an animation. We can use gganimate to create an animated GIF showing the population for the first 10 time steps.

library(gganimate)

p <- ggplot(data = filter(rockpaperscissors, Time < 10),
       aes(x = X, y = Y, color = Type, frame = Time)) +
    geom_point(shape = 15, size = 3) +
    coord_equal(ratio = 1) +
    scale_color_hue(guide = FALSE) +
    labs(title = "Rock, Paper, Scissors at t=") +
    theme_bdc_lattice_population()

gg_animate(p = p, saver = "gif", interval = 0.2)

Modifying Plots

Although you can always change a plot's properties using ggplot2's theme function, it's often difficult to remember which elements control a plot's visual characteristics. These and other gg_ functions make it a bit easier to quickly alter a plot.

gg_rescale_golden

gg_rescale_golden adjusts the proportions of the axes of the given plot object so that follow the golden ratio (horizontally). If no plot is provided, the last plot that was displayed is used.

ggplot(mtcars, aes(x = hp, y = mpg)) +
    geom_smooth(method = "loess") +
    geom_point(shape = 1) +
    labs(x = "Horsepower", y = "Fuel Efficiency (mpg)") +
    theme_bdc_grey()

gg_rescale_golden()

gg_rescale_square

gg_rescale_square adjusts the proportions of the axes of the given plot object so that they are equal. If no plot is provided, the last plot that was displayed is used.

ggplot(data = economics, aes(x = date, y = unemploy)) +
    geom_line() +
    labs(x = "Date", y = "Thousands of Unemployed Persons") +
    theme_bdc_grey()

gg_rescale_square()

gg_rescale

gg_rescale adjusts the proportions of the axes of the given plot object to the given ratio. If no plot is provided, the last plot that was displayed is used.

ggplot(data = economics, aes(x = date, y = unemploy)) +
    geom_line() +
    labs(x = "Date", y = "Unemployed Persons (x1000)") +
    theme_bdc_grey()

gg_rescale(ratio = 16 / 9)

ggsave_golden

ggsave_golden saves the given plot to the given filename. The dimensions of the resulting image file's canvas will follow the golden ratio, which is useful in conjunction with gg_rescale_golden. If no plot is specified, the most recently displayed plot is used.

# Save our plot of unemployment from earlier
ggsave_golden(filename = "monthly_unemployment.pdf", plot = pUnemploy)

ggplot2bdc's People

Contributors

briandconnelly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ggplot2bdc's Issues

rescale_plot to axis limits, not data

rescale_plot works with the plot's data and does not consider the axis limits. When limits are set manually, the resulting plots don't necessarily have the ratio specified (although the data do).

error with custom_themes

Hi Brian,
I'm primarily a lattice user, so pardon me if I've missed something obvious when trying to use your custom themes....

When running any of the examples, I receive the following message:
Error in (function (el, elname) : "plot.subtitle" is not a valid theme element name.
When I re-rerun the code without the +theme_bdc_foo part, no error is issued

My sessionInfo()

R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] magrittr_1.5     dplyr_0.5.0      ggplot2_2.1.0    ggplot2bdc_0.2.1

loaded via a namespace (and not attached):
 [1] colorspace_1.2-6 scales_0.4.0     R6_2.1.2         assertthat_0.1   plyr_1.8.4       DBI_0.4-1       
 [7] tools_3.3.1      gtable_0.2.0     tibble_1.0       Rcpp_0.12.5      grid_3.3.1       munsell_0.4.3   

Thanks for any advice,
Wade

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.