-2

I try to create a multiple-line graph to compare three different countries. Here is my data list.

library(tidyverse)
tabl3 = tribble(
    ~timeperiod, ~China, ~USA, ~Vietnam,
    "Q1_2020", 59683.16, 16027.11, 42.46,
    "Q2_2020", 85788.56,  1428099, 301.53,
    "Q3_2020", 88738.21,  5187437, 785.5,
    "Q4_2020", 92259.62, 12146383, 1268.5,
    "Q1_2021", 97132.93, 27002382, 2098.53,
    "Q2_2021", 100003.2, 32735900, 6331.2,
    "Q3_2021", 104605.7, 37638364, 329380.4,
    "Q1_2022", 132851.8, 74322951, 3842187,
    "Q2_2022", 753897.1, 82788872, 10567360
)

This is the code that I try to write

myplot <- ggplot(data = table3, aes(x=timeperiod, y=China, group=1))
myplot <- myplot + geom_line(aes(y = China))
myplot <- myplot + geom_line(aes(y = USA))
myplot <- myplot + geom_line(aes(y = Vietnam))
myplot <- myplot + theme_classic()
myplot <- myplot + scale_color_manual(name = "Variable", labels = c("China", "USA", "Vietnam"), values = c("red","blue","yellow"))
myplot

Here is my error message:

Error: Discrete value supplied to continuous scale

  • Please use `dput(table3)` and add the output to your question so we can easily copy & paste it. As per your question, see [this post](https://stackoverflow.com/questions/70796796/r-error-discrete-value-supplied-to-continuous-scale). – Donald Seinen Aug 04 '22 at 14:43

1 Answers1

0

You can improve your code by transforming your data into long format using tidr::pivot_longer() then getting one plot:

Original Data

table3 <- read.table(text = "row timeperiod    China      USA  Vietnam
1     Q1_2020 59683.16 16027.11    42.46
2     Q2_2020 85788.56  1428099   301.53
3     Q3_2020 88738.21  5187437    785.5
4     Q4_2020 92259.62 12146383   1268.5
5     Q1_2021 97132.93 27002382  2098.53
6     Q2_2021 100003.2 32735900   6331.2
7     Q3_2021 104605.7 37638364 329380.4
9     Q1_2022 132851.8 74322951  3842187
10    Q2_2022 753897.1 82788872 10567360", header = TRUE)

Pivot Code:

# Convert from wide to long
table3_long <- tidyr::pivot_longer(table3, -c(row, timeperiod))

Figure code:

ggplot(data = table3_long,
       aes(x = timeperiod, y = value, 
           group = name, color = name)) +
  geom_line() + theme_classic() +
  scale_color_manual(name = "Variable",
    labels = c("China", "USA", "Vietnam"),
    values = c("red", "blue", "yellow"))

Output:

enter image description here

jpsmith
  • 11,023
  • 5
  • 15
  • 36