0

I have this code! Does anyone know how to make on legend to appear different colouring? Using this code, on the plot it seems to appear different color points (*) but in the legend appears twice the same color red * for poth of my subsets.

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))

This is what it gives me as result

enter image description here

How to make on legend appear blue on Data1 and red on Data2?

1 Answers1

1

This might take you closer to what you are looking for, although I couldn't maintain the shape in the legend.

library(ggplot2)

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)) + 
  scale_fill_manual(values = c('red', 'blue')) + 
  guides(fill = guide_legend(override.aes = list(shape = 21)))

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • What if the one of the subsets is zero? This happened to me on another dataset and gives me this error Error: Aesthetics must be either length 1 or the same as the data (1): x and y – Maria Provelegiou Apr 02 '21 at 09:08
  • Yes, if the subset has 0 rows then it will give you an error. You can write a conditional statement for it removing that `geom_line` code from it. Something like `if(nrow(data1) > 0 & nrow(data2) > 0) {ggplot(..) + geom_path(...) + geom_point(...) + ...} else ....` – Ronak Shah Apr 02 '21 at 09:19
  • Ok I solved this but now I have a different problem at my data on facet_wrap(~ Months,scales = "free"), scales="free" does not work. I do not know why. It gives on the plot for one month all x Dates (I want the Dates of each month) plotting the values for one month giving me the red and blue * of all the dataset. Do you may know why? – Maria Provelegiou Apr 02 '21 at 10:13
  • I am not sure why that would happen. Can you ask that as a new question so that it would be easier to help? – Ronak Shah Apr 02 '21 at 10:28