使用ECharts4R在单个图表上创建多个网格

时间:2019-11-08 04:32:20

标签: r echarts

我正在尝试创建一个包含四个小图表的图表,如以下链接所示:

https://www.echartsjs.com/examples/en/editor.html?c=scatter-anscombe-quartet

以下是尝试创建图表的脚本:

df <- data.frame(
  x = 1:20, 
  testing123 = runif(20, 10, 100),
  v = runif(20, 15, 100),
  w = runif(20, 1, 100),
  z = runif(20, 25, 75)
)
df %>% 
  e_charts(x) %>% 
  e_y_axis(gridIndex = 0, min=0,max=100) %>% 
  e_y_axis(gridIndex = 1, min=0,max=100) %>% 
  e_y_axis(gridIndex = 2, min=0,max=100) %>%
  e_y_axis(gridIndex = 3, min=0,max=100) %>% 
  e_x_axis(gridIndex = 0, min=0,max=20) %>%
  e_x_axis(gridIndex = 1, min=0,max=20) %>%
  e_x_axis(gridIndex = 2, min=0,max=20) %>%
  e_x_axis(gridIndex = 3, min=0,max=20) %>% 
  e_grid(x= '7%',y='7%',width='38%',height='38%') %>%
  e_grid(x2= '7%',y='7%',width='38%',height='38%') %>%
  e_grid(x= '7%',y2='7%',width='38%',height='38%') %>%
  e_grid(x2= '7%',y2='7%',width='38%',height='38%') %>%
  e_line(serie = w, x_index = 0, y_index = 0) %>%
  e_line(serie = z, x_index = 1, y_index = 1) %>%
  e_line(serie = v, x_index = 2, y_index = 2) %>%
  e_line(serie = testing123, x_index = 3, y_index = 3) %>%
  e_tooltip(trigger = "axis") 

但是输出不符合预期:

enter image description here

三行“压缩”到一个窗格中,我希望每个窗格中只有一行。

有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:0)

从技术上讲,您可以使用echarts4r复制此代码,但由于代码过于混乱,因此不建议这样做。

有一种比摆弄e_grid更方便的方法。下面是一个示例,不是我使用相同的name来连接工具提示和图例。


library(echarts4r)

df <- data.frame(
  x = 1:20, 
  testing123 = runif(20, 10, 100),
  v = runif(20, 15, 100),
  w = runif(20, 1, 100),
  z = runif(20, 25, 75)
)

# remember to specify the ids
p1 <- e_charts(df, x, elementId = "chart1") %>% 
  e_line(serie = w, name = "common") %>% 
  e_tooltip(trigger = "axis")

p2 <- e_charts(df, x, elementId = "chart2") %>% 
  e_line(serie = v, name = "common") %>% 
  e_tooltip(trigger = "axis")

p3 <- e_charts(df, x, elementId = "chart3") %>% 
  e_line(serie = z, name = "common") %>% 
  e_tooltip(trigger = "axis")

p4 <- e_charts(df, x) %>% 
  e_line(serie = testing123, name = "common") %>% 
  e_tooltip(trigger = "axis") %>% 
  e_connect(c("chart1", "chart2", "chart3")) # connect the last chart to all others

# this is a convenience function to display the charts in a grid
e_arrange(p1, p2, p3, p4, rows = 2, cols = 2)

website

上有进一步的解释