简体   繁体   English

如何使用 e_charts() 在同一图表中组合条形图和折线图?

[英]How can I combine bar and line viz in same chart using e_charts()?

I am trying to combine bar plot and line plot on same visualization while the data for each plot comes from different data set.我试图在同一可视化上结合条形图和线图,而每个图的数据来自不同的数据集。 So the code I am trying:所以我正在尝试的代码:

library(dplyr)
library(echarts4r)

set.seed(600)
df1 <- data.frame(
  #class = c(rep("1st", times = 60), rep("2nd", time = 30), rep("3rd", time = 30)),
  week = rep(1:20, times = 3),
  cat = c(rep("A", times = 20), rep("B", times = 20), rep("C", times = 20)),
  count = round(runif(60, 1, 100))
)

df <- data.frame(
  week = rep(1:20, times = 2),
  cat = c(rep("D", times = 20), rep("E", times = 20)),
  count = round(runif(40, 1, 100))
)

df1 %>%
      group_by(cat) %>%
      e_charts(week) %>%
      e_bar(count, bind = cat) %>%
      e_tooltip(
        formatter = htmlwidgets::JS("
        function(params){
        return('<strong>' + params.name + 
                '</strong><br />week: ' + params.value[0] + 
                '<br />count: ' + params.value[1]) 
                }
       ")
      ) 

Trying to add line considering the data df on the viz.考虑到df的数据df ,尝试添加行。 Below is what I am trying to achieve :以下是我想要实现的目标: 在此处输入图片说明

Here I have used echarts4rProxy() but is same thing possible outside Shiny?在这里,我使用了 echarts4rProxy() 但在 Shiny 之外是否也可能发生同样的事情?

Also is it possible to change the colors of bars and lines?是否可以更改条形和线条的颜色?

Thanks!!谢谢!!

Yes,是的,

To go about it the way you do with 2 different datasets you can use e_data pass new data, it's just like e_charts but within the echarts4r pipe.要按照处理 2 个不同数据集的方式进行处理,您可以使用e_data传递新数据,它就像e_charts一样,但在 echarts4r 管道内。

library(dplyr)
library(echarts4r)

set.seed(600)
df1 <- data.frame(
  #class = c(rep("1st", times = 60), rep("2nd", time = 30), rep("3rd", time = 30)),
  week = rep(1:20, times = 3),
  cat = c(rep("A", times = 20), rep("B", times = 20), rep("C", times = 20)),
  count = round(runif(60, 1, 100))
)

df <- data.frame(
  week = rep(1:20, times = 2),
  cat = c(rep("D", times = 20), rep("E", times = 20)),
  count = round(runif(40, 1, 100))
)

df1 %>%
  group_by(cat) %>%
  e_charts(week) %>%
  e_bar(count, bind = cat) %>%
  e_data(data = group_by(df, cat), x = week) %>% 
  e_line(count) %>% 
  e_tooltip(
    formatter = htmlwidgets::JS("
    function(params){
    return('<strong>' + params.name + 
            '</strong><br />week: ' + params.value[0] + 
            '<br />count: ' + params.value[1]) 
            }
    ")
  ) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM