前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言圆角柱形图—ggchicklet

R语言圆角柱形图—ggchicklet

作者头像
用户7010445
发布2020-03-03 15:04:22
1.1K0
发布2020-03-03 15:04:22
举报

简单理解内容就是 R语言柱形图,但特别的是柱形图是圆角的,通常ggplot2实现的柱形图都是直角的;另外一点是柱形图的配色个人觉得很好看,自己保存下来备用。

实现圆角柱形图的是R包 ggchicklet

chicklet自己查了一下是少妇姑娘的意思,为什么会叫这个名字还有点好奇,应该是这个单词还有一个比较生僻的用法吧?

重复开头提到的文章的过程中还学到了新的R包hrbrthemes,粗略看了一下帮助文档,基本功能好像是为ggplot2补充一些主题,主要侧重在字体方面,自己试的时候遇到了一堆警告

> warnings()
警告信息:
1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
2: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
3: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
4: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
5: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
6: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
7: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
8: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
9: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
10: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
11: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
12: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
13: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
14: In grid.Call.graphics(C_text, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
15: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
16: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列
17: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  Windows字体数据库里没有这样的字体系列

如何给自己的电脑配置字体自己现在还不太清楚

接下来简单记录ggchicklet的用法

ggplot2的简单柱形图

library(ggplot2)
library(RColorBrewer)
x<-LETTERS[1:14]
y<-1:14
df<-data.frame(x,y)
df
ggplot(df,aes(x,y))+
  geom_col(aes(fill=x))+
  scale_fill_manual(name="",
                    values =c(brewer.pal(4,"Paired"),
                              brewer.pal(10,"Paired")))+
  theme_bw()+labs(x="",y="")

image.png

实现圆角柱形图只需要将geom_col()函数换成geom_chicklet()函数就可以了

library(ggplot2)
library(RColorBrewer)
install.packages("ggchicklet", repos = "https://cinc.rud.is")
library(ggchicklet)
x<-LETTERS[1:14]
y<-1:14
df<-data.frame(x,y)
df

ggplot(df,aes(x,y))+
  geom_chicklet(aes(fill=x))+
  scale_fill_manual(name="",
                    values =c(brewer.pal(4,"Paired"),
                              brewer.pal(10,"Paired")))+
  theme_bw()+labs(x="",y="")

image.png

模仿开头提到的文章里的堆积柱形图

df<-data.frame(Year=c(rep("2019",12),rep("2018",12),
                      rep("2017",12),rep("2017",12),
                      rep("2016",12),rep("2015",12)),
               Month=c(rep(1:12,6)),
               n=sample(1:15,12*6,replace = T))
dim(df)
head(df)
library(ggplot2)
library(RColorBrewer)
library(ggchicklet)
ggplot(df,aes(x = factor(Year), y = n)) + 
  geom_chicklet(aes(fill = factor(Month)),
                width = 0.25, 
                radius = grid::unit(3, "pt")) + 
  theme_bw()+
  scale_fill_brewer(name = "Month",
                    palette = "Paired",
                    breaks = 1:12,
                    labels = month.name)+
  labs(x="",y="")

image.png

这里比较神奇的是数字月份自动被替换成了英文,暂时还不知道如何实现的。

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

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 接下来简单记录ggchicklet的用法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档