-1

I am trying to create a bar chart from a data-frame that will show the mean of my four categories (one bar for each), with the standard error shown as error lines at the top of each bar.

The data is composed of four measurements each for twenty trees, with the twenty trees in the table having it's own row and four columns for the four measurements for each tree (hope this is clear).

I currently have the data in R, and can see the means for my four categories with the summary command, but do not where to go on from here. Below is the head of my data-frame, hope this helps.

    Tree Avg_number_1m Avg_number_2m Avg_number_3m Avg_number_4m
1 Tree_1          15.2          15.0          15.2          12.0
2 Tree_2          16.2          15.4          14.2          15.4
3 Tree_3          14.4           9.2           3.2           1.6
4 Tree_4          14.6           5.6          10.4           9.2
5 Tree_5          15.2          13.0           7.4           3.0
  • 3
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 27 '20 at 17:12
  • Those 2 links might help: https://stackoverflow.com/questions/46248275/plot-using-mean-and-standard-error-values-using-ggplot2 http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/ – AntoniosK Feb 27 '20 at 17:33
  • @MrFlick Thanks for the reply, I've added the head of the data-frame into my original post - hope this helps. – Lepidopteramazing Feb 27 '20 at 17:39

1 Answers1

0

To prepare data for plotting, would first convert from wide to long. It sounds like you want to plot each of the 4 categories (4 measurements for each tree) separately. If so, would group_by each of these measurements, and then calculate mean, standard deviation, and standard error for each measurement. Please let me know if this is what you had in mind.

library(tidyverse)
library(ggplot2)

df %>%
  pivot_longer(cols = -Tree, names_to = "measure", values_to = "value", names_pattern = "^Avg_number_(\\d)m") %>%
  group_by(measure) %>%
  summarise(mean = mean(value),
            SD = sd(value),
            n = n(),
            SE = SD/sqrt(n)) %>%
  ggplot(aes(x = measure, y = mean)) +
  geom_bar(stat = "identity", position = "dodge", width = .5) +
  geom_errorbar(aes(ymax = mean + SE, ymin = mean - SE), position = position_dodge(.5), width = .2)

Plot

barplot with error bars

Ben
  • 28,684
  • 5
  • 23
  • 45
  • Hi Ben, thanks for your reply. That's exactly what I was trying to do, I can see now why I was having trouble as that is longer than any code I've made before! Thank you very much for your help, I'll now apply this to the other 15 tree samples that I have. – Lepidopteramazing Feb 27 '20 at 19:37
  • That's exactly what I was trying to do, I can see now why I was having trouble as that is longer than any code I've made before! Thank you very much for your help, I'll now apply this to the other 15 tree samples that I have. – Lepidopteramazing Feb 27 '20 at 19:48