barnsm2
barnsm2

Reputation: 195

Creating bar plot of multiple years in R

I have a dataset with data for multiple years. I have taken each year and obtained the mean, which is shown below.

## structure(list(X = 1:10, Sector = c("B01", "B02", "B03", "B04", 
 "B05", "B06", "B07", "B08", "B09", "B10"), Octant = c("NW", "N", 
 "NE", "NW", "NW", "N", "N", "NE", "NE", "W"), Distance_m = c(1600L, 
 1600L, 1600L, 3200L, 3200L, 3200L, 3200L, 3200L, 3200L, 4800L
 ), Yr1951 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Yr1952 = c(4.16722556361885, 
 8.07009038922937, 19.9170847727104, 3.14049476463692, 3.14049476463692, 
 6.08176260934446, 6.08176260934446, 15.0098667568185, 15.0098667568185, 
 4.65320683317187), Yr1953 = c(15.7901552659006, 30.578613590843, 
 75.4684036666576, 11.8997398120925, 11.8997398120925, 23.0445831227103, 
 23.0445831227103, 56.8743215341634, 56.8743215341634, 17.6316009916985
 ), Yr1954 = c(23.2458594045635, 45.0170464032273, 111.102637787894, 
 17.5184900949112, 17.5184900949112, 33.9256410267316, 33.9256410267316, 
 83.7289095546898, 83.7289095546898, 25.9567883170532), Yr1955 = c(23.2458594045635, 
 45.0170464032273, 111.102637787894, 17.5184900949112, 17.5184900949112, 
 33.9256410267316, 33.9256410267316, 83.7289095546898, 83.7289095546898, 
 25.9567883170532), Yr1956 = c(23.2458594045635, 45.0170464032273, 
 111.102637787894, 17.5184900949112, 17.5184900949112, 33.9256410267316, 
 33.9256410267316, 83.7289095546898, 83.7289095546898, 25.9567883170532
 ), Yr1957 = c(23.2458594045635, 45.0170464032273, 111.102637787894, 
 17.5184900949112, 17.5184900949112, 33.9256410267316, 33.9256410267316, 
 83.7289095546898, 83.7289095546898, 25.9567883170532), Yr1958 = c(24.6439039639345, 
 47.724446276369, 117.78453479105, 18.5720811598549, 18.5720811598549, 
 35.9659853751658, 35.9659853751658, 88.7645051172278, 88.7645051172278, 
 27.517872639807), Yr1959 = c(28.8380376420477, 55.8466458957943, 
 137.830225800518, 21.7328543546861, 21.7328543546861, 42.0870184204684, 
 42.0870184204684, 103.871291804842, 103.871291804842, 32.2011256080683
 ), Yr1960 = c(28.8380376420477, 55.8466458957943, 137.830225800518, 
 21.7328543546861, 21.7328543546861, 42.0870184204684, 42.0870184204684, 
 103.871291804842, 103.871291804842, 32.2011256080683), Yr1961 = c(28.8380376420477, 
 55.8466458957943, 137.830225800518, 21.7328543546861, 21.7328543546861, 
 42.0870184204684, 42.0870184204684, 103.871291804842, 103.871291804842, 
 32.2011256080683), Yr1962 = c(28.8380376420477, 55.8466458957943, 
 137.830225800518, 21.7328543546861, 21.7328543546861, 42.0870184204684, 
 42.0870184204684, 103.871291804842, 103.871291804842, 32.2011256080683
 ), Yr1963 = c(28.8380376420477, 55.8466458957943, 137.830225800518, 
 21.7328543546861, 21.7328543546861, 42.0870184204684, 42.0870184204684, 
 103.871291804842, 103.871291804842, 32.2011256080683), Yr1964 = c(28.8380376420477, 
 55.8466458957943, 137.830225800518, 21.7328543546861, 21.7328543546861, 
 42.0870184204684, 42.0870184204684, 103.871291804842, 103.871291804842, 
 32.2011256080683), Yr1965 = c(28.8380376420477, 55.8466458957943, 
 137.830225800518, 21.7328543546861, 21.7328543546861, 42.0870184204684, 
 42.0870184204684, 103.871291804842, 103.871291804842, 32.2011256080683
 ), Yr1966 = c(28.8380376420477, 55.8466458957943, 137.830225800518, 
 21.7328543546861, 21.7328543546861, 42.0870184204684, 42.0870184204684, 
 103.871291804842, 103.871291804842, 32.2011256080683), Yr1967 = c(28.8380376420477, 
 55.8466458957943, 137.830225800518, 21.7328543546861, 21.7328543546861, 
 42.0870184204684, 42.0870184204684, 103.871291804842, 103.871291804842, 
 32.2011256080683), Yr1968 = c(28.8380376420477, 55.8466458957943, 
137.830225800518, 21.7328543546861, 21.7328543546861, 42.0870184204684, 
 42.0870184204684, 103.871291804842, 103.871291804842, 32.2011256080683
 ), Yr1969 = c(28.8380376420477, 55.8466458957943, 137.830225800518, 
 21.7328543546861, 21.7328543546861, 42.0870184204684, 42.0870184204684, 
 103.871291804842, 103.871291804842, 32.2011256080683), Yr1970 = c(28.8380376420477, 
 55.8466458957943, 137.830225800518, 21.7328543546861, 21.7328543546861, 
 42.0870184204684, 42.0870184204684, 103.871291804842, 103.871291804842, 
 32.2011256080683), Yr1971 = c(28.8380376420477, 55.8466458957943, 
 137.830225800518, 21.7328543546861, 21.7328543546861, 42.0870184204684, 
 42.0870184204684, 103.871291804842, 103.871291804842, 32.2011256080683
 ), Yr1972 = c(28.8380376420477, 55.8466458957943, 137.830225800518, 
 21.7328543546861, 21.7328543546861, 42.0870184204684, 42.0870184204684, 
 103.871291804842, 103.871291804842, 32.2011256080683), Yr1973 = c(28.8380376420477, 
 55.8466458957943, 137.830225800518, 21.7328543546861, 21.7328543546861, 
 42.0870184204684, 42.0870184204684, 103.871291804842, 103.871291804842, 
 32.2011256080683), Yr1974 = c(28.8380376420477, 55.8466458957943, 
 137.830225800518, 21.7328543546861, 21.7328543546861, 42.0870184204684, 
 42.0870184204684, 103.871291804842, 103.871291804842, 32.2011256080683
 ), Yr1975 = c(28.8380376420477, 55.8466458957943, 137.830225800518, 
 21.7328543546861, 21.7328543546861, 42.0870184204684, 42.0870184204684, 
 103.871291804842, 103.871291804842, 32.2011256080683), Yr1976 = c(28.8380376420477, 
 55.8466458957943, 137.830225800518, 21.7328543546861, 21.7328543546861, 
 42.0870184204684, 42.0870184204684, 103.871291804842, 103.871291804842, 
 32.2011256080683), Yr1977 = c(28.8380376420477, 55.8466458957943, 
 137.830225800518, 21.7328543546861, 21.7328543546861, 42.0870184204684, 
 42.0870184204684, 103.871291804842, 103.871291804842, 32.2011256080683
 ), Yr1978 = c(28.8380376420477, 55.8466458957943, 137.830225800518, 
 21.7328543546861, 21.7328543546861, 42.0870184204684, 42.0870184204684, 
 103.871291804842, 103.871291804842, 32.2011256080683), Yr1979 = c(16.6426332897682, 
 32.2294900798683, 79.5427876444474, 12.5421823029183, 12.5421823029183, 
 24.2887127940458, 24.2887127940458, 59.9448492404336, 59.9448492404336, 
 18.5834948849451), Yr1980 = c(4.44722893748883, 8.61233426394242, 
 21.2553494883767, 3.35151025115061, 3.35151025115061, 6.49040716762317, 
 6.49040716762317, 16.0184066760251, 16.0184066760251, 4.96586416182185
 ), Yr1981 = c(4.44722893748883, 8.61233426394242, 21.2553494883767, 
 3.35151025115061, 3.35151025115061, 6.49040716762317, 6.49040716762317, 
 16.0184066760251, 16.0184066760251, 4.96586416182185), Yr1982 = c(4.44722893748883, 
 8.61233426394242, 21.2553494883767, 3.35151025115061, 3.35151025115061, 
 6.49040716762317, 6.49040716762317, 16.0184066760251, 16.0184066760251, 
 4.96586416182185), Yr1983 = c(4.44722893748883, 8.61233426394242, 
 21.2553494883767, 3.35151025115061, 3.35151025115061, 6.49040716762317, 
 6.49040716762317, 16.0184066760251, 16.0184066760251, 4.96586416182185
 ), Yr1984 = c(4.44722893748883, 8.61233426394242, 21.2553494883767, 
 3.35151025115061, 3.35151025115061, 6.49040716762317, 6.49040716762317, 
 16.0184066760251, 16.0184066760251, 4.96586416182185), Yr1985 = c(4.44722893748883, 
 8.61233426394242, 21.2553494883767, 3.35151025115061, 3.35151025115061, 
 6.49040716762317, 6.49040716762317, 16.0184066760251, 16.0184066760251, 
 4.96586416182185), Yr1986 = c(4.44722893748883, 8.61233426394242, 
 21.2553494883767, 3.35151025115061, 3.35151025115061, 6.49040716762317, 
 6.49040716762317, 16.0184066760251, 16.0184066760251, 4.96586416182185
 ), Yr1987 = c(4.44722893748883, 8.61233426394242, 21.2553494883767, 
 3.35151025115061, 3.35151025115061, 6.49040716762317, 6.49040716762317, 
 16.0184066760251, 16.0184066760251, 4.96586416182185), Yr1988 = c(1.1104088237638, 
 2.15037545723583, 5.30715372557456, 0.836823695861693, 0.836823695861693, 
 1.62056091333541, 1.62056091333541, 3.99956475497726, 3.99956475497726, 
 1.23990454739508)), class = "data.frame", row.names = c(NA, -10L
 ))

