【问题标题】:Changing the histogram colour for a shiny app更改闪亮应用的直方图颜色
【发布时间】:2020-11-03 17:10:42
【问题描述】:

我正在构建一个闪亮的应用程序,我可以在其中通过更改要显示的输入来操作钻石数据集的直方图,效果很好。我现在希望能够更改绘图的颜色以反映钻石数据的切工、净度和颜色。

构建应用程序的代码

#Data preparation

library(shiny)
library(tidyverse)
library(ggplot2)

diamonds_data <- as_tibble(diamonds) %>%
    rename_all(stringr::str_to_title)

#App design
ui <- fluidPage(
    
    #App title
    titlePanel("Histogram version"),
    
    #Sidebar layout for main and sidebar panel
    sidebarLayout(
        # Sidebar panel for inputs
        sidebarPanel(
            
            #Selecting histogram colour
            selectInput(inputId="color1",label="Choose Color",choices = c("Color"="Color","Carat"="Carat","Clarity"="Clarity"),
                        selected = "Cut",multiple = F),
            
            #Selecting Histogram input
            selectInput(inputId="channel1",label="Distribution of data",choices = c("Carat"="Carat",
                                                                              "Depth"="Depth",
                                                                              "Table"="Table",
                                                                              "Price"="Price",
                                                                              "X"="X",
                                                                              "Y"="Y",
                                                                              "Z"="Z"),
                        selected = "Carat",multiple = F),
            
            #Selecting number of histogram bind
            sliderInput(inputId = "NOofBins",
                        label = "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30),
            
        ),
        
        #Main panel for outputs
        mainPanel(
            
            #Histogram
            plotOutput(outputId = "distPlot")
        )
    )
)


server <- function(input, output){
    
    
    output$distPlot <- renderPlot({
        
        if(input$color1=="Clarity"){
            Color = "Clarity"
        }else if(input$color1=="Cut"){
            Color = "Cut"
        }else if(input$color1=="Color"){
            Color = "Color"
        }
        
        my_plot <- diamonds_data %>%  ggplot()
        if(input$channel1 == "Carat"){
            my_plot <- my_plot + geom_histogram(aes(x=Carat),bins = input$NOofBins,fill=Color)
        }else if(input$channel1 == "Depth"){
            my_plot <- my_plot + geom_histogram(aes(x=Depth),bins = input$NOofBins,fill=Color)
        }else if(input$channel1 == "Table"){
            my_plot <- my_plot + geom_histogram(aes(x=Table),bins = input$NOofBins,fill=Color)
        }else if(input$channel1 == "Price"){
            my_plot <- my_plot + geom_histogram(aes(x=Price),bins = input$NOofBins,fill=Color)
        }else if(input$channel1 == "X"){
            my_plot <- my_plot + geom_histogram(aes(x=X),bins = input$NOofBins,fill=Color)
        }else if(input$channel1 == "Y"){
            my_plot <- my_plot + geom_histogram(aes(x=Y),bins = input$NOofBins,fill=Color)
        }else if(input$channel1 == "Z"){
            my_plot <- my_plot + geom_histogram(aes(x=Z),bins = input$NOofBins,fill=Color)
        }
        my_plot <- my_plot +  theme_bw()+
            theme(axis.title = element_text(size=26,color="Grey",face="bold"),
                  axis.text = element_text(size=12,color="Grey",face="bold"))+
            labs(x="Diamonds Element",y="Count",title=paste("Distribution of diamonds data",input$channel1,sep = " "))
        
        my_plot
    })
}

shinyApp(ui = ui, server = server)

如前所述,我希望能够更改直方图的输入,我可以根据切割、清晰度或颜色更改颜色,如下面的代码所示:

ggplot(data = diamonds_data, aes(x = Price)) +
  geom_histogram(aes(fill = Cut))

当我使用我的应用脚本时,我收到警告 >Unknown color name: Color

【问题讨论】:

  • 我现在已经尝试了删除颜色的 if 语句并将填充更改为 ``` input$color1 ``` 但现在我收到警告 Unknown color name: Color

标签: r shiny


【解决方案1】:

您可以使用ggplot.data 参数,它接受字符串作为输入,从而显着简化您的代码:

my_plot <- diamonds_data %>% 
      ggplot() +
      geom_histogram(aes(x = .data[[input$channel1]], fill = .data[[input$color1]]), bins = input$NOofBins) +
      theme_bw()+
      theme(axis.title = element_text(size=26,color="Grey",face="bold"),
            axis.text = element_text(size=12,color="Grey",face="bold"))+
      labs(x="Diamonds Element",y="Count",title=paste("Distribution of diamonds data",input$channel1,sep = " "))

【讨论】:

  • 感谢这让绘制其他内容变得更加容易!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-28
  • 1970-01-01
  • 2014-08-23
  • 2016-02-10
  • 1970-01-01
  • 2015-10-10
  • 2017-12-14
相关资源
最近更新 更多