我有这个代码!有谁知道如何在图例上显示不同的颜色?使用此代码,在绘图上似乎出现了不同的色点 (*),但在图例中,对于我的子集的 poth,出现了两次相同的颜色红色 *。
value<-c(2,4,5,6,7,8,9,8,7,7,7,2,1,4,1,14,34,33,66,7,87,99,90,45,69,77,0,65,2)
DateTime<-c("2020-11-24 15:59:22 GMT", "2020-11-24 16:29:22 GMT", "2020-11-25 08:02:36 GMT",
"2020-11-27 08:27:58 GMT", "2020-11-27 08:57:58 GMT", "2020-11-27 09:27:58 GMT",
"2020-11-27 09:57:58 GMT" ,"2020-11-27 10:27:58 GMT", "2020-11-27 10:57:58 GMT",
"2020-11-27 11:27:58 GMT", "2020-11-27 11:57:58 GMT" ,"2020-11-27 12:27:58 GMT",
"2020-11-27 12:57:58 GMT" , "2020-11-27 13:57:58 GMT",
"2020-11-27 14:27:58 GMT" ,"2020-11-27 14:57:58 GMT" ,"2020-11-27 15:27:58 GMT",
"2020-11-27 15:57:58 GMT" ,"2020-11-27 16:27:58 GMT" ,"2020-11-27 16:57:58 GMT",
"2020-11-27 17:27:58 GMT", "2020-11-27 17:57:58 GMT", "2020-11-27 18:27:58 GMT",
"2020-12-20 06:36:38 GMT", "2020-12-20 07:06:38 GMT" ,"2020-12-20 07:36:38 GMT",
"2020-12-20 08:06:38 GMT" ,"2020-12-20 08:36:38 GMT" ,"2020-12-20 09:06:38 GMT")
DateTime<-as.POSIXct(DateTime)
Group<-c("Hungry","Hungry","Hungry","Hungry","Laughing","Laughing","Laughing",
"Laughing","Hungry","Hungry", "Angry","Angry","Angry","Laughing",
"Laughing","Laughing","Laughing", "Happy","Happy","Happy","Happy","Angry",
"Angry","Angry","Angry","Laughing","Laughing","Laughing","Laughing")
DATA<-tibble(DateTime,value,Group)
DATA$Months<-months(DATA$DateTime,abbreviate=F)
DATA$Date<-as.Date(DATA$DateTime,format="%Y-%m-%d")
DATA$WeekDays<-weekdays(DATA$DateTime)
DATA$Date<-as.Date(DATA$DateTime)
DATA<-DATA %>%
mutate(diff.time=as.numeric(c(0,diff(DATA$DateTime))/60),
diff_time_hms=as_hms(difftime(DateTime,lag(DateTime),unit = "hours")))
DATA$diff_time_hms<-replace_na(DATA$diff_time_hms,0)
DATA<-DATA%>%
group_by(Group_ID=rleid(Group,Date))%>%
mutate(Sum_Time_Group=as_hms(sum(diff_time_hms)))
Data1<-subset(DATA,Group=="Angry")
Data2<-subset(DATA,Group=="Laughing")
ggplot(DATA,mapping=aes(x=DateTime,y=Sum_Time_Group,color=Group,group = 1)) +
geom_path()+
geom_point(data = Data1,
aes(fill = "Data1"),
color='blue',
shape = "*",
size=12)+
geom_point(data = Data2,
aes(fill = "Data2"),
color='red',
shape = "*",
size=12)+
geom_point(shape=1)+
facet_wrap(~ Months,scales = "free")+
theme(legend.title=element_blank(),axis.text.x = element_text( angle = 90, vjust = 1, hjust=1))
这就是它给我的结果
如何使图例在 Data1 上显示为蓝色,在 Data2 上显示为红色?