如何使用 ggplot2 在 r 中轻松创建浮雕图
凹凸图是一种图表,它显示不同组随时间的排名而不是绝对值,以强调组的顺序而不是变化量。
本教程介绍如何使用 ggplot2 在 R 中轻松创建凹凸图。
示例:创建浮雕图形
要在 R 中创建凹凸图,我们首先需要加载两个包: dplyr和ggplot2 :
library(ggplot2) #for creating bump chart library(dplyr) #for manipulating data
接下来,我们将创建一些要使用的数据:
#set the seed to make this example reproducible
set.seed(10)
data <- data.frame(team = rep(LETTERS[1:5], each = 10),
random_num = runif(50),
day = rep(1:10, 5))
data <- data %>%
group_by(day) %>%
arrange(day, desc(random_num), team) %>%
mutate(rank = row_number()) %>%
A group()
head(data)
# team random_num day rank
#1 C 0.865 1 1
#2 B 0.652 1 2
#3 D 0.536 1 3
#4 A 0.507 1 4
#5 E 0.275 1 5
#6 C 0.615 2 1
该数据库仅显示五支不同球队在 10 天内的“排名”。
我们可以使用 ggplot2 创建进度图来可视化每个团队在此期间每天的排名:
ggplot(data, aes(x = day, y = rank, group = team)) + geom_line(aes(color = team, alpha = 1), size = 2) + geom_point(aes(color = team, alpha = 1), size = 4) + scale_y_reverse(breaks = 1:nrow(data))
这个凹凸图以您想要的格式显示数据,但它非常丑陋。通过一些美学上的改变,我们可以使这幅画变得更好。
风格化凹凸图形
为了改善图表的外观,我们可以使用Dominik Koch创建的以下主题:
my_theme <- function() { # Colors color.background = "white" color.text = "#22211d" # Begin construction of chart theme_bw(base_size=15) + # Format background colors theme(panel.background = element_rect(fill=color.background, color=color.background)) + theme(plot.background = element_rect(fill=color.background, color=color.background)) + theme(panel.border = element_rect(color=color.background)) + theme(strip.background = element_rect(fill=color.background, color=color.background)) + # Format the grid theme(panel.grid.major.y = element_blank()) + theme(panel.grid.minor.y = element_blank()) + theme(axis.ticks = element_blank()) + # Format the legend theme(legend.position = "none") + # Format title and axis labels theme(plot.title = element_text(color=color.text, size=20, face = "bold")) + theme(axis.title.x = element_text(size=14, color="black", face = "bold")) + theme(axis.title.y = element_text(size=14, color="black", face = "bold", vjust=1.25)) + theme(axis.text.x = element_text(size=10, vjust=0.5, hjust=0.5, color = color.text)) + theme(axis.text.y = element_text(size=10, color = color.text)) + theme(strip.text = element_text(face = "bold")) + # Plot margins theme(plot.margin = unit(c(0.35, 0.2, 0.3, 0.35), "cm")) }
我们将再次创建凹凸图,但这次我们将删除图例,添加一些图表标签,并使用上面代码中定义的主题:
ggplot(data, aes(x = as.factor(day), y = rank, group = team)) + geom_line(aes(color = team, alpha = 1), size = 2) + geom_point(aes(color = team, alpha = 1), size = 4) + geom_point(color = "#FFFFFF", size = 1) + scale_y_reverse(breaks = 1:nrow(data)) + scale_x_discrete(breaks = 1:10) + theme(legend.position = 'none') + geom_text(data = data %>% filter(day == "1"), aes(label = team, x = 0.5), hjust = .5, fontface = "bold", color = "#888888", size = 4) + geom_text(data = data %>% filter(day == "10"), aes(label = team, x = 10.5), hjust = 0.5, fontface = "bold", color = "#888888", size = 4) + labs(x = 'Day', y = 'Rank', title = 'Team Ranking by Day') + my_theme()
我们还可以通过添加scale_color_manual()参数轻松突出显示其中一行。例如,在下面的代码中,我们将 A 队的线设为紫色,将所有其他线设为灰色:
ggplot(data, aes(x = as.factor(day), y = rank, group = team)) +
geom_line(aes(color = team, alpha = 1), size = 2) +
geom_point(aes(color = team, alpha = 1), size = 4) +
geom_point(color = "#FFFFFF", size = 1) +
scale_y_reverse(breaks = 1:nrow(data)) +
scale_x_discrete(breaks = 1:10) +
theme(legend.position = 'none') +
geom_text(data = data %>% filter(day == "1"),
aes(label = team, x = 0.5), hjust = .5,
fontface = "bold", color = "#888888", size = 4) +
geom_text(data = data %>% filter(day == "10"),
aes(label = team, x = 10.5), hjust = 0.5,
fontface = "bold", color = "#888888", size = 4) +
labs(x = 'Day', y = 'Rank', title = 'Team Ranking by Day') +
my_theme() +
scale_color_manual(values = c('purple', 'grey', 'grey', 'grey', 'grey'))
如果需要,我们还可以突出显示多行:
ggplot(data, aes(x = as.factor(day), y = rank, group = team)) +
geom_line(aes(color = team, alpha = 1), size = 2) +
geom_point(aes(color = team, alpha = 1), size = 4) +
geom_point(color = "#FFFFFF", size = 1) +
scale_y_reverse(breaks = 1:nrow(data)) +
scale_x_discrete(breaks = 1:10) +
theme(legend.position = 'none') +
geom_text(data = data %>% filter(day == "1"),
aes(label = team, x = 0.5), hjust = .5,
fontface = "bold", color = "#888888", size = 4) +
geom_text(data = data %>% filter(day == "10"),
aes(label = team, x = 10.5), hjust = 0.5,
fontface = "bold", color = "#888888", size = 4) +
labs(x = 'Day', y = 'Rank', title = 'Team Ranking by Day') +
my_theme() +
scale_color_manual(values = c('purple', 'steelblue', 'grey', 'grey', 'grey'))