Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a summary line per facet rather than overall

Tags:

r

ggplot2

I am trying to do something similar to this question, but hoping to do it in a single block rather than caching values separately.

I am creating a chart similar to this:

library(tidyverse)

mtcars %>%
  rownames_to_column("carmodel") %>%
  mutate(brand = substr(carmodel, 1, 4)) %>%
  group_by(brand, cyl) %>%
  summarize(avgmpg = mean(mpg)) %>%
  ggplot(aes(x=brand, y = avgmpg)) +
  geom_point() +
  facet_grid(cyl~., scales = "free_y") +
  coord_flip()

enter image description here

Grouping by one of the fields, and then charting a calculated summarize() value on the result, using facets to place similar observations together. What I would like to do is add a line in each facet showing the mean of the observations for that facet. I have tried adding geom_hline(aes(yintercept = mean(avgmpg))) to the definition, but it returns the mean for the entire dataset, not the observations in the facet:

enter image description here

What I'm after is more like this. (The lines here are drawn with an image editor.)

enter image description here

Is this possible?

like image 283
Margaret Avatar asked Jun 22 '18 03:06

Margaret


People also ask

What is a facet plot?

Facet plots, also known as trellis plots or small multiples, are figures made up of multiple subplots which have the same set of axes, where each subplot shows a subset of the data.

What is the difference between Facet_wrap and Facet_grid?

While facet_grid shows the labels at the margins of the facet plot, facet_wrap creates a label for each plot panel.

What is facet in R programming?

The facet approach partitions a plot into a matrix of panels. Each panel shows a different subset of the data. This R tutorial describes how to split a graph using ggplot2 package. There are two main functions for faceting : facet_grid()

What is Facet_wrap?

facet_wrap() makes a long ribbon of panels (generated by any number of variables) and wraps it into 2d. This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner. You can control how the ribbon is wrapped into a grid with ncol , nrow , as.


1 Answers

Because you said you wanted to do it in one block, note that among the many uses of . you can use it in geoms to refer to the original data argument to ggplot(). So here you can do an additional summarise to get the values for geom_vline. I also just reversed the aesthetics in geom_point instead of using coord_flip.

library(tidyverse)

mtcars %>%
  rownames_to_column("carmodel") %>%
  mutate(brand = substr(carmodel, 1, 4)) %>%
  group_by(brand, cyl) %>%
  summarize(avgmpg = mean(mpg)) %>%
  ggplot(aes(y=brand, x = avgmpg)) +
  geom_point() +
  geom_vline(
    data = . %>%
      group_by(cyl) %>%
      summarise(line = mean(avgmpg)),
    mapping = aes(xintercept = line)
    ) +
  facet_grid(cyl~., scales = "free_y")

Created on 2018-06-21 by the reprex package (v0.2.0).

like image 146
Calum You Avatar answered Nov 14 '22 21:11

Calum You