首页 > 解决方案 > R Shiny如何让ggplot通过dplyr过滤器做颜色/形状

问题描述

以下代码有效(没有任何颜色/形状输入),但是尝试使点具有不同的形状/颜色已被证明是困难的,我不确定问题是什么。

我试图拥有:

年份/范围在形状周围有黄色轮廓

产品/输入是不同的形状

状态/角色是不同的颜色

library(shiny)
library(dplyr)
library(shinydashboard)
library(tidyverse)

ui <- dashboardPage(
  dashboardHeader(title="Membership Satisfaction"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Demographics Dashboard", tabName = "demos", icon = 
             icon("dashboard"))
    )
  ),
  dashboardBody(
    tabItems(

    tabItem(tabName = "demos",
          sidebarPanel(
            checkboxGroupInput("inpt","Select variables to plot", choices = 
                                 c("Web" = 1,"Huddle" = 3, "Other" = 5, 
            "Test" = 7)),
            checkboxGroupInput("role", 
                               "Select Primary Role of Interest", 
                               choices = c("Student" = 1, "Not" = 2)),
            checkboxGroupInput("range", 
                               "Select year(S) of Interest", 
                               choices = c("2016"=2,"July 2017"=1))),
          fluidPage(

            plotOutput("plot")
            #  tableOutput("test")

          )))))

我的服务器:

server <- function(input,output){

  library(tidyverse)

   xPre <- reactive({
   inpt <- as.double(input$inpt)
   role <- as.double(input$role)
   range <- as.double(input$range)

   GapAnalysis_LongFormB %>%
  filter(Product %in% inpt,
         year %in% range)
  })


  yPre <- reactive({
  inpt <- as.double(input$inpt)+1
  role <- as.double(input$role)
  range <- as.double(input$range)

  GapAnalysis_LongFormB %>%
  filter(Product %in% inpt,
         year %in% range)
   })



  xPost<- reactive({
  xPre<- xPre()
  inpt <- as.double(input$inpt)
  role <- as.double(input$role)
  range <- as.double(input$range)

  xPre  %>%
  filter(year %in% range,
         Product %in% inpt,
         status %in% role)%>%
  group_by(Product, status,year)%>%
  summarize(avg = mean(Score, na.rm = TRUE)) %>%
  pull(-1)

  })


yPost<- reactive({
yPre <- yPre()
inpt <- as.double(input$inpt)+1
role <- as.double(input$role)
range <- as.double(input$range)

yPre  %>%
  filter(year %in% range,
         Product %in% inpt,
         status %in% role)%>%
  group_by(Product, status,year)%>%
  summarize(avg = mean(Score, na.rm = TRUE)) %>%
  pull(-1)


 })


ySum <- reactive({ 

yPre <- yPre()
inpt <- as.double(input$inpt)+1
role <- as.double(input$role)
range <- as.double(input$range)

yPre  %>%
  filter(year %in% range,
         Product %in% inpt)%>%
  group_by(Product,year)%>%
  summarize(avg = mean(Score, na.rm = TRUE)) %>%
  pull(-1)
})



xSum <- reactive({ 
xPre <- xPre()
inpt <- as.double(input$inpt)
role <- as.double(input$role)
range <- as.double(input$range)

xPre  %>%
  filter(year %in% range,
         Product %in% inpt)%>%
  group_by(Product,year)%>%
  summarize(avg = mean(Score, na.rm = TRUE)) %>%
  pull(-1)
})

xyCoords<- reactive({
xPost <- xPost()
yPost <- yPost()
xSum <- xSum()
ySum <- ySum()



as.data.frame(matrix(c(xPost,xSum,yPost,ySum),ncol = 2))


})



 output$test<- renderTable({
  xyCoords <- xyCoords()
 })

output$plot <- renderPlot({

  xyCoords <- xyCoords()    

  xyCoords %>% 
   ggplot(aes(x=V1, y =V2 )) +
   geom_point(colour = "blue", shape = 17, size = 5 )+
   labs(x = "Mean Satisfaction", y = "Mean Importance") +
   xlim(0,5) + ylim(0,5) +
   geom_vline(xintercept=2.5) + 
   geom_hline(yintercept =  2.5)


 })

 }



shinyApp (ui = ui, server = server)

GapAnalysis_LongFormB 的变量结构:

structure(list(status = c(1, 5, 5, 1, 1, 5), year = c(1, 1, 1, 
1, 1, 1), Product = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", 
"2", "3", "4"), class = "factor"), Score = c(2, 5, 3, 5, 4, 4
)), .Names = c("status", "year", "Product", "Score"), row.names = c(NA, 
6L), class = "data.frame")

当我尝试执行 color = input$inpt 或 shape = input$inpt 时出现错误“美学必须是长度 1 或与数据 (3) 相同:形状、颜色、大小”

有任何想法吗??谢谢!!

标签: rinputggplot2shinyaesthetics

解决方案


推荐阅读