Home > Software design >  ggplot graph inconsistent with observations
ggplot graph inconsistent with observations

Time:12-13

I tried to create a graph that visualizes the evolution of 4 variables. The ggplot produced, however, is not consistent with the observations. What might be the problem here?

Data <- data.frame(
Wind = c(236,325,470,615,647,821),
Hard_coal= c(591,811,667,681,532,344),
Gas= c(883,841,472,731,678,680),
Bio = c(883,841,811,731,678,680),
year= c("2015","2016","2017","2018","2019","2020"))


#create the plot
ggp <- ggplot(Data, aes(year)) geom_line(aes(y = Wind, (size = 1.5)), group = 1) geom_line(aes(y = Hard_coal), group = 2) geom_line(aes(y = Gas), group = 3) geom_line(aes(y = Bio), group = 4) scale_x_discrete()

#plot

ggp

CodePudding user response:

You have to reshape the data in tidy format

library(tidyverse)

Data <- data.frame(
  Wind = c(236,325,470,615,647,821),
  Hard_coal= c(591,811,667,681,532,344),
  Gas= c(883,841,472,731,678,680),
  Bio = c(883,841,811,731,678,680),
  year= c("2015","2016","2017","2018","2019","2020"))

Data %>% 
  pivot_longer(-year) %>% 
  ggplot(aes(x = year, y = value, color = name, group = name, linetype = name))  
  geom_line(size = 1.5)

Created on 2021-12-12 by the reprex package (v2.0.0)

  • Related