分类条形图标签不会中心对齐,r光泽GGPLOT2



我知道这已经回答了很多次,我还找到了有关位置_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)

这是输出在此处输入图像描述

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作为未引用的变量传递给填充。这是节省我一天的链接。我希望它有帮助;(

最新更新