R中表格的条件格式...更好的方法? [英] Conditional formatting of table in R...a better way?
问题描述
尝试改进此代码.我所做的工作正常,但看起来很丑,非常笨拙.
寻找ggplot方法或更用户友好的方法.将不胜感激的提示和建议.
library("dplyr")i<-data.frame(RH = c(1,1,1,2,2,2,3,3,3),T = c(1,2,3,1,2,3,1,2,3),THI = c(8,8,5,7,5,10,5,8,7))table_thi<-tapply(thi $ THI,list(thi $ RH,thi $ T),均值)%&%;%as.table()x = 1:ncol(table_thi)y = 1:nrow(table_thi)中心<-expand.grid(y,x)图像(x,y,t(table_thi),col = c("lightgoldenrod","darkgoldenrod","darkorange"),休息= c(5,7,8,9),xaxt ='n',yaxt ='n',xlab ='',ylab =",ylim = c(max(y)+ 0.5,min(y)-0.5))文字(圆(中心[,2],0),圆(中心[,1],0),c(表格_thi),col =黑色")mtext(paste(attributes(table_thi)$ dimnames [[2]]),at = 1:ncol(table_thi),padj = -1)mtext(attributes(table_thi)$ dimnames [[1]],at = 1:nrow(table_thi),side = 2,las = 1,adj = 1.2)背斜(h = y + 0.5)背斜(v = x + 0.5)
这是怎么回事:
库(dplyr)库(ggplot2)这<-data.frame(相对湿度= c(1,1,1,2,2,2,3,3,3),T = c(1、2、3、1、2、3、1、2、3),THI = c(8、8、5、7、5、10、5、8、7))名称(thi)= c('col1','col2','thi')ggplot(thi,aes(x = col1,y = col2,fill = factor(thi),label = thi))+geom_tile()+geom_text()
或者根据 thi
是真正的 factor
(离散)还是连续变量,您可能想要这样的东西:
ggplot(thi,aes(x = col1,y = col2,fill = thi,label = thi))+geom_tile()+geom_text(颜色='白色')
注意:您可能要避免使用保留字或缩写的列名或变量名(例如,避免调用 T
,因为这是关键字 TRUE 的缩写)代码>).在上面的代码中,我重命名了data.frame的列.
但是,由于该问题说明了表的条件格式,因此您可能需要考虑
或者这个:
thi%>%gt()%>%data_color(列= vars(thi),颜色=比例尺:: col_factor(调色板="Set1",域= NULL))
或者也许是这样
thi%>%gt()%>%tab_style(样式= cells_styles(bkgd_color =#F9E3D6",text_style =斜体"),位置= cells_data(列= vars(thi),行=小于&= 7))
Trying to improve this code. What I have worked up works but looks ugly and is VERY clumsy.
Looking for a ggplot method or something that is more user friendly. Would appreciate the tips and advice.
library("dplyr")
thi <- data.frame(RH = c(1,1,1,2,2,2,3,3,3), T = c(1,2,3,1,2,3,1,2,3), THI = c(8,8,5,7,5,10,5,8,7))
table_thi <- tapply(thi$THI, list(thi$RH, thi$T), mean) %>% as.table()
x = 1:ncol(table_thi)
y = 1:nrow(table_thi)
centers <- expand.grid(y,x)
image(x, y, t(table_thi),
col = c("lightgoldenrod", "darkgoldenrod", "darkorange"),
breaks = c(5,7,8,9),
xaxt = 'n',
yaxt = 'n',
xlab = '',
ylab = '',
ylim = c(max(y) + 0.5, min(y) - 0.5))
text(round(centers[,2],0), round(centers[,1],0), c(table_thi), col= "black")
mtext(paste(attributes(table_thi)$dimnames[[2]]), at=1:ncol(table_thi), padj = -1)
mtext(attributes(table_thi)$dimnames[[1]], at=1:nrow(table_thi), side = 2, las = 1, adj = 1.2)
abline(h=y + 0.5)
abline(v=x + 0.5)
How about this:
library(dplyr)
library(ggplot2)
thi <- data.frame(
RH = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
T = c(1, 2, 3, 1, 2, 3, 1, 2, 3),
THI = c(8, 8, 5, 7, 5, 10, 5, 8, 7)
)
names(thi) = c('col1', 'col2', 'thi')
ggplot(thi, aes(x = col1, y = col2, fill = factor(thi), label = thi)) +
geom_tile() +
geom_text()
Or depending on whether thi
is really factor
(discrete) or continuous variable, you may want something like this:
ggplot(thi, aes(x = col1, y = col2, fill = thi, label = thi)) +
geom_tile() +
geom_text(color = 'white')
Note: You probably want to avoid using column or variable names that are reserved words or abbreviations (e.g. avoid calling something T
because that's an abbreviation for the keyword TRUE
). In the code above, I renamed the columns of your data.frame.
Since the question says conditional formatting of a table, however, you may want to consider the gt
package:
library(gt)
thi %>% gt()
Or this:
thi %>% gt() %>%
data_color(
columns = vars(thi),
colors = scales::col_factor(
palette = "Set1",
domain = NULL
))
Or maybe this:
thi %>% gt() %>%
tab_style(
style = cells_styles(
bkgd_color = "#F9E3D6",
text_style = "italic"),
locations = cells_data(
columns = vars(thi),
rows = thi <= 7
)
)
这篇关于R中表格的条件格式...更好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!