Home > Net >  Line plots from two different data frames and Error- Discrete value supplied to continuous scale
Line plots from two different data frames and Error- Discrete value supplied to continuous scale

Time:04-05

I am trying to create a line plot taking y values from two different data frames. However, I keep on encountering the error: Discrete value supplied to continuous scale. I have tried using as.numeric and have tried to scale the axis but it still does not work for me.

Example of the two dataframes:

structure(list(Years = 1961:1980, GDP.growth = c(13.2341274874115, 
6.56992026378001, 11.6494473111434, 7.70627301508087, 3.2139048259846, 
1.42190836629628, 6.46229426379345, 5.3432872663989, 0.068147071333243, 
-5.54665996976902, 10.3970656983541, 4.29783746387267, -2.63683712200368, 
3.84659112655312, -1.79430023712248, 10.9319135379163, 13.5245271383117, 
5.17203033856374, -0.0649726864368176, 2.38241807770588)), row.names = c(NA, 
20L), class = "data.frame")
structure(list(GCF.at.Mp = c(8.8, 8.8, 8, 6.7, 8.3, 9.2, 10, 
9.5, 8.1, 7.4, 7.6, 8.9, 8.4, 8.5, 8.4, 8.8, 9.3, 9.9, 10.1, 
10.3), Years = c("1950", "1951", "1952", "1953", "1954", "1955", 
"1956", "1957", "1958", "1959", "1960", "1961", "1962", "1963", 
"1964", "1965", "1966", "1967", "1968", "1969")), row.names = c(NA, 
20L), class = "data.frame")

The base code that I have tried to use:

ggplot()   
  geom_line(data=WBDATA, aes(x=Years), y=GDP.growth), color='darkblue')   
  geom_line(data=ES.DATA, aes(x=Years, y=GCF.at.Mp), color='darkred', linetype= "twodash")

CodePudding user response:

Convert Years of your second dataframe to numeric, which can be done within ggplot:

ggplot()   
  geom_line(data=WBDATA, aes(x=Years, y=GDP.growth), color='darkblue')   
  geom_line(data=ES.DATA, aes(x=as.numeric(Years), y=GCF.at.Mp), color='darkred', linetype= "twodash")

enter image description here

CodePudding user response:

A different solution would be first convert your years from your WBDATA as character. Next set in the aes of both lines group = 1. To add a second scale (not necessary) use scale_y_continuous with sec.axis. And finally if you want, you can rotate the years on x-axis in the theme. You can use the following code:

WBDATA$Years <- as.character(WBDATA$Years)

library(tidyverse)
ggplot()   
  geom_line(data=WBDATA, aes(x=Years, y=GDP.growth, group = 1), color='darkblue')   
  geom_line(data=ES.DATA, aes(x=Years, y=GCF.at.Mp, group = 1), color='darkred', linetype= "twodash")  
  scale_y_continuous(
    "GCF", 
    sec.axis = sec_axis(~ ., name = "GDP")
  )   theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

Output:

enter image description here

CodePudding user response:

df2$Years is of class character (discrete scale) and not double (continuous scale). You can convert everything to character before joining them together. Then you can convert the types back. Its easier if you have one merged data frame for plotting, because they both share the same x axis:

library(tidyverse)

df1 <- structure(list(Years = 1961:1980, GDP.growth = c(
  13.2341274874115,
  6.56992026378001, 11.6494473111434, 7.70627301508087, 3.2139048259846,
  1.42190836629628, 6.46229426379345, 5.3432872663989, 0.068147071333243,
  -5.54665996976902, 10.3970656983541, 4.29783746387267, -2.63683712200368,
  3.84659112655312, -1.79430023712248, 10.9319135379163, 13.5245271383117,
  5.17203033856374, -0.0649726864368176, 2.38241807770588
)), row.names = c(
  NA,
  20L
), class = "data.frame")

df2 <- structure(list(GCF.at.Mp = c(
  8.8, 8.8, 8, 6.7, 8.3, 9.2, 10,
  9.5, 8.1, 7.4, 7.6, 8.9, 8.4, 8.5, 8.4, 8.8, 9.3, 9.9, 10.1,
  10.3
), Years = c(
  "1950", "1951", "1952", "1953", "1954", "1955",
  "1956", "1957", "1958", "1959", "1960", "1961", "1962", "1963",
  "1964", "1965", "1966", "1967", "1968", "1969"
)), row.names = c(
  NA,
  20L
), class = "data.frame")



df <-
  list(df1, df2) %>%
  # unify column classes
  map(~ .x %>% mutate(across(everything(), as.character))) %>%
  reduce(full_join) %>%
  as_tibble() %>%
  type_convert()
#> Joining, by = "Years"
#> ── Column specification
#> ──────────────────────────────────────────────────────── cols( Years =
#> col_double(), GDP.growth = col_double(), GCF.at.Mp = col_double() )

df %>%
  ggplot(aes(Years))  
  geom_line(aes(y = GDP.growth), color = "darkblue")  
  geom_line(aes(y = GCF.at.Mp), color = "darkred")
#> Warning: Removed 11 row(s) containing missing values (geom_path).
#> Removed 11 row(s) containing missing values (geom_path).

Created on 2022-04-04 by the reprex package (v2.0.0)

  • Related