Reputation: 101
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
Upvotes: 0
Views: 77
Reputation: 2290
I used facet_wrap()
and adjusted the x axis
library(ggplot2)
library(reshape)
df_melt<-melt(df, id.vars=c("Year"))
df_melt<-as.data.frame(df_melt)
ggplot(df_melt, aes(x=Year, y=value, group=variable)) +
geom_line(aes(color=variable), size=3, show.legend = FALSE) +
facet_wrap(~as.factor(variable) )+
labs(x="Year", y="Value", title="") +
theme_bw()+
theme(plot.title = element_text(hjust = 0.5, face="bold", size=20, color="black")) +
theme(axis.title.x = element_text(family="Times", face="bold", size=16, color="black"))+
theme(axis.title.y = element_text(family="Times", face="bold", size=16, color="black"))+
theme(axis.text.x = element_text( hjust = 1, face="bold", size=14, color="black") )+
theme(axis.text.y = element_text( hjust = 1, face="bold", size=14, color="black") )+
theme(plot.title = element_text(hjust = 0.5))+
theme(strip.text = element_text(family="Times", face="bold", size=16, color="black"))
You may want to highlight specific points, to do so use geom_vline()
df_melt<-melt(df, id.vars=c("Year"))
df_melt<-as.data.frame(df_melt)
ggplot(df_melt, aes(x=Year, y=value, group=variable)) +
geom_line(aes(color=variable), size=3, show.legend = FALSE) +
geom_vline(xintercept = c("2016", "2019"),col = "black", lwd = 2, lty=2) +
facet_wrap(~as.factor(variable) )+
labs(x="Year", y="Value", title="") +
theme_bw()+
theme(plot.title = element_text(hjust = 0.5, face="bold", size=20, color="black")) +
theme(axis.title.x = element_text(family="Times", face="bold", size=16, color="black"))+
theme(axis.title.y = element_text(family="Times", face="bold", size=16, color="black"))+
theme(axis.text.x = element_text( hjust = 1, face="bold", size=14, color="black") )+
theme(axis.text.y = element_text( hjust = 1, face="bold", size=14, color="black") )+
theme(plot.title = element_text(hjust = 0.5))+
theme(strip.text = element_text(family="Times", face="bold", size=16, color="black"))
Upvotes: 1
Reputation: 26238
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)
Upvotes: 2