1

For a sample dataframe:

df1 <- structure(list(name = c("a", "b", "c", "d", "e", "f", "g", "h", 
"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", 
"v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", 
"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", 
"v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", 
"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", 
"v", "w", "x", "y", "z"), amount = c(5.5, 5.4, 5.2, 5.3, 5.1, 
5.1, 5, 5, 4.9, 4.5, 6, 5.9, 5.7, 5.4, 5.3, 5.1, 5.6, 5.4, 5.3, 
5.6, 4.6, 4.2, 4.5, 4.2, 4, 3.8, 6, 5.8, 5.7, 5.6, 5.3, 5.6, 
5.4, 5.5, 5.4, 5.1, 9, 8.8, 8.6, 8.4, 8.2, 8, 7.8, 7.6, 7.4, 
7.2, 6, 5.75, 5.5, 5.25, 5, 4.75, 10, 8.9, 7.8, 6.7, 5.6, 4.5, 
3.4, 2.3, 1.2, 0.1, 6, 5.8, 5.7, 5.6, 5.5, 5.5, 5.4, 5.6, 5.8, 
5.1, 6, 5.5, 5.4, 5.3, 5.2, 5.1), decile = c(1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L), time = c(2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L)), .Names = c("name", "amount", 
"decile", "time"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-78L), spec = structure(list(cols = structure(list(name = structure(list(), class = c("collector_character", 
"collector")), amount = structure(list(), class = c("collector_double", 
"collector")), decile = structure(list(), class = c("collector_integer", 
"collector")), time = structure(list(), class = c("collector_integer", 
"collector"))), .Names = c("name", "amount", "decile", "time"
)), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"))

I ultimately want to produce a ggplot graph which details the average 'amount' for each year by quintiles (i.e. 5 little bar graphs for each year of data).

To achieve this, I need to be able calculate the quintiles (averaging all the values in deciles 1 and 2, 3 and 4, 5 and 6, 7 and 8 and 9 and 10, and also including 95% CI as well.

I have in the past tried to filter my data, but I am struggling how to conceptualise this with if statements.

Any help would be appreciated.

2
  • 2
    Is a quintile really the average of two deciles? Nov 28, 2018 at 14:06
  • 1
    Right, quintile would usually mean the 20, 40 etc. marks, with decile meaning the 10, 20, etc. marks. So the first quintile is the second decile, the second quintile is the 4th decile, etc. Nov 28, 2018 at 14:11

2 Answers 2

6

You can do this with dplyr functions using the pipe, converting decile to quintile by dividing by 2 and rounding. Here I just did a very quick and dirty confidence interval of 2 x standard deviation but you might want some other method.

library(dplyr)
library(ggplot2)

plot_data <- df1 %>% 
  mutate(quintile = ceiling(decile/2)) %>% 
  group_by(time, quintile) %>% 
  summarize(average_amount = mean(amount),
            sd_amount = sd(amount),
            ci_min = average_amount - 2 * sd_amount,
            ci_max = average_amount + 2 * sd_amount)

And here is a(n ugly) ggplot with the bar plots by year and quintile.

ggplot(plot_data, aes(x = quintile, y = average_amount)) + 
  geom_col() + 
  geom_errorbar(aes(ymin = ci_min, ymax = ci_max)) +
  facet_wrap(~ time)

enter image description here

2
  • 1
    The standard deviation for the sample quintile as an estimator of the population quintile would not be the standard deviation between sample quintiles, and the distribution of the sample quintile wouldn't be normal anyway, so this "confidence interval" really doesn't have any meaning. Nov 28, 2018 at 14:15
  • @IceCreamToucan very true. This is really just to illustrate how to plot error bars. I would say the boxplots in Jordo82 's answer are probably better for visualizing the data.
    – qdread
    Nov 28, 2018 at 15:25
1

If you're just looking for averages, try this:

library(tidyverse)

df1 %>% 
  mutate(quintile = floor((decile - 1) / 2) + 1) %>% 
  group_by(time, quintile) %>% 
  summarise(AvgAmount = mean(amount)) %>% 
  ggplot(aes(quintile, AvgAmount)) + 
  geom_bar(stat = "identity") + 
  facet_grid(time ~ .)

my plot

If you want to get a better sense of the distribution within quintiles, we could use a box plot:

df1 %>% 
  mutate(quintile = floor((decile - 1) / 2) + 1) %>% 
  ggplot(aes(quintile, amount, group = quintile)) + 
  geom_boxplot() + 
  facet_grid(time ~ .)

box plot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.