首页 > 解决方案 > 在 R 中,我可以按情节图例中的多个因素进行排序吗?

问题描述

我的目标是将点与多个图例属性相关联,以帮助某人主动过滤图表以进行比较。例如,我尝试将代码中的每个点与其 DP 及其描述独立关联。这样我就可以隐藏除 DP1 点之外的所有点,或者只显示测试一个结果。相反,我的代码使每个点作为其因素的组合而独一无二。谢谢您的帮助!

这是我的尝试。

library(tidyverse)
library(plotly)

df = read.csv("C:/Users/nicho/Documents/R/Test.csv")

output = df %>%
ggplot(df,mapping = aes(Wave,Score,col = factor(Level),
                        text = paste("DP:",DP,"<br>",
                                     "Wave:",Wave,"<br>",
                                     "Level:",Level,"<br>",
                                     "Score:",Score,"<br>",
                                     "Desc:",Desc
                                     
                        )
                        
                        ))+
  geom_line(mapping = aes(group = DP))+
  geom_point(mapping = aes(col =factor(DP),shape = factor(Desc)))+
  facet_grid(~Location)

font = list(
  size = 15,
  color = "white"
)

label = list(
  bgcolor = "#232F34",
  bordercolor = "transparent",
  font = font
  
)


ggplotly(output, tooltip = c("text")) %>%
  style(hoverlabel = label) %>%
  layout(font = font)

输出

标签: rggplot2plotlytidyverseggplotly

解决方案


这是我最终为遇到此问题的任何人找到的解决方案,我试图像这样过滤我的图表结果......

library(shinydashboard)
library(shiny)

#______________________________________________________________________________#
server <- function(input, output, session) { 
    df <- reactive({
        subset(iris, Petal.Width == input$Petalw)
    })
    
    # Extract list of Petal Lengths from selected data - to be used as a filter
    p.lengths <- reactive({
        unique(df()$Petal.Length)
    })
    
    # Filter based on Petal Length
    output$PetalL <- renderUI({
        selectInput("PetalLengthSelector", "PetalLength", as.list(p.lengths()))
    })
    
    # Subset this data based on the values selected by user
    df_1 <- reactive({
        foo <- subset(df(), Petal.Length == input$PetalLengthSelector)
        return(foo)
    })
    
    output$table <- DT::renderDataTable(
        DT::datatable(df_1(), options = list(searching = FALSE,pageLength = 25))
    )
    ###JUNK
    output$correlation_plot <- renderPlot({
        plot(df_1()$Petal.Length, df_1()$Petalw,
             xlab = "Length", ylab = "Width")
    })
}

#______________________________________________________________________________#
ui <- navbarPage(
    title = 'Select values in two columns based on two inputs respectively',
    
    fluidRow(
        column(width = 12,
    plotOutput('correlation_plot')
        )
    ),
    
    
    fluidRow(
        column(width = 3,
               selectInput("Petalw","PetalWidth", choices = unique(iris$Petal.Width),multiple = T),
               uiOutput("PetalL")
        ),
        column(9,
               tabPanel('Table', DT::dataTableOutput('table'))
        )
    )
)
shinyApp(ui, server)

推荐阅读