【问题标题】:ggplot visual of convex hull - shiny凸包的ggplot视觉效果 - 闪亮
【发布时间】:2022-01-11 06:48:26
【问题描述】:

我想为用户提供的任何数字变量创建凸包的 ggplot 视觉效果,同时允许将包拆分为所选分类输入变量的多个包。

类似这样的东西,用户可以根据需要选择任意数量的 x 变量

library(datasets)
library(ggplot2)
library(ggpubr)

df<- iris
str(df)

b <- ggplot(df, aes(x = Sepal.Length+Sepal.Width, y = Petal.Width))
 
# Convex hull of groups
b + geom_point(aes(color = Species, shape = Species)) +
  stat_chull(aes(color = Species, fill = Species), 
             alpha = 0.1, geom = "polygon") +
  scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07")) +
  scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))

到目前为止,这是我的闪亮代码,不用说它不起作用。任何帮助将不胜感激

library(shiny)
library(dplyr)
library(ggplot2)
library(ggpubr)
df<- mtcars
numeric_cols <- df %>% select(where(is.numeric))
categorical_cols <- df %>% select(where(is.factor))
ui <- fluidPage(
  titlePanel("MTCARS"),
  selectInput("Columns","Numeric Columns",
              names(numeric_cols), multiple = TRUE),
  selectInput("Columns","Categorical Columns",
              names(categorical_cols), multiple = TRUE),
  plotOutput("myplot")
)

server <- function(input, output) {
  Dataframe2 <- reactive({
    mtcars[,input$Columns] 
  })
  
  my_plot<- reactive({ggplot(Dataframe2(), aes(mpg, cyl))+geom_point()})
  output$myplot <- renderPlot({
    my_plot()
  })
}

shinyApp(ui, server)

【问题讨论】:

    标签: r ggplot2 shiny convex-hull


    【解决方案1】:

    这里有 3 个问题。首先,mtcars 中的分类变量为零。其次,您应该使用唯一的IDs,但您对两个selectInputs 使用相同的ID。最后,对于绘图,您需要使用选定的列,而不是指定 mpgcyl。试试这个

    library(shiny)
    library(dplyr)
    library(ggplot2)
    library(ggpubr)
    df<- mtcars
    numeric_cols <- df %>% dplyr::select(where(is.numeric))
    categorical_cols <- df %>% dplyr::select(where(is.factor))
    ui <- fluidPage(
      titlePanel("MTCARS"),
      selectInput("Columns","Numeric Columns",
                  names(numeric_cols), multiple = TRUE),
      # selectInput("Columns","Categorical Columns",
      #             names(categorical_cols), multiple = TRUE),
      plotOutput("myplot")
    )
    
    server <- function(input, output) {
      Dataframe2 <- reactive({
        req(length(input$Columns)>=2)
        mtcars[,input$Columns] 
      })
      
      my_plot<- reactive({ggplot(Dataframe2(), aes(.data[[input$Columns[1]]],.data[[input$Columns[2]]] ))+geom_point()})
      output$myplot <- renderPlot({
        my_plot()
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 2016-07-02
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多