是否可以按给定排名绘制以下数据并在geom_plot
中指定此排名?或者我应该先订购这个因素。我的意思是,我想按以下顺序绘制ID,A,C,其中B为深蓝色,C为浅蓝色。
tim.seq <- seq(as.POSIXlt("2013-07-01 00:00:00", origin="1960-01-01", tz="GMT"),
as.POSIXlt("2013-07-8 00:00:00", origin="1960-01-01", tz="GMT"),
by="1 hour")
id <- factor(sample(LETTERS[1:3], length(tim.seq), T, prob=c(0.4,0.8,0.2)))
rank <- factor(id, levels=levels(id), label=c(2,3,1))
data <- data.frame(time=tim.seq, id=id, rank=rank)
p1 <- ggplot(data, aes(x=time, y=..count.., alpha=id))
p1 <- p1 + geom_bar(fill="darkblue", binwidth=60*60*24, position="identity")
p1 <- p1 + scale_alpha_discrete(range=c(1, 0.2))
p1
答案 0 :(得分:0)
重新发布您的id
因素:
library(ggplot2)
tim.seq<-seq(as.POSIXlt("2013-07-01 00:00:00",origin = "1960-01-01",tz="GMT"),
as.POSIXlt("2013-07-8 00:00:00",origin = "1960-01-01",tz="GMT"),by="1 hour")
id<-factor(sample(LETTERS[1:3],length(tim.seq),T,prob=c(0.4,0.8,0.2)))
rank<-factor(id,levels=levels(id),label=c(2,3,1))
data<-data.frame(time=tim.seq,id=id,rank=rank)
# Relevel factor id
data$id <- relevel(data$id, "B")
p1 <- ggplot(data, aes(x=time, y=..count.., alpha=id))
p1<-p1+ geom_bar(fill="darkblue",binwidth=60*60*24,position = "identity")
# Changed your direction of alpha range
p1<-p1+ scale_alpha_discrete(range=c(0.2, 1))
p1
要反过来获取颜色渐变,您需要将比例从alpha
更改为colour
,因为Alpha混合不会在较暗的颜色上显示较浅的颜色,但是混合它们,使得情节不可读(用scale_alpha_discrete(range=c(1, 0.2))
尝试自己)。但是,我会留给你的。