Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot a table with R

Tags:

plot

r

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.

enter image description here

like image 986
user1701545 Avatar asked Dec 06 '25 07:12

user1701545


2 Answers

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)

enter image description here

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)
like image 189
baptiste Avatar answered Dec 08 '25 20:12

baptiste


Here an option using ggplot2. It is nearly the desired output except the bold effect.

enter image description here

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)
like image 33
agstudy Avatar answered Dec 08 '25 22:12

agstudy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!