I need to create a bar plot containing individual bars for each year. How would I do this? The y axis can be named exposure, and the x would be year. I have not had experience beyond very simple barplots. I was also wondering how I could go about combining all of these years together and getting the overall mean and median.

Upvotes: 1

Views: 510

Answers (1)

jdobres
jdobres

Reputation: 11957

For operations such as this, the tidyverse set of packages comes in very handy. tidyverse also includes ggplot, which is a great library for creating visualizations. There are a lot of great resources for learning more about the tidyverse, such as this one.

Here, I've named your data df. First, let's pivot the data into "long" format, in which each row contains one data value of interest, and then calculate the mean per year:

library(tidyverse)

df_means <- df %>% 
  pivot_longer(starts_with('Yr'), names_to = 'year') %>% 
  mutate(year_numeric = as.numeric(gsub('Yr', '', year))) %>% 
  group_by(year_numeric) %>% 
  summarize(value = mean(value, na.rm = T))

   year_numeric value
          <dbl> <dbl>
 1         1951  0   
 2         1952  8.53
 3         1953 32.3 
 4         1954 47.6 
 5         1955 47.6 
 6         1956 47.6 
 7         1957 47.6 
 8         1958 50.4 
 9         1959 59.0 
10         1960 59.0 
# … with 28 more rows

We can do a similar operation without the group_by command to get the grand mean and median:

df_overall <- df %>% 
  pivot_longer(starts_with('Yr'), names_to = 'year') %>% 
  summarize(
    grand_mean = mean(value, na.rm = T),
    grand_median = median(value, na.rm = T)
  ) %>% 
  pivot_longer(everything(), names_to = 'measure')

  measure      value
  <chr>        <dbl>
1 grand_mean    41.3
2 grand_median  28.8

And finally, we can create a plot with ggplot. Note how little plotting code is required to create something relatively complex:

plot_means <- df_means %>% 
  ggplot(data = ., aes(x = year_numeric, y = value)) +
  geom_col() +
  geom_hline(data = df_overall, aes(color = measure, yintercept = value)) +
  labs(x = 'Year', y = 'Exposure')
print(plot_means)

enter image description here

Upvotes: 1

Related Questions