-1

I have a data set with 3 columns: date, weight, and location. I want to make a graph with time on the x-axis and weight on the y-axis with a different line for each location, where each point on the line is the mean weight of all samples from that location at that date. The only ways I've been able to come up with to do this would take way too long and require more lines of code than seems reasonable just to make a graph. For instance I tried subsetting like this:

A <- df$Location == "A"
Aug10_19 <- df$Date == "2019/07/10"
ind <- Aug10_19 & A
mean(df$Weight[ind])

But then I would have to do this manually for every individual combination of date and location and then force all the means into a new data frame. What is the shorter way to accomplish this?

rose_t
  • 93
  • 5
  • 1
    Please post some of your data using `dput` or post some sample data that otherwise reproduces your data. – LMc Mar 23 '21 at 19:36

1 Answers1

0

You can use ggplot2 to quickly create summary plots.

library(dplyr)
library(ggplot2)

df <- transmute(
    iris, 
    Location = Species, 
    Date = as.Date(as.character(
        cut(Sepal.Length, breaks = 3, 
            labels = c("2019-07-10", "2019-07-12", "2019-07-15")))), 
    Weight = Sepal.Width)

ggplot(data = df, 
    mapping = aes(x = Date, y = Weight, colour = Location)) + 
    stat_summary(fun = "mean", geom = "line") + 
    theme_bw()

Stat summary adds mean lines to plot

CSJCampbell
  • 2,025
  • 15
  • 19