问题
I have the following data.frame
:
df <- data.frame(id = c("a","b","c","d"),
s1.pch = c(NA,5,5,5),
s1.col = c(NA,"red","green","yellow"),
s2.pch = c(3,3,3,3),
s2.col = c("blue","red","green","yellow"),
s3.pch = c(1,1,NA,1),
s4.col = c("blue","red",NA,"yellow"))
# df
#
# id s1.pch s1.col s2.pch s2.col s3.pch s4.col
# 1 a NA <NA> 3 blue 1 blue
# 2 b 5 red 3 red 1 red
# 3 c 5 green 3 green NA <NA>
# 4 d 5 yellow 3 yellow 1 yellow
I want to plot it as a table, where row names are df$id
and the column names are the s1, s2, s3 prefixes in df
's column names. The elements of the table are colored shapes where the shape is defined by the pch value in the columns ending with pch and the colors are defined by the columns ending with col.
This is what the figure should like for df
.
回答1:
you could combine the parameters and define a custom function for the cells in gridExtra::tableGrob (I think it only works in the dev version)
library(grid)
# devtools::install_github('baptiste/gridextra')
library(gridExtra)
df <- data.frame(id = c("a","b","c","d"),
s1.pch = c(NA,5,5,5),
s1.col = c(NA,"red","green","yellow"),
s2.pch = c(3,3,3,3),
s2.col = c("blue","red","green","yellow"),
s3.pch = c(1,1,NA,1),
s3.col = c("blue","red",NA,"yellow"))
d <- data.frame(id=df$id,
s1 = paste(df$s1.col, df$s1.pch, sep=","),
s2 = paste(df$s2.col, df$s2.pch, sep=","),
s3 = paste(df$s3.col, df$s3.pch, sep=","))
my_fun <- function(label, ...){
s <- strsplit(label, ",")[[1]]
col <- s[1]
pch <- ifelse(s[2]=="NA", NA, as.numeric(s[2]))
pointsGrob(0.5,0.5,pch=pch, gp=gpar(col=col, lwd=3, cex=0.5))
}
tt <- ttheme_minimal(12, core=list(fg_fun = my_fun),
rowhead=list(fg_params=list(fontface="bold")))
grid.newpage()
grid.table(d[,-1], rows=levels(d[,1]), theme = tt)
回答2:
Here an option using ggplot2
. It is nearly the desired output except the bold effect.
library(data.table)
library(ggplot2)
## reshape data
dd <-
melt(setDT(dat),id="id")[,c("var", "type"):=tstrsplit(variable,'[.]')][,variable:=NULL]
#" change factor order
dd[,id:=factor(id,as.character(rev(unique(dd$id))))]
## extract shape scale
shape_scale <- unique(dd[type!="col"][!is.na(value),list(var,value)])
shape_scale <- setNames(as.numeric(shape_scale$value),shape_scale$var)
## color scale
cols <- unique(dd[type=="col" & !is.na(value),value])
col_scale <- setNames(cols,cols)
## plot
ggplot(dd[type!="pch"]) +
geom_point(aes(x=var,y=id,label=value,shape=var,color=value),size=10) +
theme_classic() +
theme(axis.line=element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank(),
axis.text=element_text(size = 20))+
theme(legend.position = "none") +
scale_color_manual(values = col_scale) +
scale_shape_manual(values = shape_scale)
回答3:
Here's another ggplot
solution.
library(ggplot2)
gg.df <- reshape(df,varying=list(c(2,4,6),c(3,5,7)),v.names=c("pch","col"),dir="long")
gg.df$id <- factor(gg.df$id, levels=rev(levels(gg.df$id)))
ggplot(gg.df, aes(x=paste0("s",time),y=id)) +
geom_point(aes(shape=pch, color=col), size=5)+
scale_color_identity()+
scale_shape_identity()+
labs(x=NULL,y=NULL)+
theme_classic()+
theme(panel.background=element_rect(fill="black"),axis.text=element_text(size=20))
This is similar to the other answer except:
- Uses
reshape(...)
in base R rather than data.table melt. - Takes advantage of
scale_shape_identity(...)
andscale_color_identity(...)
rather than creating shape and color vectors. - I set the background to black just so the yellow is more apparent.
来源:https://stackoverflow.com/questions/32708192/plot-a-table-with-r