【问题标题】:Ggplot problem printing class POSIXct and coloring by group RGgplot问题打印类POSIXct和R组着色
【发布时间】:2021-03-11 08:10:16
【问题描述】:
value<-c(2,4,5,6,7,8,9,8,7,7,7,2,1,4,1,14,44,34,33,66,7,87,99,90,45,69,77,0,65,2)
LSL<-80
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:27: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","Angry","Laughing","Laughing","Laughing","Laughing","Happy","Happy","Happy","Happy","Angry","Angry","Angry","Angry","Laughing","Laughing","Laughing","Laughing")
DATA<-data.frame(cbind(DateTime,value,Group))
DATA$Months<-months(DateTime,abbreviate=F)

idx <- c(0, diff(as.numeric(DATA$DateTime)))/60
i2 <- c(1,which(idx >30), nrow(DATA)+1)
DATA$grp <- rep(1:length(diff(i2)), diff(i2))

jpeg("TEST.jpg", width=8000, height=4000, res=300)
ggplot(DATA,aes(x=DateTime,y=value))  +
geom_line(aes(group=grp))+
geom_point(aes(colour=Value<LSL))+
scale_colour_manual(name="value<LSL",values=setNames(c('red','black'),c(T,F)))+ 
geom_hline(aes(yintercept=LSL,linetype="Lower Specification Limit"), color = "red")+
scale_linetype_manual(name = "Specification Limits",values = c("solid"),guide=guide_legend(list(color 
= c("Specification Limits"))))+
labs(x = "Dates", 
   y = "value",
   title = "Value Out Of Specifications ") +  
scale_x_datetime(date_labels = "%a-%d", date_breaks  ="1 day",
               sec.axis = dup_axis(name="Hours",labels = scales::time_format("%H:%M"),breaks 
=scales::date_breaks("5 hours")))+

facet_wrap(~ Months,scales = "free")+

theme(axis.text.x = element_text( angle = 90, vjust = 1, hjust=1))

dev.off()

这是我未运行的代码,我试图运行它,但总是给我这个错误

Error: Invalid input: time_trans works with objects of class POSIXct only

嗯,我试图解决这个问题,但我做不到。这是运行它的任何方式吗?如果可能的话,我还希望将要打印的行由 Group 变量着色(一行多种颜色)提前谢谢你。

【问题讨论】:

    标签: r ggplot2 rstudio posixct


    【解决方案1】:

    如果您将其替换为DATA &lt;- tibble(DateTime, value, Group)DateTime 的数据类型会在 DATA&lt;-data.frame(cbind(DateTime,value,Group)) 中丢失。

    【讨论】:

    • 由于您已经为 geom_point 使用颜色,您可以使用不同的点形状并为 geom_point 填充美学,然后为 geom_line 着色 - 例如:ggplot(DATA,aes(x=DateTime,y=value)) + geom_line(aes(group=grp, colour = Group))+ geom_point(shape = 21, aes(fill=value&lt;LSL))+ scale_fill_manual(name="value&lt;LSL",values=setNames(c('red','black'),c(T,F)))+ ...
    • 奇怪,不应该有任何额外的点。形状 21 应该可以工作here's a site with few examples。可能有两个原因:要么点太小而看不到填充颜色 - 您可以尝试“size = 4”,或者其他问题可能是输出设备(至少在某些情况下我注意到在标准 R 图形窗口中显示的内容与写入文件时略有不同)。
    猜你喜欢
    • 2015-07-10
    • 2015-12-22
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    相关资源
    最近更新 更多