'R ggplot with 2 y-axes at different scales

The following is df_sales

df_sales <- structure(list(Year_of_Release = c(2000, 2001, 2002, 2003, 2004,  2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,  2016), Sales_Sum = c(201.58, 331.47, 395.51, 357.8, 419.05, 458.31,  518.22, 605.37, 671.79, 658.88, 590.59, 507.79, 355.84, 361.24,  331.51, 268.05, 130.1), Sales_Avg = c(0.576, 0.688, 0.477, 0.462,  0.55, 0.488, 0.515, 0.506, 0.471, 0.462, 0.471, 0.447, 0.545,  0.664, 0.571, 0.442, 0.259)), class = c("tbl_df", "tbl", "data.frame" ), row.names = c(NA, -17L))

The following are the codes for plotting

> ggplot(data = df_sales, aes(x = Year_of_Release)) +   geom_line(aes(y
> = Sales_Avg, colour = "Sales (Avg)")) +   geom_line(aes(y = Sales_Sum, colour = "Sales (Sum)")) +   scale_y_continuous(name = 'Sales (Avg)',
>                      sec.axis = sec_axis(trans = ~ ., name = "Sales (Sum)")) +   labs(y = "Sales (Sum)",
>        x = "Year of release",
>        colour = "")

However, the resulting plot looks weird. One geom_line was not added properly.

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source