Average variables and create line graph

Hello everyone,

I'm very new in R using and trying to explore about this software.I am seeking the advice that how can I average the data based on variable then draw line graphs?
For example, I have a data frame named mydata like this:
Cultivar Period Height
Cultivar A 1 32
Cultivar A 1 23
Cultivar B 1 34
Cultivar B 1 43
Cultivar C 1 43
Cultivar C 1 43
Cultivar A 2 24
Cultivar A 2 43
Cultivar B 2 32
Cultivar B 2 23
Cultivar C 2 33
Cultivar C 2 34
Cultivar A 3 23
Cultivar A 3 33
Cultivar B 3 44
Cultivar B 3 32
Cultivar C 3 42
Cultivar C 3 42
I want to draw the line graph in which x-axis presented by "Period"(3 periods), y-axis is "plant height" and the graph is filled by Cultivar.
I have tried this coding:
data<-mydata %>% group_by(Cultivar) %>% summarize(averaged.ES=mean(Height), sd.ES=sd(Height))
Because I want to average the data based on Cultivar first, but the graph was showing sawtooth pattern.
Your advice would be very appreciated!

Thank you very much!

Hi, welcome!

Is this what you are trying to do?

# Library calls
library(tidyverse)

# Sample data on a copy/paste friendly format
mydata <- data.frame(
  stringsAsFactors = FALSE,
          Cultivar = c("A","A","B","B","C","C",
                       "A","A","B","B","C","C","A","A","B","B","C","C"),
            Period = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3),
            Height = c(32,23,34,43,43,43,24,43,
                       32,23,33,34,23,33,44,32,42,42)
)

# Relevant code
mydata %>% 
    group_by(Period,Cultivar) %>% 
    summarise(mean_heigh = mean(Height, na.rm = TRUE)) %>% 
    ggplot(aes(x = Period, y = mean_heigh, color = Cultivar)) +
    geom_line()

Created on 2020-02-13 by the reprex package (v0.3.0.9001)

If this doesn't solve your problem, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Thank you so much Andresrcs,

I got your idea!

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