echarts4r创建数据透视图(子组x轴)

8xiog9wr  于 2022-12-20  发布在  Echarts
关注(0)|答案(1)|浏览(310)

我想在echarts4r中重新创建一个有两个子组x轴的数据透视图,数据透视图是指有两个子组x轴标签的图表,如下所示。

到目前为止,我发现this的答案是echart,但我无法将其翻译为echarts4r,这可能吗?

数据

dd <- data.frame(
  market = rep(c("Left", "Right"), each = 6),
  gender = rep(rep(c("Female", "Male"), each = 3), 2),
  group = c("Top", "Middle", "Bottom"),
  value = c(10, 2, 4, 5, 1, 7, 7, 4, 1, 2, 5, 6)
)
library(echarts4r)
dd |> 
  group_by(gender) |> 
  e_charts(market) |> 
  e_bar(value)

这将生成下面的图表,该图表几乎在那里,除了第二个x轴(即组信息)丢失。

埃查茨溶液

不是直接解决我的问题,但玩了一下echarts,我发现这个调整是有效的,但这并不能转化为echarts4r。

option = {
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'cross',
      crossStyle: {
        color: '#999'
      }
    }
  },
  toolbox: {
    feature: {
      magicType: { show: true, type: ['line', 'bar'] },
      restore: { show: true },
      saveAsImage: { show: true }
    }
  },
  legend: {
    data: ['Female', 'Male']
  },
  xAxis: [
    {
      type: 'category',
      data: ["Bottom", "Middle", "Top", "Bottom", "Middle", "Top"],
      axisPointer: {
        type: 'shadow'
      }
    },
    {
      type: "category",
      axisTick: { show: true,
      alignWithLabel: false,
        length: 40,
        align: "left",
        interval: function(index, value) {
          return value ? true : false;
        }},
      offset: -700,
      axisLine: {
        show: false
      },
      data: ["Left", "Right"]
    }
  ],
  yAxis: [
    {
      type: 'value',
      name: 'Value',
      min: 0,
      max: 12,
      interval: 2,
      axisLabel: {
        formatter: function(value) {
          return value;
        }
      }
    }
  ],
  series: [
    {
      name: 'Male',
      type: 'bar',
      tooltip: {
        valueFormatter: function (value) {
          return value;
        }
      },
      data: [
        7, 1, 5, 6, 5, 2
      ]
    },
    {
      name: 'Female',
      type: 'bar',
      tooltip: {
        valueFormatter: function (value) {
          return value;
        }
      },
      data: [
        4, 2, 10, 1, 4, 7
      ]
    }
  ]
};

(See代码也为this

wrrgggsh

wrrgggsh1#

JSON版本的图表可以很容易地转换成R。我没有添加所有的内容(工具提示等)。但是,一旦您了解了如何设置它,您就可以根据自己的需要对其进行定制。
首先,创建绘图,将任何[]或{}替换为list()
您将使用比echarts4r更多的echart命名约定(即xAxise_x_axis)。

ops <- list(
  xAxis = list(
    list(type = "category", position = "bottom", axisLine = list(show = F),
         data = as.list(rep(c("Top", "Middle", "Bottom"), 2))),
    list(type = "category", position = "bottom",
         axisTick = list(
           show = T, alignWithLabel = F, length = 40, align = "left",
           interval = 2),           # interval based on 0 start
         axisLine = list(show = F), axisLabel = list(margin = 30),
         splitLine = list(
           show = T, interval = 2), # interval based on 0 start
         data = list("", "Left", "", "", "Right", ""))),
  yAxis = list(type = "value", name = "Value", min = 0, max = 12, interval = 2),
  series = list(
    list(name = "Male", type = "bar",
         data = dd[dd$gender %in% "Male", ]$value),
    list(name = "Female", type = "bar",
         data = dd[dd$gender %in% "Female", ]$value)
  ),
  legend = list(data = list("Female", "Male"))
)

现在,创建打印:

e_charts() |> e_list(ops)

相关问题