R ggplot以错误的顺序填充数据
我想做一些类似于日历绘图的事情,但要在一个月内完成几个小时(而不是一年中的几天)。我设法用ggplot实现了这一点,但数据的填充顺序错误 我做了一个可复制的例子:R ggplot以错误的顺序填充数据,r,ggplot2,R,Ggplot2,我想做一些类似于日历绘图的事情,但要在一个月内完成几个小时(而不是一年中的几天)。我设法用ggplot实现了这一点,但数据的填充顺序错误 我做了一个可复制的例子: library(ggplot2) library(scales) device <- "test" dat <- data.frame(matrix(nrow=31*24, ncol=2)) dat[,1] <- seq.POSIXt(from=(as.POSIXct("2016-12-01 00:00:00")),
library(ggplot2)
library(scales)
device <- "test"
dat <- data.frame(matrix(nrow=31*24, ncol=2))
dat[,1] <- seq.POSIXt(from=(as.POSIXct("2016-12-01 00:00:00")), length.out=(31*24), by="1 hour")
dat[,2] <- seq(from=1, to=nrow(dat), by=1)
colnames(dat) <- c("Zeit", device)
dat$month<-as.numeric(as.POSIXlt(dat$Zeit)$mon+1)
dat$monthf<-factor(dat$month)
dat$monthf<-factor(dat$month,levels=as.character(1:12),labels=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"),ordered=TRUE)
dat$week <- as.numeric(format(dat$Zeit,"%W"))
dat$weekday = as.POSIXlt(dat$Zeit)$wday
dat$weekdayf<-factor(dat$weekday,levels=(c(1:6,0)),labels=(c("Mon","Tue","Wed","Thu","Fri","Sat","Sun")),ordered=TRUE)
dat$day <- as.numeric(format(dat$Zeit,"%d"))
dat$dayf<-factor(dat$day)
dat$hour <- as.numeric(format(dat$Zeit,"%H"))
dat$hourf<-factor(dat$hour)
plot(ggplot(dat, aes(hourf, week, fill = dat[2])) +
scale_y_reverse() +
geom_tile(colour = "white") +
facet_wrap(monthf~weekdayf, ncol=7) +
scale_fill_gradientn(colours=c("darkgreen", "green", "yellow", "red","darkred"),
values=rescale(c(0, 0.25, 0.5, 0.75, 1)),
guide="colorbar") +
xlab("") + ylab("") + ggtitle(device)
库(ggplot2)
图书馆(比例尺)
设备可能更容易使用facet\u grid
ggplot(dat, aes(hourf, 1, fill = test)) +
geom_tile(colour = "white") +
facet_grid(week~monthf+weekdayf, switch = 'y') +
scale_fill_gradientn(colours=c("darkgreen", "green", "yellow", "red","darkred"),
values=rescale(c(0, 0.25, 0.5, 0.75, 1)),
guide="colorbar") +
xlab("") + ylab("") +
ggtitle(device) +
theme(axis.ticks.y = element_blank(), axis.text.y = element_blank())
另外,请按名称引用变量!因此,使用test
而不是dat[2]
如果您不知道变量名,需要以编程方式进行设置,请改用aes\u string
或aes\u
ggplot(dat, aes(hourf, 1, fill = test)) +
geom_tile(colour = "white") +
facet_grid(week~monthf+weekdayf, switch = 'y') +
scale_fill_gradientn(colours=c("darkgreen", "green", "yellow", "red","darkred"),
values=rescale(c(0, 0.25, 0.5, 0.75, 1)),
guide="colorbar") +
xlab("") + ylab("") +
ggtitle(device) +
theme(axis.ticks.y = element_blank(), axis.text.y = element_blank())
使用facet\u grid
可能更容易
ggplot(dat, aes(hourf, 1, fill = test)) +
geom_tile(colour = "white") +
facet_grid(week~monthf+weekdayf, switch = 'y') +
scale_fill_gradientn(colours=c("darkgreen", "green", "yellow", "red","darkred"),
values=rescale(c(0, 0.25, 0.5, 0.75, 1)),
guide="colorbar") +
xlab("") + ylab("") +
ggtitle(device) +
theme(axis.ticks.y = element_blank(), axis.text.y = element_blank())
另外,请按名称引用变量!因此,使用test
而不是dat[2]
如果您不知道变量名,需要以编程方式进行设置,请改用aes\u string
或aes\u
ggplot(dat, aes(hourf, 1, fill = test)) +
geom_tile(colour = "white") +
facet_grid(week~monthf+weekdayf, switch = 'y') +
scale_fill_gradientn(colours=c("darkgreen", "green", "yellow", "red","darkred"),
values=rescale(c(0, 0.25, 0.5, 0.75, 1)),
guide="colorbar") +
xlab("") + ylab("") +
ggtitle(device) +
theme(axis.ticks.y = element_blank(), axis.text.y = element_blank())
当使用ggplot()
函数时,您应该仅通过其名称引用您的列-repalcefill=dat[2]
withfill=test
。当使用ggplot()
函数时,您应该仅通过其名称引用您的列-repalcefill=dat[2]
withfill=test
。