While I am quite familiar with using R somehow I have managed to go years without ever needing to graph anything. I am experiencing a tremendous amount of unexpected difficulty with adding lines using the geom_line. I have been trying for a bit now, and decided it was time to ask someone else for feedback. Below I have copied a repex that illustrates a simplified version of the code I am trying to write.
In this code I have measurements taken from five trees over the past six years. My hope is to add a line of code that will allow me to show five different colored lines (one for each tree) that connects the measurements taken for that tree over the past six years. The scatterplot that is produced by the repex has the appropriate axes and coloring, but when I try to add the geom_line() I am getting errors or behavior I am not fully understanding.
Thank you very much for any guidance you have!
#author: Chris Jones #version: 26 Apr 2021 - Repex for adding lines to a plot #import the libraries library(tidyverse) library(xlsx) library(naniar) #create the vectors that go into the data frame trees <- c("1", "2", "3", "4", "5") year_1 <- c(1, 5, 6, 7, 4) year_2 <- c(8, 4, 7, 1, 2) year_3 <- c(5, 2, 6, 7, 40) year_4 <- c(2, 3, 6, 3, 8) year_5 <- c(4, 7, 2, 6, 2) year_6 <- c(9, 4, 6, 5, 6) site_frame <- data.frame(trees = trees, year_1 = year_1, year_2 = year_2, year_3 = year_3, year_4 = year_4, year_5 = year_5, year_6 = year_6) #pivot longer so the data can be graphed longer_data <- pivot_longer(site_frame, cols = !trees, names_to = "year", values_to = "count") #make a ggplot of the data plot_1 <- ggplot(longer_data, aes(x = year, y = count)) + geom_point(aes(col = trees)) plot_1