Reddit Post Inspiration

Source: Change ggplot legend title

Original Plot

  library(ggplot2)

  df <- structure(
    list(
      year = c(1990L, 1991L, 1992L, 1994L, 1995L, 1996L, 1998L, 1999L,
               2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L,
               2008L, 2009L, 2010L, 2011L, 2012L, 2014L, 2015L, 2016L, 2018L),
      rate = c(0.736842105263158, 0.888888888888889, 0.9, 0.620689655172414, 
               0.777777777777778, 0.842105263157895, 0.714285714285714,
               0.833333333333333, 0.5, 0.75, 0.684210526315789, 0.5,
               0.790697674418605, 0.555555555555556, 0.636363636363636,
               0.666666666666667, 0.673913043478261, 0.8125, 
               0.777777777777778, 0.730769230769231, 0.652173913043478,
               0.722222222222222, 0.65625, 0.827586206896552, 
               0.666666666666667),
      postpre = c("Pre", "Pre", "Pre", "Pre", "Pre", "Pre", "Post",
                  "Post", "Post", "Post", "Post", "Post", "Post",
                  "Post", "Post", "Post", "Post", "Post", "Post",
                  "Post", "Post", "Post", "Post", "Post", "Post"),
      meanrate = c(0.794383948376689, 0.794383948376689, 0.794383948376689,
                   0.794383948376689, 0.794383948376689, 0.794383948376689,
                   0.692156429883868, 0.692156429883868, 0.692156429883868, 
                   0.692156429883868, 0.692156429883868, 0.692156429883868,
                   0.692156429883868, 0.692156429883868, 0.692156429883868,
                   0.692156429883868, 0.692156429883868, 0.692156429883868,
                   0.692156429883868, 0.692156429883868, 0.692156429883868,
                   0.692156429883868, 0.692156429883868, 0.692156429883868,
                   0.692156429883868)
      ),
    row.names = c(NA, -25L),
    class = "data.frame")

p1 <- ggplot(df, aes(x = year, y = rate * 100)) + 
  #labs(fill="this")+ 
  geom_line(aes(color = postpre)) + 
  geom_point(aes(color = postpre), size = 4) +
  theme_minimal(base_size = 20) +
  theme(
    panel.background = element_rect(colour = "black", size = 0.5),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  ) +
  # guides(fill = guide_legend(title = "THIS")) + # this doesn't work
  labs(x = "Year") +
  labs(y = "% Success")
p1

My Revisions

Separate Graphs

ColorGuideName <- "Timeframe"

p2 <- p1 +
  labs(
    color = ColorGuideName,
    title = "Time Series Plot") +
  scale_x_continuous(
    breaks = seq.int(from = 1990, to = 2020, by = 5),
    labels = function(x) {ifelse(x %% 5, "", x)}) +
  theme(
    title = element_text(size = 16),
    axis.ticks = element_line(size = 1),
    axis.text = element_text(size = 12),
    axis.text.x = element_text(angle = 90, vjust = 0.5),
    legend.text = element_text(size = 12)
  )
p2

p3 <- ggplot(df, aes(x = rate, fill = postpre)) +
  geom_density(n = 2^6, show.legend = FALSE) +
  labs(
    fill = ColorGuideName,
    x = "% Success",
    y = "Density",
    title = "Density Plot"
  ) +
  facet_wrap(postpre ~ ., nrow = 2, ncol = 1)
p3

p4 <- ggplot(df) +
  geom_boxplot(aes(x = postpre, y = rate * 100, fill = postpre),
               show.legend = FALSE) +
  geom_jitter(aes(x = postpre, y = rate * 100),
              width = 0.05, height = 0, show.legend = FALSE) +
  labs(
    title = "Box Plot",
    x = "Timeframe",
    y = "% Success",
    fill = ColorGuideName)
p4

A composite plot

  library(gridExtra)
  
  my_layout_matrix <- matrix(c(1, 1, 2, 3), nrow = 2, ncol = 2, byrow = TRUE)
  gridExtra::grid.arrange(
    p2 + scale_y_continuous(expand = expand_scale(0, 10)), p3, p4,
    layout_matrix = my_layout_matrix
  )

Creating multipanel layouts

Sample code from [1]

library(ggplot2)
library(gridExtra)

p1 <- qplot(mpg, wt, data = mtcars, colour = cyl, show.legend = FALSE)

p2 <- qplot(mpg, data = mtcars) + ggtitle("title")

p3 <- qplot(mpg, data = mtcars, geom = "dotplot")

p4 <- p1 + 
  facet_wrap( ~ carb, nrow = 1) +
  theme(legend.position = "none") +
  ggtitle("facetted plot")

gridExtra::grid.arrange(
  p1, p2, p3, p4,
  layout_matrix = matrix(
    c(1, 2, 3,
      4, 4, 4
    ),
    nrow = 2,
    ncol = 3,
    byrow = TRUE
  )
)

Citations

  1. Baptiste AuguiƩ, Laying out multiple plots on a page [Internet], 2018-11-03, referenced 2019-02-02 5:56 PM ET.

  2. Baptiste AuguiƩ, Arranging multiple grobs on a page [Internet], 2017-09-09, referenced 2019-02-04 10:15 AM ET.