R語言繪製漫畫圖表(代碼全)

漫畫 R語言 Xkcd 大數據 天善智能 2017-04-06

每日干貨好文分享丨請點擊+關注

歡迎關注天善智能微信公眾號,我們是專注於商業智能BI,大數據,數據分析領域的垂直社區。

對商業智能BI、大數據分析挖掘、機器學習,python,R等數據領域感興趣的同學加微信:tstoutiao,邀請你進入頭條數據愛好者交流群,數據愛好者們都在這兒。

今天為大家分享如何用R語言繪製出漫畫風的圖表,算是R語言圖表中的另類吧,為大家在學習R的路上增添樂趣。

需要包

library(ggplot2)

library(ggrepel)

library(xkcd)

繪圖

name = rownames(mtcars)

mtcar = data.frame(name=name,mtcars)

xrange <- range(mtcar$mpg)

yrange <- range(mtcar$hp)

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)

R語言繪製漫畫圖表(代碼全)

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 <- NULL

data$x <- seq(1, 10, 0.1)

data$y1 <- sin(data$x)

data$y2 <- cos(data$x)

data$xaxis <- -1.5

data <- as.data.frame(data)

### XKCD theme

theme_xkcd <- theme(

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 <- ggplot(data=data, aes(x=x, y=y1))+

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

R語言繪製漫畫圖表(代碼全)

對商業智能BI、大數據分析挖掘、機器學習,python,R等數據領域感興趣同學加微信:tstoutiao,邀請您加入頭條數據愛好者交流群,數據愛好者們都在這兒。

R語言繪製漫畫圖表(代碼全)

本文來源自天善社區EasyCharts的博客(公眾號),作者:周世榮。

原文鏈接:https://www.hellobi.com/u/38793/articles 。

相關推薦

推薦中...