【问题标题】:R, shiny: ggplot, geom_bar, order of bars by their fill argument when fill is a reactiveR,闪亮:ggplot,geom_bar,当填充是反应性时,它们的填充参数的条形顺序
【发布时间】:2018-09-23 14:23:26
【问题描述】:

我正在为 ggplot 中的条形顺序而苦苦挣扎。为了简单起见:这是我用来创建情节的代码:

plot_data <- data() %>% 
  filter(Name %in% input$polymers)
print(plot_data)
ggplot(data = plot_data, aes(x = ID_Polymer, y = value), position = position_dodge(width = 1))  +
  geom_bar(aes_string( fill=razeni()), position = position_dodge(width = 1), stat="identity", color="white")+
  theme_minimal() +
  theme(legend.text=element_text(size=21))+
  theme(text = element_text(size=21))+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  ggtitle(input$title_text_box_id) + 
  labs(x = "", y = input$ylabel_text_box_id) + 
  geom_text(aes(x = ID_Polymer, y = value,Group=Polymer,label=value), 
            position = position_dodge(width = 1),vjust=2, size=5,colour = "white", fontface = "bold") +
  scale_fill_tableau("Tableau 10")+
  scale_x_discrete(labels=c(xpopisky()))#puts a reactive in x labels

fill 参数是反应性的,用户在闪亮的应用程序中选择。 结果如下所示:

我希望两个蓝色条和两个红色条并排,就像在这个帖子ggplot can not group bars

棘手的是,填充参数会随着来自闪亮应用程序的用户输入而改变,因此必须改变条形图的顺序。

类似: aes_string(dodge = razeni())aes_string(order = razeni())

我希望这个问题很清楚,我很高兴收到有关如何处理它的任何建议。

【问题讨论】:

    标签: r ggplot2 shiny geom-bar


    【解决方案1】:

    所以,如果有人遇到同样的问题,这是我的解决方案,它适用于我的情况: 首先,我用数据创建了一个反应式data.frame。然后我将表的选定列设为因子(在我的情况下为“ID_Polymer”),我设置因子的级别,并使用order() 函数和用户选择的参数(由@ 987654323@selectInputinput$groupby)。代码如下:

    dataforplot <- reactive({
      plot_data <-  data() %>% 
        filter(Name %in% input$polymers) 
        plot_data$ID_Polymer <- factor(plot_data$ID_Polymer, 
              levels =plot_data$ID_Polymer[ order(plot_data[[input$groupby]])])
        return(plot_data) # so it returns the data frame and not only the column  plot_data$ID_Polymer
    })
    

    我只是创建了一个 ggplot:

    plotInput <- reactive({
        ggplot(data = dataforplot(), aes(x = ID_Polymer, y = value), position = position_dodge(width = 1)) +
    })
    

    这个解决方案对我有用,所以我希望它有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-19
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      • 2020-08-20
      相关资源
      最近更新 更多