echarts4r添加多个Xaxis标签,具有重复的值

发布于 2025-01-22 16:19:52 字数 1733 浏览 3 评论 0原文

我想使用echarts4r绘制线路图,Xaxis的标签需要具有重复的值。 我对Echarts4r的包装为0.3.2。

我的代码如下:

library(echarts4r)
library(magrittr)
library(data.table)

dt <- data.table(
  x_label = c('A', 'A', 'A', 'D', 'D', 'M', 'A'),
  x_value = c('a', 'b', 'c', 'd', 'e', 'f', 'g'),
  y = 8:14
)

p <- e_chart(dt, x_value)
p <- e_x_axis(p, show = FALSE, index = 0, position = 'top', data = purrr::map(seq_len(nrow(dt)), ~{
  list(value = dt[.x, x_value])
}))   # The show argument FALSE can not be worked 

p <- e_line_(p, 'y', x_index = 0)

p <- e_x_axis(p, show = TRUE, position = 'bottom', data = purrr::map(seq_len(nrow(dt)), ~{
  list(value = dt[.x, x_label])
}), index = 1)
p <- e_line_(p, 'y', x_index = 1)
p

但是顶部的Xaxis标签不能隐藏,我不知道为什么。 是否有人告诉我如何在具有重复值的线图中自定义Xaxis的标签,

或者我不知道如何将此选项转换为R代码:

option = {
  title: {
    text: 'ECharts entry example'
  },
  tooltip: {},
  legend: {
    data:['Sales']
  },
  xAxis: [
    {
        position: "bottom",
        data: ["shirt","cardign","chiffon shirt","pants","heels","socks"]
    },
    {
        position: "bottom",
        data: ["group1", "", "", "group2", "", ""],
      interval: 1,
      axisLine: {
        show: false
      },
      axisTick: {
        alignWithLabel: false,
        length: 40,
        align: "left",
        interval: function(index, value) {
          return value ? true : false;
        }
      },
      axisLabel: {
        margin: 30
      },
      splitLine: {
        show: true,
        interval: function(index, value) {
          return value ? true : false;
        }
      }
    }
  ],
  yAxis: {},
  series: [{
    name: 'Sales',
    type: 'bar',
    data: [5, 20, 36, 10, 10, 20]
  }]
};

I want to plot a line chart using echarts4r, and the xAxis's label need to have the duplicated values.
My packageVersion of echarts4r is 0.3.2.

And my codes as the follows:

library(echarts4r)
library(magrittr)
library(data.table)

dt <- data.table(
  x_label = c('A', 'A', 'A', 'D', 'D', 'M', 'A'),
  x_value = c('a', 'b', 'c', 'd', 'e', 'f', 'g'),
  y = 8:14
)

p <- e_chart(dt, x_value)
p <- e_x_axis(p, show = FALSE, index = 0, position = 'top', data = purrr::map(seq_len(nrow(dt)), ~{
  list(value = dt[.x, x_value])
}))   # The show argument FALSE can not be worked 

p <- e_line_(p, 'y', x_index = 0)

p <- e_x_axis(p, show = TRUE, position = 'bottom', data = purrr::map(seq_len(nrow(dt)), ~{
  list(value = dt[.x, x_label])
}), index = 1)
p <- e_line_(p, 'y', x_index = 1)
p

But the top xAxis label can not be hide, I don't know why.
Does anybody tell me how to custom the xAxis's label in a line chart with duplicated values ?

Or I don't know how to convert this options to R code:

