我试图在ggplot中的geom_segment图中有条件地设置颜色。我可以获得颜色的渐变,但不能根据另一个单元格中的注释类型将每个线段设置为我选择的颜色。
数据:
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)
我有什么想法可以让这个情节正确生成?
答案 0 :(得分:0)
scale_colour_manual创建具有离散值的比例,例如。 A,B和C.您收到此错误,因为变量“CommentType”实际上被视为连续变量。
我认为这样做的原因是你为它分配了数值:
(SchComment=="") {
CommentType = 0
} ## 0 is when there is no comment
如果你改为给它分配一个字符串或因子值,那么ggplot2应该认识到它是一个离散值并且工作正常。
(SchComment=="") {
CommentType = "0"
} ## 0 is when there is no comment