Change the colors of dots and lines in ggplot R

Code example:

library(ggplot)
par(mfrow=c(1,1))
dates15=seq(as.POSIXct("2015-01-01 00:00:00"), as.POSIXct("2015-12-31 23:45:00"), by="15 min", tz="GMT")
ex1=rnorm(35040, 150, 2)
point=rep("Control", 35040)
red=c(1000:2000, 4000:5000, 10000:10500)
point[red]="Not Controlled"
gr.cols=c("black", "red")
DF=data.frame(Date=dates15,Data=ex1, Type=point)
ggplot(DF, aes(Date, Data,colour=Type))+geom_point()+geom_line()+scale_color_manual(values=gr.cols)

How to create a line graph with colored dots according to my vector red, without a red line going from the last red dot to the next? In my code example, there is a red line from point 2000 to 4000, but I do not want this. I need a line between successive points that is colored red only if the next point is also red.

I can change the order geom_lineand geom_point, but the line still exists, it is just hidden under the black dots and does not solve the problem.

+4
source share
1 answer

, ggplot "" ( ), . : . rleid - package.table.

library(data.table)
DF$group_ID <- rleid(DF$Type)

ggplot:

ggplot(DF, aes(Date, Data,colour=Type, group=group_ID))+
  geom_point()+
  geom_line()+
  scale_color_manual(values=gr.cols)

enter image description here

, , group=1 aes.

. .

+7

Source: https://habr.com/ru/post/1621272/


All Articles