The data I have contain four columns: x
, y_cnt
, y1_rate
, y2_rate
.
set.seed(123)
x <- seq(1,10)
y_cnt <- rnorm(10, 200, 50)
y1_rate <- runif(10,0,1)
y2_rate <- runif(10,0,1)
df <- data.frame(x, y_cnt, y1_rate, y2_rate)
I need to produce a plot such that x
is on the x-axis, both y1_rate
and y2_rate
are on the main y-axis, and y_cnt
on the secondary y-axis.
Here how it looks in Excel:
Update:
This is what I've so far. It seems that the figure below shows y1_rate
only.
transf_fact <- max(df$y_cnt)/max(df$y1_rate)
# Plot
ggplot(data = df,
mapping = aes(x = as.factor(x),
y = y_cnt)) +
geom_col(fill = 'red') +
geom_line(aes(y = transf_fact * y1_rate), group = 1) +
geom_line(aes(y = transf_fact * y2_rate)) +
scale_y_continuous(sec.axis = sec_axis(trans = ~ . / transf_fact,
name = "Rate"))+
labs(x = "X")