ggplot 내의 geom_segment 플롯에서 색상을 조건부로 설정하려고합니다. 색상에 그라디언트를 적용 할 수 있지만 다른 셀의 주석 유형을 기반으로 선택한 색상으로 각 세그먼트를 설정하지는 않습니다.여러 geom_segment ggplot에 대한 색상을 조건부로 조정하는 방법은 무엇입니까?
데이터 :
START<-as.POSIXct(c("2017-07-13 01:40:00 MDT", "2017-07-21 06:00:00 MDT", "2017-07-21 14:00:00 MDT", "2017-07-24 11:00:00 MDT",
"2017-07-24 12:00:00 MDT", "2017-07-25 05:00:00 MDT", "2017-07-25 17:00:00 MDT", "2017-07-26 12:00:00 MDT",
"2017-07-30 12:00:00 MDT", "2017-07-31 04:00:00 MDT", "2017-07-31 15:00:00 MDT", "2017-08-03 18:30:00 MDT",
"2017-08-03 23:30:00 MDT", "2017-08-09 05:00:00 MDT", "2017-08-09 20:00:00 MDT", "2017-08-14 09:00:00 MDT",
"2017-08-16 05:00:00 MDT", "2017-08-16 07:00:00 MDT", "2017-08-16 19:00:00 MDT", "2017-08-17 18:00:00 MDT",
"2017-08-20 05:00:00 MDT", "2017-08-23 06:00:00 MDT", "2017-08-23 14:00:00 MDT", "2017-08-24 17:00:00 MDT",
"2017-08-28 00:00:00 MDT"))
END<-as.POSIXct(c("2017-07-21 06:00:00 MDT", "2017-07-21 14:00:00 MDT", "2017-07-24 11:00:00 MDT", "2017-07-24 12:00:00 MDT",
"2017-07-25 05:00:00 MDT", "2017-07-25 17:00:00 MDT", "2017-07-26 12:00:00 MDT", "2017-07-30 12:00:00 MDT",
"2017-07-31 04:00:00 MDT", "2017-07-31 15:00:00 MDT", "2017-08-03 18:30:00 MDT", "2017-08-03 23:30:00 MDT",
"2017-08-09 05:00:00 MDT", "2017-08-09 20:00:00 MDT", "2017-08-14 09:00:00 MDT", "2017-08-16 05:00:00 MDT",
"2017-08-16 07:00:00 MDT", "2017-08-16 19:00:00 MDT", "2017-08-17 18:00:00 MDT", "2017-08-20 05:00:00 MDT",
"2017-08-23 06:00:00 MDT", "2017-08-23 14:00:00 MDT", "2017-08-24 17:00:00 MDT", "2017-08-28 00:00:00 MDT",
"2017-09-28 13:00:00 MDT"))
RATE<-c(1485, 0, 1485, 880, 1485, 0, 1485, 1100, 1485, 0, 1485, 1483, 1485, 0, 1485, 1419, 880, 0, 1419, 1485, 1419, 0, 1100, 419, 1100)
Comms<-c("", "Issue-asdfa", "asfdaf", "Issue-asdfa", "asfdaf","", "Issue-asdfa", "asfdaf", "Issue-asdfa", "asfdaf", "", "Issue-asdfa", "asfdaf", "Issue-asdfa", "asfdaf","", "Issue-asdfa", "asfdaf", "Issue-asdfa", "asfdaf", "asfdaf","", "Issue-asdfa", "asfdaf", "Issue-asdfa")
DF<-data.frame(START, END, RATE, Comms)
기능이 너무 세그먼트를 설정하는 데 어떤 주석의 종류와 색상을 말할 수는 다음과 같습니다 데이터가 플롯되는
WhatisCommentType <- function(SchComment) {
CommentType = 0
if (grepl("Issue", SchComment)) {
CommentType = 2
} ## 2 is when there is maintenance
if (grepl("Issue", SchComment) == FALSE & (SchComment!="")) {
CommentType = 1
} ## 1 is when there is a comment and it's not a Issue
if (SchComment=="") {
CommentType = 0
} ## 0 is when there is no comment
return(CommentType)
}#end WhatisCommentType funciton
WhatisCommentTypecolour <- function(SchComment) {
CommentTypecolour = "dodgerblue4" #"#045a8d" #
if (grepl("Issue", SchComment)) {
CommentTypecolour = "firebrick1" #f03b20" #firebrick1"
} ## 2 is when there is maintenance
if (grepl("Issue", SchComment) == FALSE & (SchComment!="")) {
CommentTypecolour = "darkorange3" # "#d95f0e" #
} ## 1 is when there is a comment and it's not a Issue
if (SchComment=="") {
CommentTypecolour = "dodgerblue4" #"#045a8d" #"dodgerblue4"
} ## 0 is when there is no comment
return(CommentTypecolour)
}#end WhatisCommentType funciton
설정 :
#Comment colouring type
DF$CommentType<-unlist(lapply(DF$Comms, WhatisCommentType))
DF$CommentTypecolour<-unlist(lapply(DF$Comms, WhatisCommentTypecolour))
ym1 <- as.yearmon(min(DF$START)) # convert to yearmon
ym2 <- as.yearmon(max(DF$END)) # ditto
ListStartOfMonthDates <- seq(ym1, ym2, 1/12)
ListStartOfMonthDates <- as.Date(paste('01', ListStartOfMonthDates), format='%d %b %Y')
lower <- as.POSIXct(strptime(paste(ListStartOfMonthDates[1], "01:00"), "%Y-%m-%d %H:%M"))
upper <- max(DF$END)
limits = c(lower,upper)
ListStartOfMonthDates <- as.POSIXct(strptime(paste(ListStartOfMonthDates, "07:00"), "%Y-%m-%d %H:%M"))
ggplot(DF)+
geom_segment(aes(x=as.POSIXct(as.character(START)),
xend=as.POSIXct(as.character(END)),
y=RATE,
yend=RATE,
colour=CommentType), size=2)+
scale_colour_manual(name="",
# labels map onto colors and pretty labels
values=c("0"="dodgerblue4",
"1"="firebrick1",
"2"="firebrick1"),
labels=c("0"="No Comment",
"1"="copas",
"2"="Non-copas Commet"))
다음과 같은 오류가 발생합니다.
오류 : 이산 규모
나는이 작업을 얻을 수 있는지 확인하기 위해 이러한 다른 게시물과 함께 주변에 놀고 있었는데에 공급 연속 값.
matching legend items and colours in ggplot2 where some geom_segment are not included in legend
Colours in ggplot (geom_segment)
내가 제대로 생성이 플롯을 얻을 수있는 모든 아이디어를 어떻게가?