R画图技巧

echarts4r画漂亮甜甜圈图

2022-04-16  本文已影响0人  小熊_wh

原文来自:https://zhuanlan.zhihu.com/p/476603927

1. 模拟数据

df <- data.frame(  
  event = c("A3SS","A5SS","SE","RI","MXE"),  
  count = c(10, 24, 40, 17, 9))

2. 加载包

if(!require("echarts4r")) BiocManager::install("echarts4r",update = F,ask = F)
library(echarts4r) 
 library(dplyr)

3. 画图

df %>%
  e_chart(event) %>%
  e_pie(count, radius = c("30%", "60%"),
        timeline = T,legend =F,name="Event and Percent") %>%
  e_labels( 
    position = "outside", 
    labelLine = list(length = 30), 
    formatter = "{a|{a}}{abg|}\n{hr|}\n  {b|{b}:}{c}  {per|{d}%}",
    backgroundColor = "#F6F8FC",
    borderColor = "#8C8D8E", 
    borderWidth = 1, 
    borderRadius = 4,
    rich = list( 
      a = list(  
        color = "#6E7079",
        lineHeight = 22,  
        align = "center" 
      ),  
      hr = list( 
        borderColor = "#8C8D8E", 
        width = "100%", 
        borderWidth = 1,  
        height = 0    
      ), 
      b = list( 
        color = "#4C5058",
        fontSize = 14,   
        fontWeight = "bold", 
        lineHeight = 33  
      ),   
      per = list(     
        color = "#fff",  
        backgroundColor = "#4C5058", 
        padding = c(3, 4),   
        borderRadius = 4)))%>% 
  e_toolbox_feature(feature = c("saveAsImage")) #在Plots右上角添加了个下载png按钮
image.png

4. 函数详解

e_pie(e,serie,name=NULL,legend=TRUE,rm_x=TRUE,rm_y=TRUE,...)

其中e为数据集;serie为绘图的数据类别;name 为标签的名字,在添加文本外框时有用;legend 是否保留图例;rm_x 与rm_y是否去除;radius设置甜甜圈图的内圈大小和整个甜甜圈图的大小。

上一篇下一篇

猜你喜欢

热点阅读