r - Shiny plot in ggplot not respecting "fill" argument in plot command -
r - Shiny plot in ggplot not respecting "fill" argument in plot command -
i've included lot of sample code because i'm new shiny (and still beginner r) i'm not sure problem lies.
the first 2 code blocks ui.r , server.r of app, along image of output. problem i'm having "fill" argument in ggplot command isn't beingness respected, , i'm getting mono-color output in chart, when should have 2 different colors in histogram dependent upon "id" value in df.
the lastly block of code , image show same code beingness run in r-studio, desired effect achieved. can see in included output screenshot there 2 overlain histograms colored value of "id" column.
thanks help!
example app:server.r
library(shiny) library(ggplot2) id <- sample(0:1, 100, replace=t) val <- sample(seq(1:25), 100, replace=t) val2 <- sample(seq(1:10), 100, replace=t) info <- data.frame(id, val, val2) # define server shiny app shinyserver(function(input, output) { # fill in spot created plot output$featureplot <- renderplot({ # render histogram # note readers, outcome same whether fill command # written, or written as: fill=as.factor(id) p <- ggplot(data, aes_string(x=input$feature, fill=as.factor(data$id))) + geom_histogram(alpha=0.5, aes(y=..density..), position='identity', binwidth = input$binsize); print(p) }) })
ui.r
library(shiny) library(ggplot2) id <- sample(0:1, 100, replace=t); val <- sample(seq(1:25), 100, replace=t); val2 <- sample(seq(1:10), 100, replace=t); info <- data.frame(id, val, val2); # define overall ui shinyui( fluidpage( titlepanel("why u no color?"), sidebarlayout( sidebarpanel( selectinput("feature", "feature:", choices=colnames(data), selected='val'), hr(), helptext("select feature compare"), sliderinput("binsize", "size of bins", min = 1, max = 10, value = 2) ), mainpanel( plotoutput("featureplot") ) ) ) )
app output:
here want# illustration info help library(ggplot2) id <- sample(0:1,100,replace=t) val <- sample(seq(1:25),100,replace=t) val2 <-sample(seq(1:10), 100, replace=t) df <- data.frame(id, val, val2) ggplot(df, aes(x=val, fill=as.factor(id))) + geom_histogram(alpha=0.7, aes(y=..density..), position = 'identity', binwidth=1)
you can run code , see desired output, here's image in case
you setting fill
in aes_string
you're not passing string, trying pass vector. mixing aes()
, aes_string().
seek separating them
p <- ggplot(data, aes_string(x=input$feature)) + geom_histogram(alpha=0.5, aes(fill=as.factor(id), y=..density..), position='identity', binwidth = input$binsize);
r ggplot2 shiny
Comments
Post a Comment