用R中echarts4r包绘制饼图的笔记

2021-09-28 · 816 words · 2 minute read R

这篇笔记使用的R版本为4.0.3,echarts4r包的版本为3.3,对应echarts版本为4.9

首先,编造一份数据作图用:

library(echarts4r)

data <- data.frame(
  level = c('A', 'B', 'C', 'D', 'E'),
  num1 = sample(1:20, 5, replace = TRUE),
  num2 = sample(10:30, 5, replace = TRUE)
)

1.基本形式

data %>%
  e_charts(level) %>%
  e_pie(num1)  # 普通饼图

data %>%
  e_charts(level) %>%
  e_pie(num1, radius = c("50%", "70%")) # 环形饼图

data %>%
  e_charts(level) %>%
  e_pie(
    num1,
    radius = c("50%", "70%"),
    itemStyle = list(
      borderRadius = 20,
      borderColor = '#fff',
      borderWidth = 2 # 新版本该设置下画出来的是圆角环形图
    )
  )
  
data %>%
  e_charts(level) %>%
  e_pie(num1, roseType = "radius") # 玫瑰图

2.标签位置

以下饼图中标签设置的内容是笔者在echarts官网翻出来的,有一些别的标签设置现在的echarts4r包还没有实现。

data %>%
  e_charts(level) %>%
  e_pie(num1) %>%
  e_labels(alignTo = "edge")

data %>%
  e_charts(level) %>%
  e_pie(num1) %>%
  e_labels(alignTo = "labelLine")

data %>%
  e_charts(level) %>%
  e_pie(num1) %>%
  e_labels(alignTo = "none",
           formatter = "名称:{b} \n 值:{c} ")

3.多个饼图

data %>%
  e_charts(level) %>%
  e_pie(num1, radius = "30%", center = c("30%", "30%")) %>%
  e_pie(num1, radius = "30%", center = c("70%", "30%")) %>%
  e_pie(num1, radius = "30%", center = c("30%", "70%")) %>%
  e_pie(num1, radius = "20%", center = c("70%", "70%")) 
  # radus是设置饼的半径,也就是饼的大小
  # center设置的是每个饼的相对位置,需根据饼的数量做相应修改

4.旭日图(sunburst)

旭日图麻烦的不是画图,而是用来画图的数据不方便批量导入。有个Sunburst包专门用来画旭日图,支持导入普通形式的数据框。

library(tibble)

data.2 <- tibble(
  name = c("1", "2", "3", "4"),
  value = c(449136, 422723, 140335, 693150),
  children = list(
    tibble(
      name = c("1-1"),
      value = c(449136),
      children = list(tibble(
        name = c("1-1-1", "1-1-2"),
        value = c(207773, 241363)
      ))
    ),
    tibble(
      name = c("2-1", "2-2"),
      value = c(299793, 122930),
      children = list(tibble(
        name = c("2-1-1", "2-1-2"),
        value = c(84785, 215008)
      ), NULL)
    ),
    tibble(
      name = c("3-1", "3-2", "3-3"),
      value = c(64022,  55169, 21144)
    ),
    tibble(
      name = c('4-1',
               '4-2',
               '4-3',
               '4-4'),
      value = c(295202,
                117420,
                96258,
                184270),
      children = list(
        tibble(
          name = c("4-1-1", "4-1-2"),
          value = c(106900, 188302),
          children = list(tibble(
            name = c("4-1-1-1", "4-1-1-2"),
            value = c(56900, 50000)
          ), NULL)
        ),
        NULL,
        tibble(
          name = c("4-3-1", "4-3-2"),
          value = c(56258, 40000)
        ),
        NULL
      )
    )
  )
)

#普通旭日图
data.2 %>%
  e_charts() %>%
  e_sunburst() %>%
  e_labels(fontSize = 9,
           position = "inside",
           show = TRUE) %>%
  e_tooltip()

#圆角旭日图
data.2 %>%
  e_charts() %>%
  e_sunburst(radius = c(60, "90%")) %>%
  e_labels(fontSize = 9,
           position = "inside",
           show = TRUE)

5.树图(tree)

突然发现前面造的数据可以拆一部分出来画个树图,于是乎……

data.2[4, ] %>%
  e_charts() %>%
  e_tree(initialTreeDepth = -1) %>%  #默认从根节点到叶节点是从左至右
  e_labels(fontSize = 9) 

data.2[4, ] %>%
  e_charts() %>%
  e_tree(initialTreeDepth = -1, orient = "RL") %>%  #RL:从右至左;vertical:从上到下;BT:从下到上
  e_labels(fontSize = 9)

data.2[4, ] %>%
  e_charts() %>%
  e_tree(initialTreeDepth = -1, edgeShape = "polyline") %>%  #折线树图
  e_labels(fontSize = 9) 

data.2[4,] %>%
  e_charts() %>%
  e_tree(initialTreeDepth = -1, layout = "radial") %>%  #径向树图
  e_labels(fontSize = 9) 

今天晚上的风吹着好舒服呀,虽然已经秋分,但有种夏夜晚风既视感。