ggplot2的时间序列和图例

时间:2016-08-15 20:44:14

标签: r ggplot2 time-series

这是我的数据:

x <- c("22-01-16","26-01-16","28-01-16","01-02-16","05-02-16","16-02-16","17-03-16","18-03-16","04-04-16","05-04-16","06-04-16","08-04-16")
y <- c(97.14,75,54.44,70.45,110.56,66.3,178.76,171.90,419.41,424,518.63,242.17)
z <- c("ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP","ADCP")

所以我制作数据框

Datos <- data.frame(x)
Datos$Caudal <- y
Datos$Tipo <- z
Datos$Fecha <- as.Date(Datos$x, "%d-%m-%y")

使用ggplot2

绘图
Serie_Caudal <-
ggplot(Datos, aes(Fecha, Caudal)) +
geom_line(size=1, colour="red") +
geom_point(shape=23,size=1, colour="blue",fill = "blue") +
scale_x_date(date_breaks = "1 week",labels = date_format("%d/%b"))+
xlab("Fecha") + ylab(bquote('Caudal ('*m^3~s^-1*')')) +
ggtitle("Caudales Diarios (01-06/2016)")
Serie_Caudal 

我尝试绘制一个传奇,但我不能这样,我尝试使用Melt但我的数据以一种我无法绘制的方式改变。同时尝试scale_fill_manual,但传说中没有显示。我想知道是否有办法手工制作一个传奇。

图例必须显示蓝点和ADCP

1 个答案:

答案 0 :(得分:2)

这只显示一个蓝点。

ggplot(aes(Fecha, Caudal, colour = "ADCP"), data = Datos) +  
  geom_point() + 
  geom_point(shape=23,size=1,color="blue",fill = "blue") +
  scale_color_manual(values = c("ADCP"="blue"),name = "") +
  geom_line(color="red", size=1) +
  scale_x_date(date_breaks = "1 week",labels = date_format("%d/%b")) +
  xlab("Fecha") + ylab(bquote('Caudal ('*m^3~s^-1*')')) +
  ggtitle("Caudales Diarios (01-06/2016)") 

enter image description here