给ggplot2来个「手绘风」
Posted pythonic生物人
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了给ggplot2来个「手绘风」相关的知识,希望对你有一定的参考价值。
本文借助xkcd在ggplot2里玩下手绘风,续前两篇:
xkcd主要有以下6个属性,详细功能都在后文代码里了~
theme_xkcd Creates an XKCD theme
xkcd-package Plotting ggplot2 Graphics in an XKCD Style
xkcdaxis Plot the axis
xkcdline Draw lines or circunferences
xkcdman Draw a stick figure
xkcdrect Draw fuzzy rectangles
xkcd使用前准备
#安装xkcd
options(repos=structure(c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")))
install.packages("xkcd",dependencies = TRUE)
# 准备xkcd字体--本文在mac下,其他系统会有所差异
library(extrafont)
download.file("http://simonsoftware.se/other/xkcd.ttf", dest = "xkcd.ttf", mode = "wb")
system("cp xkcd.ttf ~/Library/Fonts/")
font_import(pattern = "[X/x]kcd", prompt = FALSE)
xkcd使用小例子,全面介绍了xkcd 6个属性的使用
volunteers <- data.frame(year = c(2007:2011), number = c(56470, 56998, 59686, 61783,
64251))
datalines <- data.frame(xbegin=c(2007.3,2008.5),ybegin=c(63000,59600), xend=c(2012.5,2011.3), yend=c(68400,70000))
p <- ggplot() + labs(title="Lovely XKCD IN R") +
#xkcdrect绘制漫画风格柱子边界
xkcdrect(aes(xmin = year, xmax = year + 0.3, ymin = number, ymax = number +
3600), volunteers, fill = "#dc2624", colour = "#2b4750") +
#theme_xkcd添加漫画风格主题
theme_xkcd() +
#xkcdaxis添加漫画风格轴
xkcdaxis(range(c(2007, 2012)), range(c(56000, 70000)))
xrange <- range(volunteers$year)
yrange <- range(volunteers$number)
ratioxy <- diff(xrange)/diff(yrange)
mapping <- aes(x = x, y = y, scale = scale, ratioxy = ratioxy, angleofspine = angleofspine,
anglerighthumerus = anglerighthumerus, anglelefthumerus = anglelefthumerus, anglerightradius = anglerightradius,
angleleftradius = angleleftradius, anglerightleg = anglerightleg, angleleftleg = angleleftleg,
angleofneck = angleofneck, color = color)
dataman <- data.frame(x = c(2009.5), y = c(70000), scale = c(2000), ratioxy = ratioxy,
angleofspine = -pi/2, anglerighthumerus = -pi/6, anglelefthumerus = pi + pi/6,
anglerightradius = -pi/12, angleleftradius = -pi/8, angleleftleg = 3 * pi/2 +
pi/12, anglerightleg = 3 * pi/2 - pi/12, angleofneck = runif(1, min = 3 *
pi/2 - pi/10, max = 3 * pi/2 + pi/10), color = c("MAX"))
#xkcdman添加漫画风格小人
p + xkcdman(mapping, dataman) +
#xkcdline添加漫画曲线
xkcdline(aes(x=xbegin,y=ybegin,xend=xend,yend=yend),datalines, xjitteramount = 0.7)
更多demo
。。。。。。。。。。。
以上是关于给ggplot2来个「手绘风」的主要内容,如果未能解决你的问题,请参考以下文章