0

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

sa90210
  • 525
  • 2
  • 12
  • https://www.r-graph-gallery.com/line-chart-dual-Y-axis-ggplot2.html – Adam Quek Dec 02 '20 at 09:12
  • Does this answer your question? [Combining Bar and Line chart (double axis) in ggplot2](https://stackoverflow.com/questions/41764312/combining-bar-and-line-chart-double-axis-in-ggplot2) – tjebo Dec 02 '20 at 09:51

2 Answers2

1
#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

1

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

Edo
  • 7,567
  • 2
  • 9
  • 19