how do i plot many variables in one plot?

Hey, I am trying to learn R, but I have this dataset:

> # >dk (the name of the dataset)
> A tibble: 36 x 5
   kvartal    rogaland hordaland vestagder  more
   <date>        <dbl>     <dbl>     <dbl> <dbl>
 1 2011-03-01     5159      6511      2572  3284
 2 2011-06-01     4471      6094      2177  2733
 3 2011-09-01     4612      5767      2266  2708
 4 2011-12-01     4380      5674      2371  2702
 5 2012-03-01     4558      5603      2563  2685
 6 2012-06-01     4318      5507      2308  2454
 7 2012-09-01     4137      5070      2506  2415
 8 2012-12-01     4060      4967      2526  2544
 9 2013-03-01     4380      5545      2724  2851
10 2013-06-01     4301      5516      2361  2685
#… with 26 more rows
> 

The kvartal is going to be my x-variable, with the name "Year", and many variables in the plot, rogaland to Rogaland, hordaland to Hordaland, vestagder to Vest-Agder and more to Møre og Romsdal. The y-axis name is going to be named "Antall arbeidsledige".

How do I get this done? I have tried this so many times, and I am really stuck.

It is going to look like this, but not with the same words of course. The countries are going to be replaced with Møre og Romsdal, Hordaland, Vest-Agder and Rogaland. Where it says country, it is going to say Fylke.
image

I would appreciate if someone helps me, thanks in advance :smiley:

A good way to set-up a question like this is with a reproducible example (or reprex, FAQ: What's a reproducible example (`reprex`) and how do I create one?), since that'll make it much easier for folks to pick up your issues and run with it.

In the tidyverse, you might use tidyr to reshape your data, and ggplot2 to create your plot.

You have a lot of label modification options with the lhttps://ggplot2.tidyverse.org/reference/labs.html

library(tidyr)
library(ggplot2)
dt <- structure(list(
  kvartal = structure(c(15034, 15126, 15218, 15309, 15400, 15492, 15584, 15675, 15765, 15857), class = "Date"), 
  rogaland = c(5159, 4471, 4612, 4380, 4558, 4318, 4137, 4060, 4380, 4301), 
  hordaland = c(6511,6094, 5767, 5674, 5603, 5507, 5070, 4967, 5545, 5516), 
  vestagder = c(2572,2177, 2266, 2371, 2563, 2308, 2506, 2526, 2724, 2361), 
  more = c(3284, 2733, 2708, 2702, 2685, 2454, 2415, 2544, 2851, 2685)),
  class = c("spec_tbl_df", 
            "tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), 
  spec = structure(list(
    cols = list(kvartal = structure(
      list(format = "%Y-%m-%d"), 
      class = c("collector_date", "collector")), 
      rogaland = structure(list(), class = c("collector_double", "collector")), 
      hordaland = structure(list(), class = c("collector_double","collector")),
      vestagder = structure(list(), class = c("collector_double","collector")), 
      more = structure(list(), class = c("collector_double", "collector"))), 
    default = structure(list(), class = c("collector_guess", "collector")), 
    skip = 1), class = "col_spec"))

dt %>% 
  pivot_longer(rogaland:more) %>% 
  ggplot(
    aes(x = kvartal, y = value, color = name)
  ) +
  geom_line()
#> Warning: Can't combine <spec_tbl_df> and <tibble>; falling back to <tibble>.
#> ℹ Convert all inputs to the same class to avoid this warning.
#> ℹ See <https://vctrs.r-lib.org/reference/faq-warning-convert-inputs.html>.

Created on 2020-05-07 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.