echarts4r添加多个Xaxis标签,具有重复的值
我想使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是找到了您的问题,您可能不再需要这个答案,但是也许其他人会从中受益。
您正在寻找来自
echarts4r
的函数e_list()
。此方法对您进行而不是排序或汇总。但是,您可以使用R或JS功能。在代码中,我仅按
x_label
进行排序,因为所有x_value
都是唯一的。如果您想查看在R代码中提供的JS绘图,则只有几个步骤可以采取。
option
(这是r中的关键字)这是代码。我保留了与您提供的JSON相同的间距。
对于两个轴,我都添加了两个值得注意的版本。 (您会在代码中看到有关JSON更改的注释。)
请记住,如果您的视图(即,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()
fromecharts4r
. 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 ofx_value
is unique.If you want to see the JS plot you provided in R code, there are only a few steps to take.
option
(that's a keyword in R)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.)
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.