-1

I am trying to plot values on the y axis against years on the x axis with ggplot2.

This is the dataset: https://drive.google.com/file/d/1nJYtXPrxD0xvq6rBz2NXlm4Epi52rceM/view?usp=sharing

I want to plot the values of specific countries.

It won't work by just specifying year as the x axis and a country's values on the y axis. I'm reading I need to melt the data frame, so I did that, but it's now in a format that doesn't seem convenient to get the job done.

I'm assuming I haven't correctly melted, but I have a hard time finding what I need to specifically do.

What I did beforehand is manually transpose the data and make the years a column, as well as all the countries.

This is the dataset transposed:

https://drive.google.com/file/d/131wNlubMqVEG9tID7qp-Wr8TLli9KO2Q/view?usp=sharing

Here's how I melted:

inv_melt.data <- melt(investments_t.data, id.vars="Year")

ggplot() +
  geom_line(aes(x=Year, y=value), data = inv_melt.data) 

The plot shows the aggregated values of all countries per year, but I want them per country in such a manner that I can also select to plot certain countries only.

How do I utilize melt in such a manner? Could someone walk me through this?

  • What kind of plot do you want? See for instance [a recent answer](https://stackoverflow.com/questions/56824221/how-to-plot-different-y-with-the-same-x-in-ggplot/56824979#56824979) of mine. There are many, many similar others in SO. – Rui Barradas Jul 01 '19 at 11:54
  • @RuiBarradas I think I came across this answer of yours as well. I tried to get the data in long form, according to exactly what you suggest. I would like a line plot with years on the x axis and values per country on the y axis, so I can add several lines of specific countries. –  Jul 01 '19 at 11:58
  • In that answer, use `geom_line`, not `geom_point` and remove both `geom_smooth` and `facet_grid`. It becomes just 2 code lines after `melt`. – Rui Barradas Jul 01 '19 at 11:59
  • @RuiBarradas I edited the question and added how I tried to plot, and the data after manually transposing. I hope it helps. –  Jul 01 '19 at 12:02
  • 1
    For line plots, how can both the `y` and `x` axes be catorgical (i.e., years, country). How does the numeric metric of interest display? – Parfait Jul 01 '19 at 12:22
  • @Parfait Values –  Jul 01 '19 at 17:59
  • Sorry guys, I shared the same data set twice, just noticed. The 2nd link is edited. –  Jul 01 '19 at 18:02

1 Answers1

0

There are no columns named "Year" in the linked to data set, there are columns per year. So it need to be melted by "country" and then the "variable" edited with sub.

inv_melt.data <- reshape2::melt(investments_t.data, id.vars="country")
inv_melt.data$variable <- as.integer(sub("^X", "", inv_melt.data$variable))

ggplot(inv_melt.data, aes(variable, value, color = country)) +
  geom_line(show.legend = FALSE)

Edit.

The following code keeps only some countries, filtering out the ones with more missing values.

i <- sapply(investments_t.data[-1], function(x) sum(is.na(x)) == 0)
i <- c(1, which(i))
inv_melt.data <- reshape2::melt(investments_t.data[i], id.vars = "Year")

ggplot(inv_melt.data, aes(Year, value, color = variable)) +
  geom_line(show.legend = FALSE)
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • I'm sorry, I shared the same link twice. The transposed dataset which shows a column for years has a link now (2nd one). –  Jul 01 '19 at 18:01
  • Thank you for the help so far. I want to be able to plot selected countries, not all. I wasn't entirely clear on that. –  Jul 01 '19 at 18:09
  • @RoddyMacintyre See the edit.It has code implementing a possible selection criterion. – Rui Barradas Jul 01 '19 at 19:03
  • The edit isn't working as you described. I could subset before melting, maybe I'll stick with that. I do prefer not having to subset or anything of that kind, but rather have the data in a way to be able to select the countries entirely in ggplot. –  Jul 01 '19 at 20:21