0

I have a dataset in which the first column is named central_government_debt_percent_of_gdp and contains a list of years, then several columns with the name of some countries that contain the debt/GDP ratio for each of them in every year.

You can see some of the data at this link: You can see some of the data at this link

I want to create a graph that shows the evolution of the ratio for each country, with separate lines. How can I do it with ggplot? Do I have to add a geom_line for each country? Should I do some data manipulation ?

kjetil b halvorsen
  • 1,206
  • 2
  • 18
  • 28
Abaco
  • 1
  • 1
  • 3
    Welcome to SO, Abaco! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (please be explicit about non-base packages), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Dec 22 '22 at 00:42
  • 2
    Side note: a column named `central_government_debt_percent_of_gdp` seems like its contents would be a floating-point, indicating a percentage. Why does it contain a list of years? Is it a list-column, or a "regular column"? I think sample data is going to be very useful here, please [edit] and add as requested. Thanks! – r2evans Dec 22 '22 at 00:43
  • Hi @r2evans , thank you for reaching out. I added a screenshot with some of the data to be more clear! – Abaco Dec 22 '22 at 01:19
  • Please do not post (only) an image of code/data/errors: it breaks screen-readers and it cannot be copied or searched (ref: https://meta.stackoverflow.com/a/285557 and https://xkcd.com/2116/). Please include the code, console output, or data (e.g., `data.frame(...)` or the output from `dput(head(x))`) directly. – r2evans Dec 22 '22 at 14:16

1 Answers1

1

As some have already mentioned, it would be appreciated if you provided a reproducible example. I will still try to answer your question, based on the link you included.

You need to do some data transformation, as your data is not yet in "tidy" format. This means: You want a column for every variable, a row for every observation and a cell should contain one value. For that, you need the pivot_longer() function.

library(tidyverse)

data %>%
  pivot_longer(
    cols= austria:germania,
    names_to= "countries",
    values_to= "values") %>%
  ggplot(aes(x= central_government_dept_percent_of_gdp, 
             y=values, 
             color= countries)+
  geom_line()
Martin Gal
  • 16,640
  • 5
  • 21
  • 39
Nick Glättli
  • 421
  • 1
  • 7