R can do a lot of things really really well, but one thing it struggles to do (at least in base R anyway) is to create pretty visuals. I have recently been creating visuals for some clients and wrote some code to produce some pretty dials in R. I will start by building a simple dial, and then adding various bits to it.

1.Simple dial

###Create a sequence from your breaks
breaks.min=seq(from=0,to=100,by=2)
###Creating the dial shape
get.poly <- function(a,b,r1=0.5,r2=1.0) {
th.start <- pi*(1-a/100)
th.end <- pi*(1-b/100)
th <- seq(th.start,th.end,length=100)
x <- c(r1*cos(th),rev(r2*cos(th)))
y <- c(r1*sin(th),rev(r2*sin(th)))
return(data.frame(x,y))
}
###A nice big empty plotting area
par(mar=c(10,4,10,4))
plot(1,ylim=c(0,1),xlim=c(-1,1),type="n",axes=F,xlab="",ylab="")
###Dialling in the numbers
#Where you want the dial to finish
pos<-70
#The coloured section up to your position
p<-get.poly(breaks[1],pos,0.75)
polygon(p,col="lightgreen",border=NA)
#From your position to the end of the dial
p<-get.poly(pos,breaks[11],0.75)
polygon(p,col="lightgrey",border=NA)

Ta da! Here we have the output for the most simple of simple dials

### Like this:

Like Loading...

## Author: Jamie Samson

I am Head of Data Science and Insights at www.mindfolio.com. I am a lover of R and consider myself an Rtist in data visualisations.
View all posts by Jamie Samson