简体   繁体   中英

Add legend to scatterplots from different datasets ggplot2 in R

I have two datasets that I want to plot in the same plot and add a legend to using ggplot2 in R. My datasets look like that :

df1 = data.frame(
  DateTime = c("20/06/2016  09:11:01", "20/06/2016  10:11:01","20/06/2016  11:11:01","20/06/2016  12:11:01", "20/06/2016  13:11:01", "20/06/2016  14:11:01","20/06/2016  15:11:01", "20/06/2016  16:11:01", "20/06/2016  17:11:01","20/06/2016  18:11:01","20/06/2016  19:11:01", "20/06/2016  20:11:01")), 
  price1 = c(0.072, 0.072,  0.072,  0.074,  0.074,  0.072,  0.072,  0.072,  0.074,  0.075,  0.074, 0.074)
      )

df2 = data.frame(
DateTime = c("25/05/2016  00:05:00","31/05/2016  00:05:00","07/06/2016  00:05:00","14/06/2016  00:05:00","21/06/2016  00:05:00","29/06/2016  00:05:00","06/07/2016  00:05:00","14/07/2016  00:05:00","21/07/2016  00:05:00","26/07/2016  00:05:00","02/08/2016  00:05:00","09/08/2016  00:05:00"),
    price2 = c(0.54,    0.5,    0.49,   0.83,   0.61,   0.54,   0.38,   0.4,    0.29,   0.27,   0.6,    0.51)
                )

df1$DateTime1 = strptime(as.character(df1$DateTime), "%d/%m/%Y %H:%M:%S")
df2$DateTime2 = strptime(as.character(df2$DateTime), "%d/%m/%Y %H:%M:%S")

The code I am using to produce the plot and the legend is the following

ggplot() + 
  geom_point(data = df1, aes(x = DateTime1, y = price1),colour="red", size = 2) +   
  geom_point(data = df2, aes(x = DateTime2, y = price2),colour="blue", size = 1) +
  xlab('Date') + ylab('Price') + ggtitle("Plot of prices") + 
  theme(plot.title = element_text(lineheight=.8, face="bold")) + 
  scale_colour_manual("", breaks = c("Price1", "Price2"),
                      values = c("Price1"="red", "Price2"="blue"))

The output is below

两个数据集中不同日期的价格图

Although I get the plot I want, the legend doesn't seem to appear in the graph and I get no error. Any help would be much appreciated.

In ggplot to get a proper legend you need to specify the corresponding aesthetics.

Here's the solution with explicit colour names for colour manual:

ggplot() + 
  geom_point(data = df1, aes(x = DateTime1, y = price1, color = 'df1'), size = 2) +   
  geom_point(data = df2, aes(x = DateTime2, y = price2, color = 'df2'), size = 1) +
  xlab('Date') + ylab('Price') + ggtitle("Plot of prices") + 
  theme(plot.title = element_text(lineheight=.8, face="bold")) + 
  scale_colour_manual("", values = c("df1"="red", "df2"="blue"))

This should work, by simply moving the colours in aes , to include them in the colour legend:

ggplot() + 
  geom_point(data = df1, aes(x = DateTime1, y = price1, colour="red"), size = 2) +   
  geom_point(data = df2, aes(x = DateTime2, y = price2, colour="blue"), size = 1) +
  xlab('Date') + ylab('Price') + ggtitle("Plot of prices") + 
  theme(plot.title = element_text(lineheight=.8, face="bold")) +
  scale_colour_manual(values = c("blue", "red"))

在此处输入图片说明

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