1

I am trying to recreate the following figure using ggplot but I am running into some conceptual issues that I'm hoping someone can help me resolve? This is the initial plot I'm working to recreate:

enter image description here

And this is the plot that I made using the same data: enter image description here

And this is the code I wrote to try to generate it:

fig6 <- ggplot(PS78_89, aes(x = YearMonth, y= OrcaDays)) +
    geom_col(position = "dodge", fill = "#ffc300") +
    geom_line(aes(y = DecMean), size=0.75, color = "#333533",
              position = position_dodge2(width = 0.9)) +
    labs(y="No. of Days/Month", x = "Months of Each Year") +
    scale_y_continuous(expand = expansion(mult = c(0.003, .1))) +
    theme_bw() +
    theme(panel.grid.major.y = element_line(color = "darkgray"),
          panel.grid.major.x = element_line(colour = "lightgray"),
          panel.grid.minor = element_blank(),
          panel.background = element_rect(fill = alpha("lightgray", alpha = 0.40)),
          axis.line = element_line(color = "#3c4245"),
          axis.title = element_text(size = 18, face = "bold", colour = "#3c4245"),
          axis.title.y = element_text(margin = margin(r=7, unit = "pt")),
          axis.title.x = element_text(margin = margin(t=7, unit = "pt")),
          axis.text.x = element_text(size = 8, face = "bold", angle = 45, hjust = 1),
          axis.text.y = element_text(size = 12, face = "bold"),
          legend.title = element_blank(),
          legend.text = element_text(size=18, face = "bold", margin = margin(l = 3), hjust = 0),
          legend.key.size = unit(2,"line"),
          plot.title = element_text(face = "bold", colour = "#3c4245", hjust = 0.5, size = 28, margin = margin(b=10, unit = "pt")),
          plot.caption = element_text(face = "italic", colour = "#3c4245", size = 20, margin = margin(t = 5), hjust = 0, vjust = 0.5))
fig6

When I try to add the geom_line() element I get this warning: geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic? and the trend line does not populate. The trendline is a decadal mean for each month in a given decadal plot. So the data represented are from one decade, in this case it's 1978-1989 (I know it's technically 12 years, but the db wasn't establish at a "clean" point in time so the first decade is slightly longer...). So this means that every January in every year of the df (Month == 1) the decadal mean is the exact same (DecMean == 3.666667)

This what the df that I'm trying to plot looks like:

> head(PS78_89)
# A tibble: 6 x 5
# Groups:   Decade, YearMonth [6]
  Decade    YearMonth Month OrcaDays DecMean
  <chr>     <chr>     <dbl>    <int>   <dbl>
1 1978-1989 1978-01       1        2    3.67
2 1978-1989 1978-02       2        5    3.91
3 1978-1989 1978-03       3        3    2.83
4 1978-1989 1978-04       4        1    3.17
5 1978-1989 1978-05       5        3    2.11
6 1978-1989 1978-06       6        4    2.14

and this is the dput() of the df:

> dput(PS78_89)
structure(list(Decade = c("1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
"1978-1989", "1978-1989", "1978-1989", "1978-1989"), YearMonth = c("1978-01", 
"1978-02", "1978-03", "1978-04", "1978-05", "1978-06", "1978-07", 
"1978-08", "1978-09", "1978-10", "1978-11", "1978-12", "1979-01", 
"1979-02", "1979-03", "1979-04", "1979-05", "1979-06", "1979-07", 
"1979-08", "1979-09", "1979-10", "1979-11", "1979-12", "1980-01", 
"1980-02", "1980-03", "1980-04", "1980-05", "1980-06", "1980-07", 
"1980-08", "1980-09", "1980-10", "1980-11", "1980-12", "1981-01", 
"1981-02", "1981-03", "1981-04", "1981-05", "1981-06", "1981-07", 
"1981-08", "1981-09", "1981-10", "1981-11", "1981-12", "1982-01", 
"1982-02", "1982-03", "1982-04", "1982-05", "1982-07", "1982-08", 
"1982-09", "1982-10", "1982-11", "1982-12", "1983-01", "1983-02", 
"1983-03", "1983-04", "1983-05", "1983-06", "1983-07", "1983-08", 
"1983-09", "1983-10", "1983-11", "1983-12", "1984-01", "1984-02", 
"1984-03", "1984-04", "1984-06", "1984-07", "1984-09", "1984-10", 
"1984-11", "1985-01", "1985-02", "1985-03", "1985-04", "1985-05", 
"1985-07", "1985-08", "1985-10", "1985-11", "1985-12", "1986-01", 
"1986-03", "1986-04", "1986-08", "1986-09", "1986-10", "1986-11", 
"1986-12", "1987-01", "1987-02", "1987-03", "1987-04", "1987-06", 
"1987-07", "1987-08", "1987-10", "1987-12", "1988-01", "1988-02", 
"1988-03", "1988-04", "1988-05", "1988-07", "1988-08", "1988-09", 
"1988-11", "1989-01", "1989-02", "1989-03", "1989-04", "1989-05", 
"1989-07", "1989-08", "1989-09", "1989-10", "1989-11", "1989-12"
), Month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 
4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 
7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 
2, 3, 4, 6, 7, 9, 10, 11, 1, 2, 3, 4, 5, 7, 8, 10, 11, 12, 1, 
3, 4, 8, 9, 10, 11, 12, 1, 2, 3, 4, 6, 7, 8, 10, 12, 1, 2, 3, 
4, 5, 7, 8, 9, 11, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12), OrcaDays = c(2L, 
5L, 3L, 1L, 3L, 4L, 9L, 17L, 6L, 8L, 4L, 2L, 1L, 18L, 5L, 6L, 
1L, 1L, 6L, 9L, 4L, 6L, 3L, 2L, 1L, 2L, 1L, 4L, 1L, 1L, 5L, 4L, 
7L, 13L, 6L, 4L, 4L, 1L, 1L, 2L, 4L, 3L, 7L, 10L, 8L, 4L, 3L, 
1L, 7L, 4L, 2L, 3L, 3L, 3L, 5L, 7L, 1L, 9L, 6L, 4L, 3L, 1L, 5L, 
2L, 3L, 6L, 6L, 3L, 3L, 4L, 1L, 6L, 1L, 6L, 3L, 2L, 4L, 1L, 3L, 
3L, 2L, 3L, 2L, 5L, 1L, 5L, 6L, 3L, 1L, 4L, 1L, 3L, 1L, 5L, 1L, 
2L, 4L, 6L, 3L, 1L, 1L, 3L, 1L, 5L, 1L, 6L, 7L, 7L, 1L, 8L, 2L, 
1L, 2L, 1L, 3L, 1L, 6L, 4L, 1L, 3L, 3L, 2L, 6L, 1L, 9L, 2L, 6L
), DecMean = c(3.66666666666667, 3.90909090909091, 2.83333333333333, 
3.16666666666667, 2.11111111111111, 2.14285714285714, 4.90909090909091, 
6.36363636363636, 4.1, 5.27272727272727, 3.63636363636364, 3.9, 
3.66666666666667, 3.90909090909091, 2.83333333333333, 3.16666666666667, 
2.11111111111111, 2.14285714285714, 4.90909090909091, 6.36363636363636, 
4.1, 5.27272727272727, 3.63636363636364, 3.9, 3.66666666666667, 
3.90909090909091, 2.83333333333333, 3.16666666666667, 2.11111111111111, 
2.14285714285714, 4.90909090909091, 6.36363636363636, 4.1, 5.27272727272727, 
3.63636363636364, 3.9, 3.66666666666667, 3.90909090909091, 2.83333333333333, 
3.16666666666667, 2.11111111111111, 2.14285714285714, 4.90909090909091, 
6.36363636363636, 4.1, 5.27272727272727, 3.63636363636364, 3.9, 
3.66666666666667, 3.90909090909091, 2.83333333333333, 3.16666666666667, 
2.11111111111111, 4.90909090909091, 6.36363636363636, 4.1, 5.27272727272727, 
3.63636363636364, 3.9, 3.66666666666667, 3.90909090909091, 2.83333333333333, 
3.16666666666667, 2.11111111111111, 2.14285714285714, 4.90909090909091, 
6.36363636363636, 4.1, 5.27272727272727, 3.63636363636364, 3.9, 
3.66666666666667, 3.90909090909091, 2.83333333333333, 3.16666666666667, 
2.14285714285714, 4.90909090909091, 4.1, 5.27272727272727, 3.63636363636364, 
3.66666666666667, 3.90909090909091, 2.83333333333333, 3.16666666666667, 
2.11111111111111, 4.90909090909091, 6.36363636363636, 5.27272727272727, 
3.63636363636364, 3.9, 3.66666666666667, 2.83333333333333, 3.16666666666667, 
6.36363636363636, 4.1, 5.27272727272727, 3.63636363636364, 3.9, 
3.66666666666667, 3.90909090909091, 2.83333333333333, 3.16666666666667, 
2.14285714285714, 4.90909090909091, 6.36363636363636, 5.27272727272727, 
3.9, 3.66666666666667, 3.90909090909091, 2.83333333333333, 3.16666666666667, 
2.11111111111111, 4.90909090909091, 6.36363636363636, 4.1, 3.63636363636364, 
3.66666666666667, 3.90909090909091, 2.83333333333333, 3.16666666666667, 
2.11111111111111, 4.90909090909091, 6.36363636363636, 4.1, 5.27272727272727, 
3.63636363636364, 3.9)), row.names = c(NA, -127L), groups = structure(list(
    Decade = c("1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989", "1978-1989", "1978-1989", 
    "1978-1989", "1978-1989", "1978-1989"), YearMonth = c("1978-01", 
    "1978-02", "1978-03", "1978-04", "1978-05", "1978-06", "1978-07", 
    "1978-08", "1978-09", "1978-10", "1978-11", "1978-12", "1979-01", 
    "1979-02", "1979-03", "1979-04", "1979-05", "1979-06", "1979-07", 
    "1979-08", "1979-09", "1979-10", "1979-11", "1979-12", "1980-01", 
    "1980-02", "1980-03", "1980-04", "1980-05", "1980-06", "1980-07", 
    "1980-08", "1980-09", "1980-10", "1980-11", "1980-12", "1981-01", 
    "1981-02", "1981-03", "1981-04", "1981-05", "1981-06", "1981-07", 
    "1981-08", "1981-09", "1981-10", "1981-11", "1981-12", "1982-01", 
    "1982-02", "1982-03", "1982-04", "1982-05", "1982-07", "1982-08", 
    "1982-09", "1982-10", "1982-11", "1982-12", "1983-01", "1983-02", 
    "1983-03", "1983-04", "1983-05", "1983-06", "1983-07", "1983-08", 
    "1983-09", "1983-10", "1983-11", "1983-12", "1984-01", "1984-02", 
    "1984-03", "1984-04", "1984-06", "1984-07", "1984-09", "1984-10", 
    "1984-11", "1985-01", "1985-02", "1985-03", "1985-04", "1985-05", 
    "1985-07", "1985-08", "1985-10", "1985-11", "1985-12", "1986-01", 
    "1986-03", "1986-04", "1986-08", "1986-09", "1986-10", "1986-11", 
    "1986-12", "1987-01", "1987-02", "1987-03", "1987-04", "1987-06", 
    "1987-07", "1987-08", "1987-10", "1987-12", "1988-01", "1988-02", 
    "1988-03", "1988-04", "1988-05", "1988-07", "1988-08", "1988-09", 
    "1988-11", "1989-01", "1989-02", "1989-03", "1989-04", "1989-05", 
    "1989-07", "1989-08", "1989-09", "1989-10", "1989-11", "1989-12"
    ), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
        9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
        20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 
        31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 
        42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 
        53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 
        64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 
        75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 
        86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 
        97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 
        107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 
        116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 
        125L, 126L, 127L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -127L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

I'd also like to be able to control how the x-axis is being labelled so that it's not as overwhelming and is broken down a bit more like the original figure at the top.

I know I'm super confused by this task, and so I apologize if my post isn't super clear, but please let me know how I can modify my question to make it easier to understand.

Thanks so much for your time!

  • Untested, but you probably want to do something like `geom_line(aes(y = DecMean)...)` because you want the `DecMean` column to be plotted on the y-axis. The error is because you are suggesting that you want each value of `DecMean` to be mapped to a particular colour. There is only one value per colour group, hence `Each group consists of only one observation` – Calum You May 23 '21 at 00:38
  • Ooh thanks @CalumYou for that suggestion! I just tried it and I still get the same error ```Each group consists of only one observation...``` – Salma Abdel-Raheem May 23 '21 at 00:44
  • @CalumYou I just tried it with ```geom_point()``` and the points will display but as soon as I try to connect them with a line it goes back to the same ```Each group...``` error – Salma Abdel-Raheem May 23 '21 at 00:48

1 Answers1

1

Here is a fix. Add group = 1 to the aesthetics for your geom_line(). I don't know exactly why this works, but when you get this error, just create a dummy group in the aesthetics so that all data are in the same group.

ggplot(PS78_89, aes(x = YearMonth)) +
  geom_col(aes(y= OrcaDays), position = "dodge", fill = "#ffc300") +
  geom_line(aes(y = DecMean, group = 1), size=0.75, color = "#333533",
            position = position_dodge2(width = 0.9)) +
  labs(y="No. of Days/Month", x = "Months of Each Year") +
  scale_y_continuous(expand = expansion(mult = c(0.003, .1))) +
  theme_bw() +
  theme(panel.grid.major.y = element_line(color = "darkgray"),
        panel.grid.major.x = element_line(colour = "lightgray"),
        panel.grid.minor = element_blank(),
        panel.background = element_rect(fill = alpha("lightgray", alpha = 0.40)),
        axis.line = element_line(color = "#3c4245"),
        axis.title = element_text(size = 18, face = "bold", colour = "#3c4245"),
        axis.title.y = element_text(margin = margin(r=7, unit = "pt")),
        axis.title.x = element_text(margin = margin(t=7, unit = "pt")),
        axis.text.x = element_text(size = 8, face = "bold", angle = 45, hjust = 1),
        axis.text.y = element_text(size = 12, face = "bold"),
        legend.title = element_blank(),
        legend.text = element_text(size=18, face = "bold", margin = margin(l = 3), hjust = 0),
        legend.key.size = unit(2,"line"),
        plot.title = element_text(face = "bold", colour = "#3c4245", hjust = 0.5, size = 28, margin = margin(b=10, unit = "pt")),
        plot.caption = element_text(face = "italic", colour = "#3c4245", size = 20, margin = margin(t = 5), hjust = 0, vjust = 0.5))

enter image description here

Ben Norris
  • 5,639
  • 2
  • 6
  • 15