R,闪亮:ggplot,geom_bar,当 fill 是反应式时,条形图按其 fill 参数排序
R, shiny: ggplot, geom_bar, order of bars by their fill argument when fill is a reactive
我正在为 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
填充参数是反应性的,用户在闪亮的应用程序中选择。
结果如下所示:
我希望两个蓝色条和两个红色条并排,就像这样 post ggplot can not group bars
棘手的是,填充参数会随着用户从 shiny app 输入而改变,因此必须改变条的顺序。
类似于:
aes_string(dodge = razeni())
或 aes_string(order = razeni())
我希望这个问题很清楚,我很乐意就如何处理它提出任何建议。
所以,如果有人遇到同样的问题,这是我的解决方案,适用于我的情况:
首先,我用数据创建了一个反应式 data.frame
。然后我将 table 的选定列作为一个因子(在我的例子中是 "ID_Polymer"),我设置了因子的水平,并使用 order()
函数和一个由用户(由 shinyUI()
由 selectInput
input$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)) +
})
此解决方案对我有用,希望对您有所帮助。
我正在为 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
填充参数是反应性的,用户在闪亮的应用程序中选择。
结果如下所示:
我希望两个蓝色条和两个红色条并排,就像这样 post ggplot can not group bars
棘手的是,填充参数会随着用户从 shiny app 输入而改变,因此必须改变条的顺序。
类似于:
aes_string(dodge = razeni())
或 aes_string(order = razeni())
我希望这个问题很清楚,我很乐意就如何处理它提出任何建议。
所以,如果有人遇到同样的问题,这是我的解决方案,适用于我的情况:
首先,我用数据创建了一个反应式 data.frame
。然后我将 table 的选定列作为一个因子(在我的例子中是 "ID_Polymer"),我设置了因子的水平,并使用 order()
函数和一个由用户(由 shinyUI()
由 selectInput
input$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)) +
})
此解决方案对我有用,希望对您有所帮助。