简体   繁体   中英

R: Displaying a Legend using ggplot2 with multiple dataframes

I am using ggplot to plot multiple dataframes in one plot.

ggplot(data2_CY, aes(x = Date, y = AVERAGETOTALCOST)) + geom_point(color = 'blue') + geom_point(data = data2_CN, color = 'red') + 
  geom_point(data = data2_LY, color = 'cyan') + geom_point(data = data2_LN, color = 'magenta') + 
  labs(title = "Trends Over Time", x = "Time", y = "Average Total Cost") + scale_x_date(breaks = seq(as.Date("2015-01-01"), as.Date("2019-07-01"), by = "6 months"), date_labels = "%b\n%Y") + 
  scale_color_manual(values = c('C&Y' = 'blue', 'C&N' = 'red', 'L&Y' = 'cyan', 'L&N' = 'magenta'))

The code above returns a singular plot with multiple points in the correct colors. However, the scale_color_manual() portion of the code does not return a legend. I am trying to display the legend to the right of the graph.

Thanks for the help!

The scale_color_manual() must connect to a specific data. My advice to you is to merge all these data and plot them using one dataframe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM