我有一个带有几个二进制变量的表。是否可以制作一张表格,使所有带有1的单元格都为黑色。像这样
答案 0 :(得分:1)
使用gridExtra
创建一个gtable。
library(grid)
library(gridExtra)
set.seed(1)
sample.data <- matrix(data=sample(0:1, 5*5, r=TRUE), nrow=5, ncol=5)
colnames(sample.data) <- LETTERS[1:5]
rownames(sample.data) <- letters[1:5]
# for cut() to work data has to be of class matrix or table.
# data.frame will cause error
fcol <- as.character(cut(
sample.data, c(0, 0.5, 1), include.lowest=TRUE,
labels=c("black", "white")))
bcol <- as.character(cut(
sample.data, c(0, 0.5, 1), include.lowest=TRUE,
labels=c("white", "black")))
# building a theme.
# core specifies the features of the main field,
# rowhead the row titles, and colhead the column titles.
# fg_params specifies the features of the foreground, namely the text,
# bg_params specifies the features of the background, like the fill
# and border colors.
t1 <- ttheme_minimal(base_size=20,
core=list(
fg_params=list(col=fcol),
bg_params=list(fill=bcol, col="black", lwd=2)
),
rowhead=list(
fg_params=list(fontface=1)
),
colhead=list(
fg_params=list(fontface=1, rot=90)
)
)
grid.newpage()
grid.table(sample.data, theme=t1)
答案 1 :(得分:1)
我们将使用图像中的数据(尽管这可能不是您的用例)。这将模拟它:
library(tidyverse)
set.seed(2018-11-20)
# expand.grid just creates a data frame out of all the combinations
# of the provided vectors.
# tbl_df just turns the output into a data frame that (oversimplifying)
# prints better
# set_names is just setting column names
# the mutate line is where we generate the 0/1s
expand.grid(
sprintf("Address%s", 1:9),
sprintf("Transaction%s", 1:16),
stringsAsFactors = FALSE
) %>%
tbl_df() %>%
set_names(c("address", "transaction")) %>%
mutate(value = sample(0:1, n(), replace=TRUE)) -> xdf
xdf
## # A tibble: 144 x 3
## address transaction value
## <chr> <chr> <int>
## 1 Address1 Transaction1 0
## 2 Address2 Transaction1 1
## 3 Address3 Transaction1 1
## 4 Address4 Transaction1 0
## 5 Address5 Transaction1 1
## 6 Address6 Transaction1 1
## 7 Address7 Transaction1 0
## 8 Address8 Transaction1 0
## 9 Address9 Transaction1 1
## 10 Address1 Transaction2 0
## # ... with 134 more rows
如果您的数据是“宽”还是“长”(如上所述),请查看tidyr::gather
函数。
现在,这只是更多的数据争执和一些基本的ggplot2。首先,我们将使用您的逻辑并为每个单元格定义单元格填充和文本颜色。并且,由于我们希望以相反的方向绘制X轴,因此我们也对其进行了排序,以便ggplot2可以满足这种需求。
mutate(
xdf,
fill = ifelse(value == 1, "black", "white"),
color = ifelse(value == 1, "white", "black"),
address = factor(address, levels = sort(unique(address), decreasing = TRUE))
) -> xdf
现在,我们使用平铺层,并在其上面放置一个文本层。我们在上面的mutate
调用中使用了“真实的”颜色名称,因此我们可以通过用I()
包围映射的列美感来利用ggplot2的“ hack”,这告诉ggplot2按原样使用值,并且也不必费心给我们一个图例(如果您愿意,我们可以生成一个图例,但这不在稀疏问题中)。
然后,我们删除标签和地块之间的一些空白,将Y标签粘贴在顶部并旋转它们(尽管这不是一个好习惯,因为让人们扭动头来阅读您的地块并不酷)。
ggplot(xdf, aes(x = transaction, y = address)) +
geom_tile(
aes(fill = I(fill)),
color = "#2b2b2b", size=0.125,
) +
geom_text(
aes(label = value, color = I(color))
) +
scale_x_discrete(expand=c(0,0), position = "top") +
scale_y_discrete(expand=c(0,0)) +
labs(x = NULL, y = NULL) +
hrbrthemes::theme_ipsum_rc(grid="XY") +
theme(axis.text.x = element_text(angle=90, vjust=0.5, hjust=1))