前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言画连线排序图

R语言画连线排序图

作者头像
医学和生信笔记
发布2022-11-15 10:02:04
8180
发布2022-11-15 10:02:04
举报
文章被收录于专栏:医学和生信笔记

今天学习一个专门画连线图的R包:ggbump。可以用来可视化随时间变化的数据,或者两个点之间的连接,或者不同名次的比较等。

安装

代码语言:javascript
复制
# 2选1
install.packages("ggbump")

devtools::install_github("davidsjoberg/ggbump")

使用

首先构建一个数据集。

代码语言:javascript
复制
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()而已:

代码语言:javascript
复制
ggplot(df, aes(year, rank, color = country)) +
    geom_bump()

image-20220602190131251

如果你使用geom_line()函数,出来的图形是这样的:

代码语言:javascript
复制
ggplot(df, aes(year,rank,color=country))+
  geom_line()

image-20220602190153571

所以你可以认为这是折线图的一种变体图形。

美化

当然也是可以进行各种美化的:

代码语言:javascript
复制
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"))

image-20220602190206806

这个包需要的数据结构需要含有一列类似排名的东西,才能正确画出这种图。下面是一个奥运会奖牌榜的例子。

奥运会奖牌榜

首先查看一下数据结构。

代码语言:javascript
复制
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,~

然后是画图:

代码语言:javascript
复制
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)

image-20220602190322724

大家以后有这种类似的数据和需求,可以用这个包展示哦!

以上就是今天的内容,希望对你有帮助哦!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-07-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 医学和生信笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装
  • 使用
  • 美化
  • 奥运会奖牌榜
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档