ggplot可视化表格,来替代Excal吧
写在前面
表格用于展示原始数据,用于展示处理程度最低的数据,对于了解研究的最原始的状况,最真实的尚未人工修饰的十分重要。
ggplot 展示表格
library(ggpubr)
df <- head(iris)
展示基本数据表格
默认灰色主题
# Default table
# Remove row names using rows = NULL
ggtexttable(df, rows = NULL)
#### 无边框模式 (ttheme(“blank”))# Blank theme
ggtexttable(df, rows = NULL, theme = ttheme("blank"))
#### 双线表 (ttheme(“light”))# light theme
ggtexttable(df, rows = NULL, theme = ttheme("light"))
#### 白底黑字全边框# classic theme
ggtexttable(df, rows = NULL, theme = ttheme("classic"))
仅仅只有竖线表格
# minimal theme
ggtexttable(df, rows = NULL, theme = ttheme("minimal"))
Excel格式表格
# Medium blue (mBlue) theme
ggtexttable(df, rows = NULL, theme = ttheme("mBlue"))
#### 如何修改背景颜色
colnames.style 用于设置表头,color设置表头边框颜色,fill设置表头背景填充颜色
tbody.style:用于设置表格主题,color设置表头边框颜色,fill设置表头背景填充颜色
ggtexttable(df, rows = NULL,
theme = ttheme(
colnames.style = colnames_style(color = "white", fill = "#8cc257"),
tbody.style = tbody_style(color = "black", fill = c("#e8f3de", "#d3e8bb"))
)
)
如何对表格背景使用一组颜色
# Use RColorBrewer palette
# Provide as many fill color as there are rows in the table body, here nrow = 6
ggtexttable(df,
theme = ttheme(
colnames.style = colnames_style(fill = "white"),
tbody.style = tbody_style(fill = get_palette("RdBu", 6))
)
)
# Text justification
#::::::::::::::::::::::::::::::::::::::::::::::
# Default is "centre" for the body and header, and "right" for the row names.
# Left justification: hjust=0, x=0.1
# Right justification: hjust=1, x=0.9
tbody.style = tbody_style(color = "red",
fill = c("#e8f3de", "#d3e8bb"), hjust=1, x=0.9)
library(scales)
show_col(c("#e8f3de", "#d3e8bb"))
ggtexttable(head(iris), rows = NULL,
theme = ttheme(
colnames.style = colnames_style(color = "white", fill = "blue"),
tbody.style = tbody.style
)
)
#### 如何高亮一个单元格tab <- ggtexttable(head(iris), rows = NULL,
theme = ttheme("classic"))
tab <- table_cell_font(tab, row = 3, column = 2,
face = "bold")
tab <- table_cell_bg(tab, row = 4, column = 3, linewidth = 5,
fill="darkolivegreen1", color = "darkolivegreen4")
tab
如何组合图形和表格
# Combine density plot and summary table
#:::::::::::::::::::::::::::::::::::::
# Density plot of "Sepal.Length"
density.p <- ggdensity(iris, x = "Sepal.Length",
fill = "Species", palette = "jco")
# Draw the summary table of Sepal.Length
# Descriptive statistics by groups
stable <- desc_statby(iris, measure.var = "Sepal.Length",
grps = "Species")
stable <- stable[, c("Species", "length", "mean", "sd")]
stable.p <- ggtexttable(stable, rows = NULL,
theme = ttheme("mOrange"))
# Arrange the plots on the same page
ggarrange(density.p, stable.p,
ncol = 1, nrow = 2,
heights = c(1, 0.5))