绘图导航
  • 基础绘图
  • 高级绘图
  • 微生物
  • 转录组

R语言|绘制Bump Chart

 二维码 1

R语言绘制Bump Chart的小示例


# install.packages("tidyverse")

# install.packages("ggbump")

library(tidyverse)

library(ggbump)

#创建数据集:各类商品2018-2021年的销量排名

year <- rep(2018:2021, 7)

position <- c(2,6,5,1,1,5,1,3,5,3,2,5,7,2,4,2,4,1,3,4,6,7,7,7,3,4,6,6)

goods <- c("电话", "电话", "电话","电话",

            "配件", "配件", "配件", "配件",

            "设备", "设备", "设备","设备",

            "复印机", "复印机", "复印机","复印机",

            "椅子", "椅子", "椅子","椅子",

            "桌子", "桌子", "桌子","桌子",

            "文具", "文具", "文具","文具")

df <- data.frame(x = year,

                 y = position,

                 group = goods)


#绘图

ggplot(df, aes(x = x, y = y, color = group)) +

  geom_bump(size = 1.5) +

  geom_point(size = 6) #添加点

  scale_color_brewer(palette = "Set2") #颜色



#添加图例文字

ggplot(df, aes(x = x, y = y, color = group)) +

    geom_bump(size = 1.5) +

    geom_point(size = 6) +

    geom_text(data = df %>% filter(x == min(x)),

              aes(x = x - 0.1, label = group),

              size = 5, hjust = 1) +

    geom_text(data = df %>% filter(x == max(x)),

              aes(x = x + 0.1, label = group),

              size = 5, hjust = 0) +

    scale_color_brewer(palette = "Set2") +

    theme_void()

    ###theme(legend.position = "none") 去掉右侧图例




“作图帮”微信公众号同步更新

作图帮二维码.jpg


可添加下方微信进入【生信作图交流群】,群内免费分享绘图代码与示例数据

公司二维码.png



推荐使用谷歌浏览器点击下载