0

dataset screenshot

Hello,

I have a dateset structured as shown in the link above. I am extremely new to R. And this is probably super easy to get done. But I cannot figure out how to plot this dataset using ggplot...

Could anyone guide and give me hints?

I basically want to color lines according to socioeconomic levels and visualize it by each years' value...

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    Please don't provide an image of data or code, I think you'll find most people will ignore or downvote your implied request for us to transcribe data from your data screenshot. Please just provide the output from `dput(head(x))` or similar unambiguous data. Additionally, please show the code you've attempted so far. Thanks! – r2evans Apr 05 '21 at 18:56
  • Generic comments: `ggplot2` prefers "long" data for several reasons, look in questions about pivoting/reshaping from wide to long. (It can make sense in a sense, it is rarely a good idea to have some form of numeric data encoded within a column name; in this case, `2016` is a number -- the year -- yet is a column. There are times when this is fine, but `ggplot2` and the concept of "tidy data" strongly encourage reshaping this data.) Good luck! – r2evans Apr 05 '21 at 19:01
  • Just to motivate why not to post images of code or data: https://meta.stackoverflow.com/a/285557/11374827. For tips on making good questions for R: https://stackoverflow.com/a/5963610/11374827. – teunbrand Apr 05 '21 at 19:04

1 Answers1

0

You need to reshape you data to run ggplot.

library(reshape)
library(dplyr)
library(ggplot2)

df_long <- melt(df) # reshape the dataframe to a long format
    
df_long %>%
      ggplot( aes(x=variable, y=value, group=group, color=group)) +
      geom_line()

Note: You will get better answers if you post your code with a reproducible dataset.

apds1088
  • 60
  • 4
  • It worked!! Thank you very much! Literally created this account an hour ago, hoping to get familiar with this website soon! and probably in a few years I'll look back and laugh at this post. – Uğurcan Koç Apr 05 '21 at 19:34