当用户输入的值发生变化时,在 Shiny Dashboard 的 ggplot 中显示动态百分比和数量值
Diplaying Dynamic Percentage and Quantity Value in ggplot in Shiny Dashboard when values changes on user input
我正在尝试在闪亮的仪表板中的 ggplot 中显示百分比和数量标签,它应该根据用户对 ggplot 的输入进行更新。
我的数据集如下所示:
Week Region Flag Warehouse TAT Quantity
2021-W01 North Local ABC In TAT 10
2021-W01 North Local ABC Out TAT 5
2021-W01 East Local ABC In TAT 13
2021-W01 East Local ABC Out TAT 6
2021-W02 West UPC XYZ In TAT 15
2021-W02 West UPC XYZ Out TAT 10
这里Week是一年中的周数,Region代表North/East/West/South,Flag只有Local和UPC两个值,是移动类型,Warehouse代表不同的仓库,TAT栏显示性能部分。如果是 In tat 则性能好,如果是 out 则性能差。
我现在的成就:
我已经能够在闪亮的仪表板中创建绘图和过滤器,它会根据用户输入进行更新。但是我无法为其添加标签,标签将动态更改为用户输入。
代码:
library(plotly)
library(ggplot2)
library(dplyr)
library(reshape2)
library(gtools)
ui <- shinyUI(
navbarPage(
title = 'Dashboard',
tabPanel('Performance',
tabsetPanel(
tabPanel('View1',
selectInput('warehouse', 'Select Warehouse', unique(plot1$shipment_fc)),
selectInput('region', 'Select Region', unique(plot1$regional_zone)),
selectInput('mov_type', 'Select Movement Type', unique(plot1$flag)),
fluidRow(
plotlyOutput("myplot_fwd")
)
),
tabPanel('View 2'
)
))
)
)
server <- function(input, output) {
output$myplot_fwd <- renderPlotly({
plot1 <- read.csv("plot1.csv", sep = ",", header = TRUE)
data <- plot1 %>%
filter(Warehouse == input$warehouse, Region == input$region, flag == input$mov_type)
p<- ggplot(data, aes(fill=TAT, y=Quantity, x=Week)) +
geom_bar(position="fill", stat="identity",colour="black") +
labs(x = "Week") +
labs(y = "Percentage") +
labs(title = "Performance") +
scale_y_continuous(labels=scales::percent)
p <- ggplotly(p, tooltip="text")
p
})
}
shinyApp(ui, server)
我试图更新我的 renderplotly 部分以显示百分比:
output$myplot_fwd <- renderPlotly({
plot1 <- read.csv("plot1.csv", sep = ",", header = TRUE)
data <- plot1 %>%
filter(Warehouse == input$warehouse, Region == input$region, flag == input$mov_type)
p<- ggplot(data, aes(fill=TAT, y=Quantity, x=Week)) +
geom_bar(position="fill", stat="identity",colour="black") +
labs(x = "Week") +
labs(y = "Percentage") +
labs(title = "Performance") +
scale_y_continuous(labels=scales::percent) +
geom_text(aes(label = Quantity), position = position_stack(vjust = 0.5))
geom_text(data = . %>%
filter(Warehouse == input$warehouse, Region == input$region,flag == input$flag) %>%
group_by(Week,TAT) %>%
mutate(p = Quantity / sum(Quantity)) %>%
ungroup(),
aes(y = p, label = scales::percent(p)),
position = position_stack(vjust = 3.25),
show.legend = FALSE)
p <- ggplotly(p, tooltip="text")
p
})
但这是显示错误的结果并破坏情节
例如:
有没有一种方法可以在 GGplot 中正确显示百分比标签,如果可能的话还可以显示数量标签。
任何帮助将不胜感激。
谢谢。
试试这个:
output$myplot_fwd <- renderPlotly({
plot1 <- read.csv("plot1.csv", sep = ",", header = TRUE)
data <- plot1 %>%
filter(Warehouse == input$warehouse, Region == input$region, Flag == input$mov_type) %>%
group_by(Week) %>%
mutate(label = prop.table(Quantity) * 100)
p <- ggplot(data, aes(fill=TAT, y=label, x=Week, label = paste0(round(label, 2), '%'))) +
geom_col(colour="black") +
labs(x = "Week") +
labs(y = "Percentage") +
labs(title = "Performance") +
geom_text(position = position_stack(vjust = 0.5))
p <- ggplotly(p, tooltip="text")
p
})
我正在尝试在闪亮的仪表板中的 ggplot 中显示百分比和数量标签,它应该根据用户对 ggplot 的输入进行更新。
我的数据集如下所示:
Week Region Flag Warehouse TAT Quantity
2021-W01 North Local ABC In TAT 10
2021-W01 North Local ABC Out TAT 5
2021-W01 East Local ABC In TAT 13
2021-W01 East Local ABC Out TAT 6
2021-W02 West UPC XYZ In TAT 15
2021-W02 West UPC XYZ Out TAT 10
这里Week是一年中的周数,Region代表North/East/West/South,Flag只有Local和UPC两个值,是移动类型,Warehouse代表不同的仓库,TAT栏显示性能部分。如果是 In tat 则性能好,如果是 out 则性能差。
我现在的成就:
我已经能够在闪亮的仪表板中创建绘图和过滤器,它会根据用户输入进行更新。但是我无法为其添加标签,标签将动态更改为用户输入。
代码:
library(plotly)
library(ggplot2)
library(dplyr)
library(reshape2)
library(gtools)
ui <- shinyUI(
navbarPage(
title = 'Dashboard',
tabPanel('Performance',
tabsetPanel(
tabPanel('View1',
selectInput('warehouse', 'Select Warehouse', unique(plot1$shipment_fc)),
selectInput('region', 'Select Region', unique(plot1$regional_zone)),
selectInput('mov_type', 'Select Movement Type', unique(plot1$flag)),
fluidRow(
plotlyOutput("myplot_fwd")
)
),
tabPanel('View 2'
)
))
)
)
server <- function(input, output) {
output$myplot_fwd <- renderPlotly({
plot1 <- read.csv("plot1.csv", sep = ",", header = TRUE)
data <- plot1 %>%
filter(Warehouse == input$warehouse, Region == input$region, flag == input$mov_type)
p<- ggplot(data, aes(fill=TAT, y=Quantity, x=Week)) +
geom_bar(position="fill", stat="identity",colour="black") +
labs(x = "Week") +
labs(y = "Percentage") +
labs(title = "Performance") +
scale_y_continuous(labels=scales::percent)
p <- ggplotly(p, tooltip="text")
p
})
}
shinyApp(ui, server)
我试图更新我的 renderplotly 部分以显示百分比:
output$myplot_fwd <- renderPlotly({
plot1 <- read.csv("plot1.csv", sep = ",", header = TRUE)
data <- plot1 %>%
filter(Warehouse == input$warehouse, Region == input$region, flag == input$mov_type)
p<- ggplot(data, aes(fill=TAT, y=Quantity, x=Week)) +
geom_bar(position="fill", stat="identity",colour="black") +
labs(x = "Week") +
labs(y = "Percentage") +
labs(title = "Performance") +
scale_y_continuous(labels=scales::percent) +
geom_text(aes(label = Quantity), position = position_stack(vjust = 0.5))
geom_text(data = . %>%
filter(Warehouse == input$warehouse, Region == input$region,flag == input$flag) %>%
group_by(Week,TAT) %>%
mutate(p = Quantity / sum(Quantity)) %>%
ungroup(),
aes(y = p, label = scales::percent(p)),
position = position_stack(vjust = 3.25),
show.legend = FALSE)
p <- ggplotly(p, tooltip="text")
p
})
但这是显示错误的结果并破坏情节 例如:
有没有一种方法可以在 GGplot 中正确显示百分比标签,如果可能的话还可以显示数量标签。 任何帮助将不胜感激。 谢谢。
试试这个:
output$myplot_fwd <- renderPlotly({
plot1 <- read.csv("plot1.csv", sep = ",", header = TRUE)
data <- plot1 %>%
filter(Warehouse == input$warehouse, Region == input$region, Flag == input$mov_type) %>%
group_by(Week) %>%
mutate(label = prop.table(Quantity) * 100)
p <- ggplot(data, aes(fill=TAT, y=label, x=Week, label = paste0(round(label, 2), '%'))) +
geom_col(colour="black") +
labs(x = "Week") +
labs(y = "Percentage") +
labs(title = "Performance") +
geom_text(position = position_stack(vjust = 0.5))
p <- ggplotly(p, tooltip="text")
p
})