I am new to r, so please excuse me. I want to draw a ggplot with kvartal as x-asis, and arbeidsledige and oljepris as two different y-axis. I also dont know how to fix the name on the graph, and change the x asis name from "kvartal" to Year, and the y axises name from "arbeidsledige" to "Antall arbeisledige", and the other y axis name from "oljepris" to "Oljepris i dollar". Please help
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.
I would appreciate for the help, thanks in advance
There are two key steps to arranging the data so that the plot is easily made. You should set the column containing dates to have date values instead of character strings and you should reshape the data so that a single column contains the region labels and a single column contains the values to be plotted. I do this in the code below, using the name of US states rather than your (Norwegian?) regions. Notice that the states labeled as New_York and New_Mexico in the data are listed in the legend without the underscore because I manually entered the legend text.
DF <- data.frame(kvartal = c("2011-03-01","2011-06-01","2011-09-01","2011-12-01",
"2012-03-01","2012-06-01","2012-09-01","2012-12-01"),
New_York = 1:8 + rnorm(8,0,1),
Florida = 2:9 + rnorm(8, 0, 1.5),
Texas = 1.5:8.5,
New_Mexico = 0:7 + rnorm(8, 0, 0.5))
DF
#> kvartal New_York Florida Texas New_Mexico
#> 1 2011-03-01 0.04125076 0.2634613 1.5 -0.2976835
#> 2 2011-06-01 3.96233379 3.1024347 2.5 0.6971117
#> 3 2011-09-01 5.07298595 3.5399068 3.5 1.2338146
#> 4 2011-12-01 4.59675228 3.2087747 4.5 3.0602612
#> 5 2012-03-01 5.42429997 6.8475503 5.5 4.2669195
#> 6 2012-06-01 6.28974342 6.5363706 6.5 5.5786714
#> 7 2012-09-01 7.83045386 7.4183318 7.5 5.6547064
#> 8 2012-12-01 8.88240002 7.9082453 8.5 7.0834392
library(tidyr)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
library(ggplot2)
#Make the kvartal column a date value
DF$kvartal <- ymd(DF$kvartal)
#pivot the data to have a column labeled Fylke with the state names
#and a single value column
DF_long <- DF %>% pivot_longer(New_York:New_Mexico, names_to = "Fylke", values_to = "Value")
head(DF_long)
#> # A tibble: 6 x 3
#> kvartal Fylke Value
#> <date> <chr> <dbl>
#> 1 2011-03-01 New_York 0.0413
#> 2 2011-03-01 Florida 0.263
#> 3 2011-03-01 Texas 1.5
#> 4 2011-03-01 New_Mexico -0.298
#> 5 2011-06-01 New_York 3.96
#> 6 2011-06-01 Florida 3.10
ggplot(DF_long, aes(x = kvartal, y = Value, color = Fylke, group = Fylke)) + geom_line() +
labs(x = "Year", y = "Antall arbeidsledige") +
scale_color_hue(labels = c("Florida", "New York", "New Mexico", "Texas"))
Your values of Oljepris are about 100 and your values of Kronekurs are about 5. If you multiply Oljepris by 600 as you did here
geom_line(aes(kvartal, Oljepris * 600, color = "Oljepris"))
then they become about 60000 as seen in your plot and on that scale the values of Kronekurs are too small to see. You should multiply Oljepris by a number that brings it close to 5, maybe 0.05, and then in scale_y_continuous divide by the same number.