

Ggplot2 makes it easy to use facet_wrap() with two variables by simply stringing them together with a +. Notice that facet_wrap() chose a 1-row layout as optimum for our three panels. Many “good” characters, on the other hand, were introduced in subsequent years. A large number of “bad” characters were introduced in 1963 (8) and 1964 (16) but far fewer in later years. This plot is more informative than the original.
Subtitle = "(limited to characters with more than 100 appearances)", Labs(title = "New Marvel characters by alignment", Geom_line(color = "steelblue", size = 1) + ggplot(data = marvel_count, aes(year, n)) + Think of facet_wrap() as a ribbon of plots that arranges panels into rows and columns and chooses a layout that best fits the number of panels. # $ n 2, 1, 1, 4, 1, 3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 6, 4.īy simply adding + facet_wrap(~ align) to the end of our plot from above we can create a multi-panel plot with one pane per “alignment”. # $ align "Good Characters", "Neutral Characters", "Bad Characters. To create a multi-panel plot with one panel per “alignment” we first need counts by year by alignment which we do with this code: marvel_count 1939, 1939, 1940, 1940, 1941, 1941, 1943, 1944, 1947, 19. Our first multi-panel plot: counts of appearance of “good” and “bad” characters with facet_wrap() Mutate(SEX = stringr::str_replace(SEX, "Characters", "")) %>% library(ggplot2)įilter(!is.na(APPEARANCES), APPEARANCES>100) %>% Since many of the characters are of limited importance to the franchise, we also filter to characters that have appeared at least 100 times. In particular, we will remove records with missing values for our key variables, shorten the SEX variable and rename the SEX variable name to gender. We will start by loading the data and applying some cleanup. ALIGN: representing whether the character is good, bad or neutral.YEAR: year of first appearance for the character.We will take advantage of three variables: In honor of the release of Captain Marvel and the much anticipated upcoming Avengers: Endgame we’re using the fun Marvel character dataset downloaded from Kaggle for our example. An example: year of appearance of Marvel characters
