help with ggplot two different y-axis

My data looks like this:

alusd
A tibble: 36 x 3
kvartal arbeidsledige oljepris
date dbl dbl
1 2011-03-01 74190 115.
2 2011-06-01 65487 114.
3 2011-09-01 65254 113.
4 2011-12-01 63655 108.
5 2012-03-01 67669 125.
6 2012-06-01 64196 95.2
7 2012-09-01 63060 113.
8 2012-12-01 62569 108.
9 2013-03-01 69969 108.
10 2013-06-01 66596 103.
… with 26 more rows

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

1 Like

ggplot(alusd, aes(x = kvartal)) +
geom_line(aes(y = arbeidsledige, colour = "Antall arbeidsledige")) +
geom_line(aes(y = oljepris, colour = "Oljepris i dollar")) +
scale_y_continuous(sec.axis = sec_axis(~./scl, name = "Oljepris i Dollar"))+
ggtitle("Sammenhengen mellom oljepris og antall arbeidsledige") +
theme(legend.position = "bottom",
legend.margin=margin(-5,0,0,0),
plot.title = element_text(hjust = 0.5),
axis.text.y.right=element_text(colour=cols[1]),
axis.ticks.y.right=element_line(colour=cols[1]),
axis.title.y.right=element_text(colour=cols[1]),
axis.text.y=element_text(colour=cols[2]),
axis.ticks.y=element_line(colour=cols[2]),
axis.title.y=element_text(colour=cols[2])) +
scale_colour_manual(values=cols) +
labs(colour="")

that is my code, and i get this

Is this close?

DF <- read.csv("c:/users/fjcc/Documents/R/Play/Dummy.csv", sep = " ", 
               stringsAsFactors = FALSE)
library(dplyr)
library(lubridate)
library(ggplot2)
DF <- DF %>% mutate(kvartal = ymd(kvartal))
ggplot(DF) + geom_line(aes(kvartal, arbeidsledige)) + 
  geom_line(aes(kvartal, oljepris * 600), color = "red") + 
  scale_y_continuous(sec.axis = sec_axis(
    trans = ~ . /600,
    name = "oljepris i dollar",
    breaks = waiver(),
    labels = waiver(),
    guide = waiver()
  )) +
  labs(x = "Year", y = "Antall arbeisledige") +
  theme(axis.line.y.right = element_line(color = "red"),
        axis.text.y.right = element_text(color = "red"))

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

1 Like

thank you so much, it looks right yes, but what about the color on oljepris i dollar on the secondary y-axis, how do I get it to red?

would also be nice to have it like the first picture, where it says what color is what

colors = c("oljepris" = "red", "arbeidsledige" = "blue" )
DF <- DF %>% mutate(kvartal = ymd(kvartal))
ggplot(DF) + geom_line(aes(kvartal, arbeidsledige, color = "arbeidsledige")) + 
  geom_line(aes(kvartal, oljepris * 600, color = "oljepris")) + 
  scale_y_continuous(sec.axis = sec_axis(
    trans = ~ . /600,
    name = "oljepris i dollar",
    breaks = waiver(),
    labels = waiver(),
    guide = waiver()
  )) +
  labs(x = "Year", y = "Antall arbeisledige") +
  scale_color_manual(values = colors) + 
  theme(axis.line.y.right = element_line(color = "red"),
        axis.text.y.right = element_text(color = "red"),
        axis.title.y.right = element_text(color = "red"))

thank you so much for the help. i was wondering one more question:
I have this dataset:

>dk (the name of the dataset)

A tibble: 36 x 5
kvartal rogaland hordaland vestagder more

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 for the help, thanks in advance :smiley:

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"))

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

thank you very much sir. i learnt a lot from you, more than from anybody else.

hello, i have the same issue.
i used your #2 post with this dataset:

# A tibble: 36 x 3
   kvartal    Kronekurs Oljepris
   <date>         <dbl>    <dbl>
 1 2011-03-01      5.59    115. 
 2 2011-06-01      5.44    114. 
 3 2011-09-01      5.61    113. 
 4 2011-12-01      5.88    108. 
 5 2012-03-01      5.71    125. 
 6 2012-06-01      6.02     95.2
 7 2012-09-01      5.75    113. 
 8 2012-12-01      5.6     108. 
 9 2013-03-01      5.77    108. 
10 2013-06-01      5.87    103. 
# … with 26 more rows

I used:

ggplot(dollabilla) + geom_line(aes(kvartal, Kronekurs, color = "Kronekurs")) + 
      geom_line(aes(kvartal, Oljepris * 600, color = "Oljepris")) + 
      scale_y_continuous(sec.axis = sec_axis(
      trans = ~ . /600,
      name = "Oljepris i dollar",
      breaks = waiver(),
      labels = waiver(),
      guide = waiver()
      )) +
      labs(x = "År", y = "Kronekurs") +
      scale_color_manual(values = colors) + 
     theme(axis.line.y.right = element_line(color = "red"),
      axis.text.y.right = element_text(color = "red"),
      axis.title.y.right = element_text(color = "red"))

and I got this plot:

What did I do wrong?

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.