I need to create plots that have 2 y axes, one that shows dollars (whose scale is in millions) and another metric (whose scale is much smaller (spanning from 10s to 100s). I have the below loop using sec_axis because i have many variables that I need to plot, but the secondary axis on the right hand side of the plot that is supposed to represent the metric is using the same scale as the dollar axis. Why is this ?
for (metric in metrics) {
for (brand_name in unique(entire_brand_month_data$Brand)){
brand_data <- entire_brand_month_data[entire_brand_month_data$Brand == brand_name,]
ggplot(brand_data, aes(x=Date)) +
geom_line(aes(y=total_dollars, color = "Dollar Sales")) +
geom_line(aes(y=get(metric), color = metric)) +
scale_y_continuous(
sec.axis = sec_axis(trans = ~., name = metric)
) +
labs(title = paste("Brand", brand_name, "Sales and", metrics),
x="Date",
y="Dollar Sales") +
theme_minimal() + guides(guide_legend(title = "Legend"))->p
print(p)
}
}