Novice Trouble with ggplot geom_line()

Hi everyone,

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
suppressPackageStartupMessages({
  library(ggplot2)
  library(tidyr)
  })
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, color = trees, group = trees)) +
  geom_line() + theme_minimal()
plot_1

Thank you very much!

This is exactly what I had wanted. Very elegant solution! I knew it shouldnt have been a complicated fix. For others who have stumbled on this thread for the same problem I see that this resource looks very promising Aesthetics: grouping — aes_group_order • ggplot2

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.