简体   繁体   English

RShiny 中的绘图分组条形图

[英]Plotly Grouped Bar Chart in RShiny

I am trying to create a grouped bar chart using Plotly and RShiny where the user can select a variable to group by and display on the xaxis (called xvar()), and a variable which splits the xaxis variable into further groups (called xsubvar()).我正在尝试使用 Plotly 和 RShiny 创建一个分组条形图,用户可以在其中选择一个变量进行分组并显示在 xaxis 上(称为 xvar()),以及一个将 xaxis 变量拆分为更多组的变量(称为 xsubvar( ))。

I am trying to display the average of the variable AVAL on the y axis.我试图在 y 轴上显示变量 AVAL 的平均值。

Here is my current code:这是我当前的代码:

barGraphGrouped <- reactive({
    
    filteredData() %>% group_by(xvar(),xsubvar()) %>% mutate(n=n_distinct(USUBJID)) %>% 
      plot_ly(x=xvar(),y=filteredData()$AVAL,type="bar",text =~paste('n =',n), textposition = 'outside',
              textfont = list(size = 14),
              transforms = list(
                list(
                  type = 'aggregate',
                  groups = xvar(),
                  aggregations = list(list(target = 'y', func = 'avg', enabled = T))
                )
              )) %>%
      add_trace(x=xsubvar(),y=filteredData()$AVAL,
                transforms = list(
                  list(
                    type = 'aggregate',
                    groups = xsubvar(),
                    aggregations = list(list(target = 'y', func = 'avg', enabled = T))
                  )
                )) %>%
      layout(barmode='group',title=paste("Average AVAL by",input$xradio),
             xaxis = list(title = input$xradio,tickfont = list(size = 13)), 
             yaxis = list(title = input$yradio,tickfont = list(size = 13)))
  })

This results in the following plot: Plot1这导致以下图: Plot1

As you can see, the groups are split how I would like but the n on top of each bar and the mean AVAL is not calculating as I hoped.正如您所看到的,这些组按照我想要的方式划分,但是每个条形顶部的 n 和平均 AVAL 并没有按我希望的那样计算。

I have created the exact graph I am hoping to accomplish in ggplot using the following code:我已经使用以下代码创建了我希望在 ggplot 中完成的确切图形:

myplot <- filteredData() %>% group_by(xvar(),xsubvar()) %>%
       dplyr::mutate(AVAL=mean(AVAL)) %>%
       ggplot(aes(x=xvar(),y=AVAL,fill=xsubvar(),label=xsubvar()))+
       geom_col(stat="identity",position = position_dodge(.9))+
       scale_fill_brewer(palette = "Set1") +
       theme_classic()+
       ggtitle(paste("Average AVAL by",input$xradio,"and",input$xsubradio))+
       ylab(input$yradio)+
       xlab(input$xradio)+
       scale_x_discrete(labels=names(xvar()))+
       geom_text(position = position_dodge(.9),size=3)+
       theme(
         legend.position = "none",
         panel.grid.major.y = element_blank(),
       )

ggplotly(myplot) %>% layout(legend = list(orientation = "h", x = 0.4, y = -0.2))

This generates the following plot: Plot 2这将生成以下图:图 2

However I am trying to figure out how to recreate in plotly because I prefer the visual output and customization options present in plotly.但是,我试图弄清楚如何在 plotly 中重新创建,因为我更喜欢 plotly 中存在的视觉输出和自定义选项。

Thank you!谢谢!

Without sample data it is hard to verify the issue.没有样本数据很难验证问题。 Please try this请试试这个

filteredData() %>% group_by(xvar(),xsubvar()) %>% mutate(n=mean(AVAL)) %>% 
  plot_ly(x=xvar(),y=n,type="bar",text =~paste('n =',n), textposition = 'outside',
          textfont = list(size = 14),
          transforms = list(
            list(
              type = 'aggregate',
              groups = xvar(),
              aggregations = list(list(target = 'y', func = 'avg', enabled = T))
            )
          ))

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

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