I just need some help in terms of plotting. So my data frame is below and I'm trying to plot growth trajectory for plants using height of plants at different time points but the data frame is in a weird format. Below is the sample data frame
| Plant ID | Height at Day 1 | Height at Day 5 | Height at Day 10 | Type of Plant |
|---|---|---|---|---|
| 001 | 2 | 9 | 15 | Sunflower |
| 002 | 5 | 6 | 5 | Tulip |
Essentially I want 2 line graphs showing the average heights for Sunflower and Tulips across the time points with the x-axis being the Days (1, 5, 10) and the y-axis being the height (cm) with 2 lines one for the Sunflower and one for Tulips keeping in mind we have 100s of sunflowers and 100s of tulips samples with 300 lines of data.
The alternative I guess could be a box plot for the various time points and simply show all 3 time points by category for a total of 6 box plots, but
So my attempts so far include trying to melt the data so that facet_wraps could work, but it's a bit confusing. I seem to be overthinking it when it seems like such a simple idea and my data frame is not overly complicated.
Thanks
11 Answer
Here is the code. Let's suppose your data is called df! I scraped the table, so my column names are different than yours. In my case, there are no spaces between column names, but dots. You can get my df by running the code at the bottom of the answer. You need to install package rvest!
library(tidyverse) df <- df %>% pivot_longer(cols=2:4, names_to = "Height.at.Day") %>% mutate(day = str_match_all(Height.at.Day, "[0-9]+")%>% unlist %>% as.numeric ) ggplot(data = df, aes(x=day, y=value))+ geom_line() + facet_wrap(~ Type.of.Plant) + ylab(label = "cm") Downloading data:
library(rvest) library(tidyverse) url <-"" df <- read_html(url) %>% html_table() %>% as.data.frame() df