其他
R语言画连线排序图
今天学习一个专门画连线图的R包:ggbump
。可以用来可视化随时间变化的数据,或者两个点之间的连接,或者不同名次的比较等。
安装
# 2选1
install.packages("ggbump")
devtools::install_github("davidsjoberg/ggbump")
使用
首先构建一个数据集。
suppressPackageStartupMessages(library(tidyverse))
library(ggbump)
df <- tibble(country = c("India", "India", "India", "Sweden", "Sweden", "Sweden", "Germany", "Germany", "Germany", "Finland", "Finland", "Finland"),
year = c(2011, 2012, 2013, 2011, 2012, 2013, 2011, 2012, 2013, 2011, 2012, 2013),
value = c(492, 246, 246, 369, 123, 492, 246, 369, 123, 123, 492, 369))
df <- df %>%
group_by(year) %>%
mutate(rank = rank(value, ties.method = "random")) %>%
ungroup()
head(df)
## # A tibble: 6 x 4
## country year value rank
## <chr> <dbl> <dbl> <int>
## 1 India 2011 492 4
## 2 India 2012 246 2
## 3 India 2013 246 2
## 4 Sweden 2011 369 3
## 5 Sweden 2012 123 1
## 6 Sweden 2013 492 4
画一个连线图很简单,就是一个geom_bump()
而已:
ggplot(df, aes(year, rank, color = country)) +
geom_bump()
如果你使用geom_line()
函数,出来的图形是这样的:
ggplot(df, aes(year,rank,color=country))+
geom_line()
所以你可以认为这是折线图的一种变体图形。
美化
当然也是可以进行各种美化的:
library(wesanderson)
ggplot(df, aes(year, rank, color = country)) +
geom_point(size = 7) +
geom_text(data = df %>% filter(year == min(year)),
aes(x = year - .1, label = country), size = 5, hjust = 1) +
geom_text(data = df %>% filter(year == max(year)),
aes(x = year + .1, label = country), size = 5, hjust = 0) +
geom_bump(size = 2, smooth = 8) +
scale_x_continuous(limits = c(2010.6, 2013.4),
breaks = seq(2011, 2013, 1)) +
theme_minimal(base_size = 14) +
theme(legend.position = "none",
panel.grid.major = element_blank()) +
labs(y = "RANK",
x = NULL) +
scale_y_reverse() +
scale_color_manual(values = wes_palette(n = 4, name = "GrandBudapest1"))
这个包需要的数据结构需要含有一列类似排名的东西,才能正确画出这种图。下面是一个奥运会奖牌榜的例子。
奥运会奖牌榜
首先查看一下数据结构。
load(file = "df4.Rdata")
glimpse(df4)
## Rows: 70
## Columns: 6
## $ year <dbl> 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1~
## $ team <chr> "Australia", "China", "France", "Germany", "Great Brita~
## $ n_medal <int> 57, 70, 55, 198, 50, 45, 47, 33, 220, 222, 130, 94, 49,~
## $ rank <int> 5, 4, 6, 3, 7, 9, 8, 10, 2, 1, 2, 5, 8, 3, 10, 7, 9, 6,~
## $ team_2_letters <chr> "au", "cn", "fr", "de", "gb", "it", "jp", "nl", "ru", "~
## $ host <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,~
然后是画图:
library(ggbump)
#devtools::install_github("baptiste/ggflags")
library(ggflags)
library(wesanderson)
df4 %>%
ggplot(aes(year, rank, group = team, color = team, fill = team)) +
geom_bump(smooth = 10, size = 1.5, lineend = "round") +
geom_label(data = df4 %>% filter(host == 1),
aes(label = "Host")) +
geom_text(data = df4 %>% filter(host == 1),
aes(label = "Host"),
color = "black") +
geom_flag(data = df4 %>% filter(year == min(year)),
aes(country = team_2_letters),
size = 10,
color = "black") +
geom_flag(data = df4 %>% filter(year == max(year)),
aes(country = team_2_letters),
size = 10,
color = "black") +
scale_color_manual(values = c(wesanderson::wes_palette("GrandBudapest2"), wesanderson::wes_palette("GrandBudapest1"), wesanderson::wes_palette("BottleRocket2"))) +
scale_fill_manual(values = c(wesanderson::wes_palette("GrandBudapest2"), wesanderson::wes_palette("GrandBudapest1"), wesanderson::wes_palette("BottleRocket2"))) +
scale_y_reverse(breaks = 1:100) +
scale_x_continuous(breaks = df4$year %>% unique()) +
theme_minimal(base_size = 16) +
theme(legend.position = "none",
panel.grid = element_blank(),
plot.background = element_rect(fill = "gray70"),
text = element_text(color = "white")) +
labs(x = NULL,
y = NULL)
大家以后有这种类似的数据和需求,可以用这个包展示哦!
以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发!
欢迎扫描二维码加 QQ群 :613637742
欢迎关注公众号:医学和生信笔记
往期回顾
一文搞懂临床预测模型的评价!
zotero笔记界面大改造
ggplot2版本的热图-方便拼图!
使用ggside添加边际图形
tableone?table1?傻傻分不清楚
列线图的本质
使用 aplot “拼”一个热图