option = {
  title: {
    text: 'ECharts entry example'
  },
  tooltip: {},
  legend: {
    data:['Sales']
  },
  xAxis: [
    {
        position: "bottom",
        data: ["shirt","cardign","chiffon shirt","pants","heels","socks"]
    },
    {
        position: "bottom",
        data: ["group1", "", "", "group2", "", ""],
      interval: 1,
      axisLine: {
        show: false
      },
      axisTick: {
        alignWithLabel: false,
        length: 40,
        align: "left",
        interval: function(index, value) {
          return value ? true : false;
        }
      },
      axisLabel: {
        margin: 30
      },
      splitLine: {
        show: true,
        interval: function(index, value) {
          return value ? true : false;
        }
      }
    }
  ],
  yAxis: {},
  series: [{
    name: 'Sales',
    type: 'bar',
    data: [5, 20, 36, 10, 10, 20]
  }]
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年华零落成诗 2025-01-29 16:19:52

我只是找到了您的问题,您可能不再需要这个答案,但是也许其他人会从中受益。

您正在寻找来自echarts4r的函数e_list()。此方法对您进行而不是排序或汇总。但是,您可以使用R或JS功能。

在代码中,我仅按x_label进行排序,因为所有x_value都是唯一的。

library(echarts4r)
library(data.table)

dt <- data.table(
  x_label = c('A', 'A', 'A', 'D', 'D', 'M', 'A'),
  x_value = c('a', 'b', 'c', 'd', 'e', 'f', 'g'),
  y = 8:14
)

opts <- list(
  xAxis = list(
    list(data = as.list(dt[order(x_label)][, x_value]), position = "bottom"),
    list(data = list("A", "", "", "", "D", "", "M"), position = "bottom",
         axisLine = list(show = F), axisLabel = list(margin = 30),
         axisTick = list(
           show = T, alignWithLabel = F, length = 40, align = "left",
           interval = htmlwidgets::JS(
             "function(index, value) {return value ? true : false;}")),
         splitLine = list(show = T,
           interval = htmlwidgets::JS(
             "function(index, value) {return value ? true : false;}")
         ))),
  yAxis = list(type = "value"),
  series = list(list(type = "line", name = "Y", # data has to be sorted
                     data = as.list(dt[order(x_label)][, y])))
)

e_charts() %>% e_list(opts)

如果您想查看在R代码中提供的JS绘图,则只有几个步骤可以采取。

  • 不要将列表列表列表option(这是r中的关键字)
  • 替换JS函数,替换为htmlwidgets :: js()
  • 在htmlwidgets之外的所有内容中的htmlwidgets :: js()ex.js :: js()
  • : semi-colons
  • 替换:with = =
  • 替换{},[],(),list()
  • 替换为true,false用t,f(或true,false),

这是代码。我保留了与您提供的JSON相同的间距。
对于两个轴,我都添加了两个值得注意的版本。 (您会在代码中看到有关JSON更改的注释。)

opts2 = list(
  title = list(
    text = 'ECharts entry example'
  ),
  tooltip = list(),
  legend = list(
    data = list('Sales')
  ),
  xAxis = list(
    list(
      position = "bottom", type = "category",  # <--- added this (and fixed spelling of cardigan)
      data = list("shirt","cardigan","chiffon shirt","pants","heels","socks"),
    ),
    list(
      position = "bottom", 
      data = list("group1", "", "", "group2", "", ""),
      interval = 1,
      axisLine = list(
        show = F
      ),
      axisTick = list(
        alignWithLabel = F,
        length = 40,
        align = "left",
        interval = htmlwidgets::JS("function(index, value) {return value ? true : false;}")
      ),
      axisLabel = list(
        margin = 30
      ),
      splitLine = list(
        show = T,
        interval = htmlwidgets::JS("function(index, value) {return value ? true : false;}")
      )
    )
  ),
  yAxis = list(type = "value"),    # <--- like the xaxis, type added here
  series = list(list(
    name = 'Sales',
    type = 'bar',
    data = list(5, 20, 36, 10, 10, 20)
  ))
)

e_charts() |> e_list(opts2)

“在此处输入图像描述”

请记住,如果您的视图(即,rstudio viewer pane,浏览器等)太窄,则图中的标签将重叠,一些标签被隐藏。如有疑问,请更改视图的大小。

这是您可能看到的示例,如果视图太窄。

I just found your question, you may no longer need this answer, but perhaps someone else will benefit from it.

You were looking for the function e_list() from echarts4r. This method does not sort or aggregate for you. However, you can use R or JS functions.

In the code, I've only sorted by x_label since all of x_value is unique.

library(echarts4r)
library(data.table)

dt <- data.table(
  x_label = c('A', 'A', 'A', 'D', 'D', 'M', 'A'),
  x_value = c('a', 'b', 'c', 'd', 'e', 'f', 'g'),
  y = 8:14
)

opts <- list(
  xAxis = list(
    list(data = as.list(dt[order(x_label)][, x_value]), position = "bottom"),
    list(data = list("A", "", "", "", "D", "", "M"), position = "bottom",
         axisLine = list(show = F), axisLabel = list(margin = 30),
         axisTick = list(
           show = T, alignWithLabel = F, length = 40, align = "left",
           interval = htmlwidgets::JS(
             "function(index, value) {return value ? true : false;}")),
         splitLine = list(show = T,
           interval = htmlwidgets::JS(
             "function(index, value) {return value ? true : false;}")
         ))),
  yAxis = list(type = "value"),
  series = list(list(type = "line", name = "Y", # data has to be sorted
                     data = as.list(dt[order(x_label)][, y])))
)

e_charts() %>% e_list(opts)

enter image description here

If you want to see the JS plot you provided in R code, there are only a few steps to take.

  • don't name the list of lists option (that's a keyword in R)
  • replace JS functions with values or encapsulated in htmlwidgets::JS()
  • for everything outside of htmlwidgets::JS():
  • remove any semi-colons
  • replace : with =
  • replace {}, [], (), with list()
  • replace true, false with T, F (or TRUE, FALSE)

Here's the code. I kept the same spacing as in the JSON you provided.
There are two notable editions to make this work, for both axes, I added the type. (You'll see comments in the code about the changes from the JSON.)

opts2 = list(
  title = list(
    text = 'ECharts entry example'
  ),
  tooltip = list(),
  legend = list(
    data = list('Sales')
  ),
  xAxis = list(
    list(
      position = "bottom", type = "category",  # <--- added this (and fixed spelling of cardigan)
      data = list("shirt","cardigan","chiffon shirt","pants","heels","socks"),
    ),
    list(
      position = "bottom", 
      data = list("group1", "", "", "group2", "", ""),
      interval = 1,
      axisLine = list(
        show = F
      ),
      axisTick = list(
        alignWithLabel = F,
        length = 40,
        align = "left",
        interval = htmlwidgets::JS("function(index, value) {return value ? true : false;}")
      ),
      axisLabel = list(
        margin = 30
      ),
      splitLine = list(
        show = T,
        interval = htmlwidgets::JS("function(index, value) {return value ? true : false;}")
      )
    )
  ),
  yAxis = list(type = "value"),    # <--- like the xaxis, type added here
  series = list(list(
    name = 'Sales',
    type = 'bar',
    data = list(5, 20, 36, 10, 10, 20)
  ))
)

e_charts() |> e_list(opts2)

enter image description here

Keep in mind that if your view (i.e., RStudio viewer pane, browser, etc.) is too narrow, in which the labels in the plot would overlap, some of the labels are hidden. When in doubt, change the size of your view.

This is an example of what you might see if the view is too narrow.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文