Creating pretty dials

1 d3w3uiLunDng-Cuj6rEO-A

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

1 On-eNjk5ksLXrlm5Q6fHHQ

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s