For a sample dataframe:
df <- structure(list(year = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L), letter_group = c("A", "A", "A", "B", "B", "B", "C",
"C", "C", "C", "A", "A", "A", "B", "B", "B", "C", "C", "C", "C",
"A", "A", "A", "B", "B", "B", "C", "C", "C", "C", "C", "C", "C",
"A", "A", "A", "B", "B", "B", "C", "C", "C", "C", "C"), value = c(2L,
3L, 4L, 5L, 6L, 6L, 7L, 8L, 5L, 6L, 7L, 3L, 4L, 5L, 6L, 4L, 5L,
6L, 2L, 3L, 4L, 4L, 5L, 6L, 7L, 8L, 5L, 3L, 2L, 4L, 5L, 6L, 4L,
3L, 4L, 5L, 6L, 7L, 1L, 2L, 4L, 5L, 6L, 4L)), .Names = c("year",
"letter_group", "value"), row.names = c(NA, -44L), class = c("tbl_df",
"tbl", "data.frame"), spec = structure(list(cols = structure(list(
year = structure(list(), class = c("collector_integer", "collector"
)), letter_group = structure(list(), class = c("collector_character",
"collector")), value = structure(list(), class = c("collector_integer",
"collector"))), .Names = c("year", "letter_group", "value"
)), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"))
I am trying to create a box plot which comprises the years on the x axes - but also the 'letter-groups' grouped by year...
i.e. A, B, C for year 1, then a small space then A, B C for year 2 and so on....
I have the following:
library(ggplot2)
p1 <- ggplot(df, aes(year, value))
p1 + geom_boxplot(aes(group=letter_group))
But this is only producing the 3 box plots.
Could someone please help me?