首页 文章

分类条形图标签不会居中对齐,R Shiny ggplot2

提问于
浏览
0

我知道这已经被无数次回答了,我也找到了有关position_dodge的详细解释,以便在这篇文章中对齐条形图标签What is the width argument in position_dodge?

但由于某种原因,我无法弄清楚我的情况的躲闪位置 . 我正在创建一个反应数据集,以便通过从下拉菜单中选择的指标获取计数,然后将其传递给ggplot2,下面是我的代码 . 输入$ qtr和输入$ met由用户选择 .

library(readxl)
library(shiny)
library(ggplot2)
library(dplyr)
library(zoo)
library(shinydashboard)

data ("mtcars")
df$Qtr <- ifelse(mtcars$am==1, "2018Q1","2018Q2")
df$Responsibility <- ifelse(mtcars$gear %in% c(3, 4), "Category 1","Category 
2")
df$Leader <- ifelse(mtcars$vs==0, "No","Yes")
df$Failure <- ifelse(mtcars$carb %in% c(1, 2), "Quality","Timing")
df$Cname <- ifelse(mtcars$carb %in% c(1, 2), "Company 1","Company 2")
df <- df[,c("Cname", "Responsibility", "Qtr", "Leader","Failure")]





ui <- dashboardPage(skin="blue",
                dashboardHeader(title = "R Shiny Concept",titleWidth = 200),
                # Sidebar layout with a input and output definitions
                dashboardSidebar(id="", sidebarMenu(uiOutput("qtr"),
                  menuItem("All", tabName = "all", icon = icon("bars")))),

                dashboardBody (tabItems(tabItem(tabName = "all",
                          fluidRow(uiOutput("met")),
                          fluidRow(plotOutput(outputId = "metrics"))))))

server <- function(input, output, session) {  
# Drop-down selection box for quarter
output$qtr <- renderUI({selectInput(inputId = "qtr", 
            label = "Pick a Quarter", 
            choices= as.list(gsub(" ","",df$Qtr)),
            selected = 1)})

# Drop-down selection box for metrics
output$met <- renderUI({selectInput(inputId = "met", 
            label = "Pick a metrics to report", 
            choices= c("Responsibility", 
                       "Leader", 
                       "Failure"),
            selected = 1)})

#  Create a subset of data filtering for selected CRO
freq_subset2 <- reactive({
req(input$qtr)
req(input$met)

df %>%
group_by_at(vars(Cname, Qtr,(input$met))) %>%
select(Cname, Qtr,(input$met)) %>%
summarise(count = n()) %>% 
filter(gsub(" ","",Qtr) %in% input$qtr)
})

plot3 <- output$metrics <- renderPlot({
ggplot(data = freq_subset2(), aes(x=Cname, y=count)) +
labs(y=" ", x = " ")+
geom_bar(stat="identity", position = "dodge", width=0.8, aes_string(fill =
(input$met))) +   
geom_text(aes(label=count), color="black",position = 
position_dodge(width=0.8),
         hjust = 1.5, size=3.5) +scale_fill_brewer(palette="Set1") +
theme(axis.ticks = element_blank(),axis.text.y = element_blank(),
     panel.grid.minor = element_blank(), 
    panel.grid.major = element_blank(),panel.background= element_blank(), 
   plot.title = element_text(size = rel(1.5), face = "bold"),
  legend.position="bottom",legend.title = element_text(color = "white"))

})
plot3

}
# Create the Shiny app object
shinyApp(ui = ui, server = server)

这是输出enter image description here

1 回答

  • 0
    server <- function(input, output, session) {  
             # Drop-down selection box for quarter
             output$qtr <- renderUI({selectInput(inputId = "qtr", 
                                      label = "Pick a Quarter", 
                                      choices= as.list(gsub(" ","",df$Qtr)),
                                      selected = 1)})
    
             # Drop-down selection box for metrics
              output$met <- renderUI({selectInput(inputId = "met", 
                                      label = "Pick a metrics to report", 
                                      choices= c("Responsibility", 
                                                 "Leader", 
                                                 "Failure"),
                                      selected = 1)})
    
          #  Create a subset of data filtering for selected CRO
            freq_subset2 <- reactive({
               req(input$qtr)
               req(input$met)
    
               df %>%
                  group_by_at(vars(Cname, Qtr,(input$met))) %>%
                  select(Cname, Qtr,(input$met)) %>%
                  summarise(count = n()) %>% 
                  filter(gsub(" ","",Qtr) %in% input$qtr)
    
    
      })
    
               #observe(print(freq_subset2()))
               plot3 <- output$metrics <- renderPlot({
                        ggplot(data = freq_subset2(), aes_string(x="Cname", y="count", fill= as.character(input$met))) +
      labs(y=" ", x = " ") + geom_bar(stat="identity", position = "dodge", width=0.8) +
      #geom_bar(stat="identity", position = "dodge", width=0.8, aes_string(fill = (input$met))) +   
      geom_text(aes(label=count), color="black",position = 
                  position_dodge(width=0.8),
                hjust = 0.5,vjust=-1, size=3.5) +scale_fill_brewer(palette="Set1") +
      theme(axis.ticks = element_blank(),axis.text.y = element_blank(),
            panel.grid.minor = element_blank(), 
            panel.grid.major = element_blank(),panel.background= element_blank(), 
            plot.title = element_text(size = rel(1.5), face = "bold"),
            legend.position="bottom",legend.title = element_text(color = "white"))
    
          })
          plot3
    
      }
       # Create the Shiny app object
       shinyApp(ui = ui, server = server)
    

    你是对的问题是如何通过 input$met 作为未加引号的变量来填充 . 这是link,它节省了我的一天Loooool . 我希望它有所帮助;)

相关问题