0

I am new to R and I would love some assistance on this. I am using this dataset: https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-01-29/clean_cheese.csv

I am trying to first find the Average of the following Cheeses: Cheddar, American, Mozzarella, Italian, Swiss, Muenster, and Blue. Then I would like to place them into a line graph but show them all at once. I would like to show the average consumption of these cheeses.

The following is my code and what I have so far. I am new at this so this might like horrible to some.

line_3 <- clean_cheese %>%
  select(c(Year, Cheddar, Mozzarella, `American Other`, `Italian other`, Swiss, Muenster, Blue)) %>%
  group_by(Year) %>%
  summarise(avg_cheddar_cheese = mean(Cheddar), avg_mozz_cheese = mean(Mozzarella), avg_american_other = mean(`American Other`), avg_italin_other = mean(`Italian other`), avg_swiss_cheese = mean(Swiss), avg_muenster = mean(Muenster), avg_blue = mean(Blue)) %>%
  pivot_longer(-c(Year)) +
  ggplot(aes(x = Year, y = value, color=name,group=name)) +
  geom_line() +
  facet_wrap(.~name,scales = 'free_y')


ggplotly(line_3)
Marce
  • 91
  • 7
  • Do you wanna show them all in the same graph? If that's the case, just remove the line `facet_wrap(.~name, scales = 'free_y')` from your code. If not, what exactly would you like to do? – Joao Pedro Macalos Nov 07 '20 at 21:45
  • @JoaoPedroMacalos Yes I am trying to show them all in the same graph! But removing the `facet_wrap` did not help. :( – Marce Nov 07 '20 at 22:19
  • Strange. It worked on my machine. Don't forget to remove the plus after `geom_line()` as well. – Joao Pedro Macalos Nov 07 '20 at 22:29
  • @JoaoPedroMacalos This is the error I get "Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class uneval Did you accidentally pass `aes()` to the `data` argument?" – Marce Nov 08 '20 at 00:30
  • 1
    The problem in your code is that you added a `+` instead of a `%>%` after `pivot_longer(-c(Year))`. Change that and it should work. – Joao Pedro Macalos Nov 08 '20 at 08:14

1 Answers1

1

You can try :

library(tidyverse)

clean_cheese <- read.csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-01-29/clean_cheese.csv')

line_3 <- clean_cheese %>%
  group_by(Year) %>%
  summarise(across(Cheddar:Blue, mean)) %>%
  pivot_longer(cols = -Year) %>%
  ggplot(aes(x = Year, y = value, color=name,group=name)) +
  geom_line() +
  facet_wrap(.~name,scales = 'free_y')

line_3

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213