Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot以错误的顺序填充数据_R_Ggplot2 - Fatal编程技术网

R 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")),

我想做一些类似于日历绘图的事情,但要在一个月内完成几个小时(而不是一年中的几天)。我设法用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")), 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()
函数时,您应该仅通过其名称引用您的列-repalce
fill=dat[2]
with
fill=test
。当使用
ggplot()
函数时,您应该仅通过其名称引用您的列-repalce
fill=dat[2]
with
fill=test