前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ggplot2优雅的绘制圆点柱状图

ggplot2优雅的绘制圆点柱状图

作者头像
R语言数据分析指南
发布2022-09-21 15:10:16
4450
发布2022-09-21 15:10:16
举报
文章被收录于专栏:R语言数据分析指南

❝本节来介绍如何灵活使用「geom_segment」「geom_point」这两个几何对象来构建圆柱形条行图,下面通过1个案例来进行展示;

加载R包

代码语言:javascript
复制
library(tidyverse)

加载数据

代码语言:javascript
复制
  data <- read_tsv("data.xls")

数据可视化

代码语言:javascript
复制
  ggplot(data,aes(y = reorder(country, diff), x = diff, color=balance))+
    geom_segment(aes(yend = country), xend=0, size=5)+
    geom_point(size=4.5)+
    geom_vline(xintercept = 0, size=1, color='grey35')+
    expand_limits(x=c(-3,3.75))+
    scale_x_continuous(breaks = c(-2, -1, 1, 2))+
    # 构建数据分别在左右两边添加文本
    geom_text(data=data %>% filter(diff <0),aes(label=country), x = -3.2, hjust=0, size=3.5, color="black")+
    geom_text(data=data %>% filter(diff >0),aes(label=country), x =2.2, hjust=0, size=3.5, color="black")+
    # 两边分别添加阴影区域
    geom_segment(data=data %>% filter(diff <0),aes(yend = country), xend = -3.2, x=0, alpha = 0.2, size=5)+
    geom_segment(data=data %>% filter(diff >0),aes(yend = country), xend = 4.5, x=0,alpha = 0.2, size=5)+
    scale_color_manual(values=c("#BA7A70", "steelblue4"))+
    guides(color="none", y="none")+
    labs(x=NULL,y=NULL)+
    theme_minimal()+
    theme(plot.background = element_rect(fill = "Aliceblue", color="Aliceblue"),
          panel.grid = element_blank(),
          axis.line.x = element_line(color = "grey3", size = 0.5),
          panel.grid.major.x = element_line(linetype = "dashed", color = "grey3"),
          axis.text.x = element_text(size = 10, color = "black"))+
    # 绘制图例
    geom_rect(xmax =3.8,xmin = 2, ymin = 0, ymax = 3, fill = "aliceblue", color ="steelblue4")+
    geom_segment(y=1,yend=1, x=2.6, xend=3.6, size=5, alpha=0.5, color='steelblue4')+
    geom_point(y=1, x = 2.6, size = 4.5, color = "steelblue4")+
    geom_text(label = "More", y = 1 ,x = 2.1, hjust = 0, color = "steelblue4", size = 3.5)+
    geom_segment(y=2.3, yend=2.3, x=2.6, xend=3.6, size=5, alpha=0.5, color='#BA7A70')+
    geom_point(y=2.3,x = 2.6, size = 4.5, color = "#BA7A70")+
    geom_text(label = "Less", y =2.3, x = 2.1, hjust = 0, color = "#BA7A70", size = 3.5)
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-06-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 R语言数据分析指南 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 加载R包
  • 加载数据
  • 数据可视化
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档