I have a dataset, from which I am trying to create a graph that plots the development of four variables over five years. The year variable is character, but the other ones are numeric. When I try to plot the a ggplot, I get the error message:
Error: Discrete value supplied to continuous scale
Code for creating the ggplot:
ggp <- ggplot(yearlywindhcgasbio, 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)
Data:
year Wind Hard_coal Gas Bio
1: 2015 236.2378 591.1061 596.0468 883.9906
2: 2016 325.8156 811.5624 454.8719 841.1440
3: 2018 615.1742 681.8199 570.9216 731.3470
4: 2019 647.8811 532.7532 512.6783 678.8823
5: 2020 821.2766 344.1962 472.8535 680.0227
How can I fix this?
CodePudding user response:
Since your year
column is of class character
, ggplot
is giving out the error
Error: Discrete value supplied to continuous scale
You need to add the following line to make it work.
scale_x_discrete()
Your code will look like this:
ggp <- ggplot(yearlywindhcgasbio, 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()