sa90210
sa90210

Reputation: 585

ggplot with a secondary y axis

I have the following dataset in tidy format. I would like to create a ggplot the Reserves (NAD) variable as a geom_bar (with the scale on the left axis) and the Reserves (USD) variable as a geom_line (with the scale on the right axis). What's the easiest way to plot both on the same chart?

structure(list(Date = structure(c(18170, 18201, 18231, 18262, 
18293, 18322, 18353, 18383, 18414, 18444, 18475, 18506, 18536, 
18170, 18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414, 
18444, 18475, 18506, 18536), class = "Date"), Key = c("Reserves (NAD)", 
"Reserves (NAD)", "Reserves (NAD)", "Reserves (NAD)", "Reserves (NAD)", 
"Reserves (NAD)", "Reserves (NAD)", "Reserves (NAD)", "Reserves (NAD)", 
"Reserves (NAD)", "Reserves (NAD)", "Reserves (NAD)", "Reserves (NAD)", 
"Reserves (USD)", "Reserves (USD)", "Reserves (USD)", "Reserves (USD)", 
"Reserves (USD)", "Reserves (USD)", "Reserves (USD)", "Reserves (USD)", 
"Reserves (USD)", "Reserves (USD)", "Reserves (USD)", "Reserves (USD)", 
"Reserves (USD)"), Value = c(32469.69683154, 29752.37718804, 
28940.88482301, 30961.07351507, 32168.72169411, 32973.94333811, 
35659.4389906, 32944.455576, 31758.97192528, 35399.5709836, 33387.0647566, 
32665.79275848, 34353.83925875, 2150.23984845138, 2030.39391190091, 
2067.39803146078, 2062.61398712044, 2052.95138288458, 1846.74175243683, 
1924.51004045528, 1877.39090357876, 1833.55302380232, 2081.25035179437, 
1971.35496109494, 1952.07290341642, 2114.88932754343)), row.names = c(NA, 
-26L), class = c("tbl_df", "tbl", "data.frame"))

Upvotes: 0

Views: 349

Answers (2)

pasuvula sai kiran
pasuvula sai kiran

Reputation: 9

#assign variable table to your tibble

library(ggplot2)

View(table)

typeof(table)

plot = ggplot(data = table, aes(x= Date,y= Value, color=Key)) + geom_bar(stat='identity')
plot + geom_line()

enter image description here

Upvotes: 1

Edo
Edo

Reputation: 7858

That's how you can plot a bar chart and a line chart together, while making them comparable.

Basically, the idea is to change mean and sd of the vector that draws the line based on mean and sd of the vector that draws the bars. The opposite transformation has to be applied on the second y axis so to make it comparable to line you draw.

library(ggplot2)
library(tidyr)

# user defined function:
# apply mean and sd of the second vector to the first one
renormalize <- function(from, to){

 from <- scale(from) 
 to <- scale(to) 
 from * attr(to, 'scaled:scale') + attr(to, 'scaled:center')
 
}


# reshape 
df <- df %>% 
 pivot_wider(names_from = Key, values_from = Value)

ggplot(df, aes(x = Date)) +
 geom_col(aes(y = `Reserves (NAD)`), fill = "steelblue") +
 geom_line(aes(y = renormalize(`Reserves (USD)`, `Reserves (NAD)`)), colour = "coral", size = 2) +
 scale_y_continuous(sec.axis = sec_axis(~renormalize(., df$`Reserves (USD)`), name = "Reserves (USD)")) +
 theme_light()

enter image description here

Upvotes: 1

Related Questions