Sunday 8 December 2019

r - How can we make xkcd style graphs?

I designed a xkcd themed analytics calendar just using RStudio. Here is an example of bar plot xkcd style



  • Font used = HumorSans.ttf [link given above]

  • Package used [xkcd]


To generate this plot
Bar plot proxy for 'Dangers at Work'


Here is the code used


#using packages xkcd, ggplot
library(xkcd)
library(ggplot2)
font_import(pattern="[H/h]umor")
loadfonts()
### Set up the trial dataset
d1 <- data.frame('type'=c('DROWNING','RADIATION','TOILET',"ELECTRICAL",'NOISE','PANTRY','YOUR FALLING ON OBJECTS','OBJECTS FALLING ON YOU','BOSS','FIRE','TRAVEL TO WORK'),'score'=c(2,2,3,6,6,6,11,14,21,26,30))
# we will keep adding layers on plot p. first the bar plot
p <- NULL
p <- ggplot() + xkcdrect(aes(xmin = type-0.1,xmax= type+0.1,ymin=0,ymax =score),
d1,fill= "#D55E00", colour= "#D55E00") +
geom_text(data=d1,aes(x=type,y=score+2.5,label=score,ymax=0),family="Humor Sans") + coord_flip()
#hand drawn axes
d1long <- NULL
d1long <- rbind(c(0,-2),d1,c(12,32))
d1long$xaxis <- -1
d1long$yaxis <- 11.75
# drawing jagged axes
p <- p + geom_line(data=d1long,aes(x=type,y=jitter(xaxis)),size=1)
p <- p + geom_line(data=d1long,aes(x=yaxis,y=score), size=1)
# draw axis ticks and labels
p <- p + scale_x_continuous(breaks=seq(1,11,by=1),labels = data$Type) +
scale_y_continuous(breaks=NULL)
#writing stuff on the graph
t1 <- "GOOGLE RESULTS"
p <- p + annotate('text',family="Humor Sans", x=12.5, y=12, label=t1, size=6)
# XKCD theme
p <- p + theme(panel.background = element_rect(fill="white"),
panel.grid = element_line(colour="white"),axis.text.x = element_blank(),
axis.text.y = element_text(colour="black"),text = element_text(size=18, family="Humor Sans") ,panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.border = element_blank(),axis.title.y = element_blank(),axis.title.x = element_blank(),axis.ticks = element_blank())
print(p)

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...