ggplot2坐标系转换的应用

ggplot2坐标系转换的应用

这篇是用ggplot2画了一个我也叫不上名的炫酷图表的学习笔记,讲述了如何使用坐标系转换绘制一幅神奇的图表。不同于原作者的做法,我最后没有手动修图,而是全部使用R完成。

构造数据

R
1
2
3
4
5
6
7
8
9
10
library(ggplot2)
library(grid)

# 构造数据
mydata <- data.frame(
id = 1:13,
class = rep_len(1:4, length = 13),
Label=c("Events","Lead List","Partner","Markeiting & Advertising","Tradeshows","Paid Search","Webinar","Emial Campaign","Sales generated","Website","Other","Facebook/Twitter/\nOther Social","Employee & Customer\nReferrals"),
Value=c(7.6,15.5,17.9,21.8,29.6,29.7,32.7,43.0,57.5,61.4,67.4,68.6,68.7)
)

绘制基本的柱状图

R
1
2
3
4
5
6
7
8
(p <- ggplot(mydata) +
geom_col(aes(x = id, y = Value/2 + 150,
fill = factor(class)),
colour = NA, width = 1) +
geom_col(aes(x = id, y = 150 - Value/2),
fill = "white", colour = "white",
width = 1) +
scale_x_continuous(limits = c(0, 26), expand = c(0, 0)))

使用极坐标变换

R
1
2
3
(p <- p + coord_polar(theta = 'x',
direction = 1,
start = -14.275))

主题修饰

R
1
2
3
4
(p <- p + scale_fill_manual(
values = c("#31A2CE","#DDB925","#3F9765","#C84F44"),
guide = F
) + theme_void())

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
p <- ggplot() +
geom_col(data = mydata,
aes(x = id, y = Value/2 + 150,
fill = factor(class)), colour = NA, width = 1) +
geom_col(data = mydata,
aes(x = id, y = 150 - Value/2), fill = "white",
colour = "white", width = 1) +
geom_point(data = mydata[which(mydata$id < 11),],
aes(x = id, y = 150)) +
geom_line(data = NULL, aes(x = rep(c(0.5, 13.5), 2),
y = rep(c(126, 174), each = 2),
group = rep(c(1, 2), each = 2)),
linetype = 2, size = 0.25) +
scale_x_continuous(limits = c(0, 26), expand = c(0, 0)) +
coord_polar(theta = 'x', start = -14.275,
direction = 1) +
scale_fill_manual(values = c("#31A2CE", "#DDB925", "#3F9765", "#C84F44"), guide=FALSE) +
theme_void();p

使用cowplot添加标签

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
library(cowplot)
ggdraw(p) +
draw_label(label = "Events,Lead Lists and partners-\nmore likely be colosed-lost", x = 0.5, y = 0.65, size = 11, fontface = 'bold') +
draw_label(label = "Marketing events may by fun, but they create\nlousy sales opprunities.When analyzing share\nof closed-won vs.closed-lost opportunities,\nevents,leads lists and partners seem to provide the\nworst performance,while refreals and social\nprovide the best performance.", x = 0.5, y = 0.55, size = 9) +
draw_label(label = "68.7%", colour = "white",
x = 0.71, y = 0.56, size = 10) +
draw_label(label = "Employee & Customer\nReferrals",
colour = "white",
x = 0.705, y = 0.53, size = 6) +
draw_label(label = "68.6%", colour = "white",
x = 0.695, y = 0.64, size = 10) +
draw_label(label = "Facebook/Twitter/\nOther Social",
colour = "white",
x = 0.69, y = 0.61, size = 6) +
draw_label(label = "67.4%", colour = "white",
x = 0.672, y = 0.71, size = 10) +
draw_label(label = "Other",
colour = "white",
x = 0.67, y = 0.68, size = 6) +
draw_label(label = "Website", x = 0.68, y = 0.825,
colour = "black", size = 8) +
draw_label(label = "61.4%", x = 0.68, y = 0.85,
colour = "black", size = 10, fontface = "bold") +
draw_label(label = "Sales\ngenerated", x = 0.63, y = 0.88,
colour = "black", size = 8) +
draw_label(label = "57.5%", x = 0.63, y = 0.91,
colour = "black", size = 10, fontface = "bold") +
draw_label(label = "Emial\nCampaign", x = 0.565, y = 0.90,
colour = "black", size = 8) +
draw_label(label = "43.0%", x = 0.565, y = 0.93,
colour = "black", size = 10, fontface = "bold") +
draw_label(label = "Webinar", x = 0.5, y = 0.91,
colour = "black", size = 8) +
draw_label(label = "32.7%", x = 0.5, y = 0.94,
colour = "black", size = 10, fontface = "bold") +
draw_label(label = "Paid Search", x = 0.442, y = 0.89,
colour = "black", size = 8) +
draw_label(label = "29.7%", x = 0.442, y = 0.92,
colour = "black", size = 10, fontface = "bold") +
draw_label(label = "Tradeshows", x = 0.375, y = 0.86,
colour = "black", size = 8) +
draw_label(label = "29.6%", x = 0.375, y = 0.89,
colour = "black", size = 10, fontface = "bold") +
draw_label(label = "Markeiting & \nAdvertising", x = 0.32, y = 0.805,
colour = "black", size = 8) +
draw_label(label = "21.8%", x = 0.32, y = 0.835,
colour = "black", size = 10, fontface = "bold") +
draw_label(label = "Partner", x = 0.27, y = 0.705,
colour = "black", size = 8) +
draw_label(label = "17.9%", x = 0.27, y = 0.735,
colour = "black", size = 10, fontface = "bold") +
draw_label(label = "Lead List", x = 0.24, y = 0.607,
colour = "black", size = 8) +
draw_label(label = "15.5%", x = 0.24, y = 0.637,
colour = "black", size = 10, fontface = "bold") +
draw_label(label = "Events", x = 0.23, y = 0.507,
colour = "black", size = 8) +
draw_label(label = "7.6%", x = 0.23, y = 0.537,
colour = "black", size = 10, fontface = "bold")

# R

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×