将逻辑数据绘制为R中的填充方格行与空白方格行

将逻辑数据绘制为R中的填充方格行与空白方格行,r,plot,R,Plot,我正在跟踪我想在文本文件/电子表格/任何东西中建立的习惯。看起来是这样的: habits <- read.table(textConnection(" date study exercise clean 2019-01-08 TRUE TRUE TRUE 2019-01-09 FALSE FALSE TRUE 2019-01-10 TRUE TRUE TRUE 2019-01-11 FALSE TRUE TRUE 2019-01-12 TRUE FALSE TRUE 2019-01-13

我正在跟踪我想在文本文件/电子表格/任何东西中建立的习惯。看起来是这样的:

habits <- read.table(textConnection("
date study exercise clean
2019-01-08 TRUE TRUE TRUE
2019-01-09 FALSE FALSE TRUE
2019-01-10 TRUE TRUE TRUE
2019-01-11 FALSE TRUE TRUE
2019-01-12 TRUE FALSE TRUE
2019-01-13 FALSE TRUE TRUE
2019-01-14 TRUE TRUE TRUE
2019-01-15 FALSE FALSE TRUE
2019-01-16 FALSE FALSE TRUE
2019-01-17 FALSE FALSE TRUE
2019-01-18 FALSE FALSE TRUE
2019-01-19 FALSE FALSE TRUE
2019-01-20 FALSE TRUE TRUE
2019-01-21 FALSE FALSE TRUE
2019-01-22 TRUE FALSE TRUE
2019-01-23 FALSE TRUE TRUE
2019-01-24 TRUE FALSE TRUE
2019-01-25 FALSE FALSE TRUE
2019-01-26 TRUE FALSE TRUE
2019-01-27 FALSE FALSE TRUE
2019-01-28 TRUE TRUE FALSE
2019-01-29 FALSE TRUE TRUE
2019-01-30 TRUE TRUE TRUE
2019-01-31 FALSE TRUE TRUE
2019-02-01 TRUE FALSE TRUE
2019-02-02 FALSE TRUE TRUE
2019-02-03 TRUE TRUE TRUE
2019-02-04 FALSE FALSE TRUE
2019-02-05 FALSE FALSE TRUE
2019-02-06 FALSE TRUE TRUE
2019-02-07 TRUE FALSE TRUE
2019-02-08 FALSE FALSE TRUE
2019-02-09 FALSE FALSE TRUE
2019-02-10 TRUE TRUE TRUE
2019-02-11 FALSE FALSE TRUE
2019-02-12 FALSE TRUE TRUE
2019-02-13 FALSE FALSE TRUE
2019-02-14 FALSE FALSE FALSE
2019-02-15 FALSE FALSE FALSE
2019-02-16 FALSE FALSE FALSE
2019-02-17 TRUE FALSE TRUE
2019-02-18 FALSE FALSE TRUE
2019-02-19 FALSE FALSE TRUE
2019-02-20 FALSE FALSE TRUE
2019-02-21 FALSE FALSE TRUE
2019-02-22 TRUE FALSE TRUE
2019-02-23 FALSE FALSE FALSE
2019-02-24 FALSE FALSE FALSE
2019-02-25 FALSE FALSE FALSE
2019-02-26 FALSE FALSE FALSE
2019-02-27 FALSE FALSE FALSE
2019-02-28 FALSE FALSE FALSE
"), header = TRUE, colClasses=c("Date", "logical", "logical", "logical"))

habits这是一个开始

library(tidyverse) # really need ggplot2, dplyr and tidyr

habits %>% 
gather(activity, achievement, -date) %>% 
filter(achievement == TRUE) %>% # Only show sucesses
ggplot(aes(date, achievement))+
geom_tile()+ # This gives you the tile structure
facet_wrap(~activity, ncol = 1) # Could change this

这会给你一些看起来非常接近你想要的东西。可能有更好的方法可以在y轴上获得活动磁贴。

这里是一个开始

library(tidyverse) # really need ggplot2, dplyr and tidyr

habits %>% 
gather(activity, achievement, -date) %>% 
filter(achievement == TRUE) %>% # Only show sucesses
ggplot(aes(date, achievement))+
geom_tile()+ # This gives you the tile structure
facet_wrap(~activity, ncol = 1) # Could change this

这会给你一些看起来非常接近你想要的东西。可能有更好的方法来获得y轴上的活动磁贴。

这里是一个基本的R解决方案。它只是画了一堆矩形

BW = c("#555555", "#EEEEEE")
StartX = as.numeric(habits$date)[1]

dev.new(width=22,height=4)
plot(rep(2:5, length.out=nrow(habits)) ~ habits$date, type="n", 
    xlab="", ylab="", yaxt="n", xaxt="n", bty="n" )
for(j in 2:4) {
    for(i in 1:nrow(habits)) {
        polygon(c(StartX+i-1, StartX+i-1, StartX+i, StartX+i), 
            c(j,j+1,j+1,j), col=BW[habits[i,j]+1])
    }
}
axis(2, at=c(2.6, 3.6, 4.6), labels=names(habits[,2:4]), 
    lty=0, las=1, line=-2)
axis(1, at=habits$date+0.5, labels=as.character(habits$date), 
    lty=0, las=2, line=-0.8)

这是一个基本的R解决方案。它只是画了一堆矩形

BW = c("#555555", "#EEEEEE")
StartX = as.numeric(habits$date)[1]

dev.new(width=22,height=4)
plot(rep(2:5, length.out=nrow(habits)) ~ habits$date, type="n", 
    xlab="", ylab="", yaxt="n", xaxt="n", bty="n" )
for(j in 2:4) {
    for(i in 1:nrow(habits)) {
        polygon(c(StartX+i-1, StartX+i-1, StartX+i, StartX+i), 
            c(j,j+1,j+1,j), col=BW[habits[i,j]+1])
    }
}
axis(2, at=c(2.6, 3.6, 4.6), labels=names(habits[,2:4]), 
    lty=0, las=1, line=-2)
axis(1, at=habits$date+0.5, labels=as.character(habits$date), 
    lty=0, las=2, line=-0.8)