今天为大家分享如何用R语言绘制出漫画风的图表,算是R语言图表中的另类吧,为大家在学习R的路上增添乐趣。
需要包
library(ggplot2)
library(ggrepel)
library(xkcd)
绘图
name = rownames(mtcars)
mtcar = data.frame(name=name,mtcars)
xrange yrange
ggplot(mtcar, aes(mpg,hp))+
geom_point(shape=21,fill='skyblue',size=2)+
geom_text_repel(aes(label=name),size=5,
fontface = 'italic',family = 'HersheyScript')+
labs(title= "A Plot Script With R",subtitle = Sys.time(),
caption = R.version.string)+
xkcdaxis(xrange,yrange)
Stack Overflow 社区的作品
library(ggplot2)
library(extrafont)
### Already have read in fonts (see previous answer on how to do this)
download.file("http://simonsoftware.se/other/xkcd.ttf",
dest="xkcd.ttf", mode="wb")
system("mkdir ~/.fonts")
system("cp xkcd.ttf ~/.fonts")
font_import(pattern = "[X/x]kcd", prompt=FALSE)
loadfonts()
### Set up the trial dataset
data data$x data$y1 data$y2 data$xaxis
data
### XKCD theme
theme_xkcd panel.background = element_rect(fill="white"),
axis.ticks = element_line(colour=NA),
panel.grid = element_line(colour="white"),
axis.text.y = element_text(colour=NA),
axis.text.x = element_text(colour="black"),
text = element_text(size=16, family="HersheyScript")
)
### Plot the chart
p geom_line(aes(y=y2), position="jitter")+
geom_line(colour="white", size=3, position="jitter")+
geom_line(colour="red", size=1, position="jitter")+
geom_text(family="HersheyScript", x=6, y=-1.2, label="A SIN AND COS CURVE")+
geom_line(aes(y=xaxis), position = position_jitter(h = 0.005), colour="black")+
scale_x_continuous(breaks=c(2, 5, 6, 9),
labels = c("YARD", "STEPS", "DOOR", "INSIDE"))+labs(x="", y="")+
theme_xkcd()
p
关注“数据与算法之美”
获取更多最全数据集
-----这里是数学思维的聚集地------
超模Python交流QQ群
579295081
超模君等你来撩