R 更改闪亮应用程序的直方图颜色
我正在构建一个闪亮的应用程序,通过改变要显示的输入,我可以操纵钻石数据集的直方图,这很好。我现在希望能够改变绘图的颜色,以反映钻石数据的切割、清晰度和颜色 构建应用程序的代码R 更改闪亮应用程序的直方图颜色,r,shiny,R,Shiny,我正在构建一个闪亮的应用程序,通过改变要显示的输入,我可以操纵钻石数据集的直方图,这很好。我现在希望能够改变绘图的颜色,以反映钻石数据的切割、清晰度和颜色 构建应用程序的代码 #Data preparation library(shiny) library(tidyverse) library(ggplot2) diamonds_data <- as_tibble(diamonds) %>% rename_all(stringr::str_to_title) #App
#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)
当我使用我的应用程序脚本时,我得到警告>未知颜色名称:Color您可以使用
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 = " "))
my_plot%
ggplot()+
geom_直方图(aes(x=.data[[input$channel1]],fill=.data[[input$color1]]),bins=input$NOofBins)+
主题_bw()+
主题(axis.title=element\u text(大小=26,color=“Grey”,face=“bold”),
axis.text=元素\文本(大小=12,颜色=“灰色”,面=“粗体”))+
实验室(x=“钻石元素”,y=“计数”,title=粘贴(“钻石数据的分发”,输入$channel1,sep=”“))
我现在尝试了一个版本,删除了颜色的if语句,并将填充改为“`input$color1``,但现在我得到了一个警告:未知颜色名称:COLOR谢谢,这使得绘制其他内容变得更容易了!
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 = " "))