【问题标题】:Interactive faceted rCharts bar graph filled with a third variable (replicate ggplot2 aes(x=var1, fill=var2) + geom_bar + facet_wrap)用第三个变量填充的交互式多面 rCharts 条形图(复制 ggplot2 aes(x=var1, fill=var2) + geom_bar + facet_wrap)
【发布时间】:2019-11-06 15:53:05
【问题描述】:

我想制作一个包含一个变量的交互式条形图,其中条形由第二个变量填充并由第三个变量分面。我想将它嵌入到一个 slidify 卡组中。我猜 rCharts 是正确的工具(或者另一个包会更好?)。

这是我的数据:

df <- data.frame(numbers = rep(1:3, 30),
                 letter = sample(c("A", "B", "C", "D"), 90, replace = TRUE),
                 status = sample(c("good", "bad", "ugly"), 90, replace = TRUE))

我可以在 ggplot2 中执行此操作,但无法使其具有交互性。我可以制作这张图(抱歉,无法嵌入):

ggplot(df, aes(letter, fill = status)) + geom_bar() + facet_wrap(.~numbers)

理想情况下,我可以选择和取消选择“状态”并查看条形图的变化。我在this page 上玩过条形图代码,但无法让它工作。这个功能可行吗?

【问题讨论】:

    标签: r ggplot2 bar-chart rcharts slidify


    【解决方案1】:

    我是一个重度 rCharts 用户,但现在,在我看来,plotly 是一个更好的支持选项(rCharts 不再在 CRAN 中)。

    Furthermoreplotly 有一种自动方式使ggplot2 图表具有交互性。以您为例:

    library(ggplot2)
    library(plotly)
    df <- data.frame(
      numbers = rep(1:3, 30),
      letter = sample(c("A", "B", "C", "D"), 90, replace = TRUE),
      status = sample(c("good", "bad", "ugly"), 90, replace = TRUE)
    )
    
    p <-
      ggplot(df, aes(letter, fill = status)) + geom_bar() + facet_wrap(. ~ numbers)
    
    ggplotly(p)
    

    编辑 1

    此修改是为了满足评论的需要。 您请求的更改无法直接从ggplot2 进行,因此您需要使用plotly。可能有比我的 for 循环更优雅的方式来绘制图表,但它们似乎有效:

    library(data.table)
    library(ggplot2)
    library(plotly)
    # make data.table
    df <- data.table(
      numbers = rep(1:3, 30),
      letter = sample(c("A", "B", "C", "D"), 90, replace = TRUE),
      status = sample(c("good", "bad", "ugly"), 90, replace = TRUE)
    )
    
    
    # keep unique status values for bars and numbers for facets
    traces <- df[,unique(status)]
    facets <- df[,unique(numbers)]
    # cast df
    casted_df <- dcast(df,letter + numbers ~ status )
    # initialise list to keep charts   
    facetplots <- list()
    for(i in 1:length(facets)){
      # initialise chart
      facetplots[[i]] <- plot_ly(type = "bar")
      facet_df <- casted_df[numbers == facets[i]]
      for(j in traces){
        if(i == 1){
          facetplots[[i]] <- add_trace(facetplots[[i]], x = facet_df[,letter], y = facet_df[,get(j)],legendgroup = j , color = j, name = j )
    
        }else{
          facetplots[[i]] <- add_trace(facetplots[[i]], x = facet_df[,letter], y = facet_df[,get(j)],legendgroup = j, color = j , name = j ,  showlegend = F)
    
        }
      }
      facetplots[[i]] <- layout(  facetplots[[i]], barmode = "stack")
    }
    
    q <- subplot(facetplots, shareX = TRUE)
    
    q
    

    【讨论】:

    • 我只是希望有一个功能可以选择和取消选择“好”、“坏”和“丑”,并且条形图会自动调整。我可以显示和隐藏不同的类别——但条形本身不会改变(因此,如果我取消选择条形中间的类别,条形的顶部仍将悬停在其上方,而不是条形变形)。
    【解决方案2】:

    也许这就是你在 ggplot 中所追求的?我不知道您是否想要 ggplots 解决方案或 rCharts 解决方案。所以这是ggplot。

    将 position = "dodge" 添加到您的 geom_bar 参数将模仿您链接到的其他图表

    ggplot(df, aes(letter, fill = status)) +
     geom_bar(position = "dodge") +
     facet_wrap(.~numbers)
    

    或者,您可以“取消选择”状态,并按数字填写——注意,在本例中必须将其作为一个因素:

    ggplot(df, aes(letter, fill = as.factor(numbers))) +
     geom_bar(position = "dodge") +
     facet_wrap(.~numbers)
    

    或者省略保留灰度

    ggplot(df, aes(letter)) +
     geom_bar(position = "dodge") +
     facet_wrap(.~numbers)
    

    【讨论】:

    • 抱歉,我的意思是我想要一个 rCharts 解决方案,因为我希望它是交互式的。我编辑了我的帖子以使其更清晰。
    猜你喜欢
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多