Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Punchcard plot in R

Tags:

plot

r

Can anyone help me out on how can I create a punchcard plot in R (without using ggplot)?

Just like this one that was made in python: enter image description here

like image 817
Gago-Silva Avatar asked Nov 18 '13 09:11

Gago-Silva


2 Answers

The black border looks good on the original figure. It can be reproduced with symbols(). sqrt((pctable$value / max(pctable$value)) / pi) added in order to make sure that values are proportional to the symbols' surface rather than to their radius.

pctable <- data.frame(expand.grid(weekday=c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"),
                              day=1:22), value=abs(rnorm(nrow(pctable), 20, 30)))

par(list(las=1, mar=c(6,6,1,1), mgp=c(4.5,1,0)))

bubble.size <- sqrt((pctable$value / max(pctable$value)) / pi)
symbols(pctable$day, pctable$weekday, circles=bubble.size,
    inches=.2, fg="black", bg="blue", yaxt="n", xaxt="n", xlab="Day", ylab="Weekday")
axis(1, at=1:22, labels=c(1:22)) 
axis(2, at=1:7,labels=rev(c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")), cex.axis=0.7) 

enter image description here

like image 182
SESman Avatar answered Nov 17 '22 23:11

SESman


How about this?

pctable<-data.frame(expand.grid(c("monday","tuesday","wednesday","thursday","friday","saturday","sunday"),1:22))
colnames(pctable)<-c("weekday","day")
pctable$value=rnorm(nrow(pctable),15,8)
plot(pctable$day,pctable$weekday,cex=pctable$value/(max(pctable$value)/5), col=rgb(0,0,1), pch=19, yaxt="n", xaxt="n")
axis(1, at=1:22, labels=c(1:22)) 
axis(2, at=1:7, labels=c("monday","tuesday","wednesday","thursday","friday","saturday","sunday"), cex.axis=0.7) 

You will need to probably have a function to return the correct cex='?' value for your scale, it won't resize.

Out of interest, why not ggplot? plot

like image 32
Troy Avatar answered Nov 18 '22 00:11

Troy