r - How to retain plot layout characteristics when using ggplot in Shiny? -
when making straightforward ggplot, canvas size extend height of plot appropriate, allowing spacing between variables. example, if have 10 variables need show on 1 plot, plotted nicely:
time shiny! let's see if can embed plot in tabpanel.
here go:
looks great, horizontal segments cramped pack of hotdogs. there no whitespace between horizontal geom_segments
.
this issue plagues me.
i feel ggplot has brain of it's own, when comes canvas sizes, fine, can't pry skull off appropriately control how plot itself.
to quote forrest r. stevens, "...the key getting consistent canvas , other dimensions control graphic output device directly."
i totally agree him , used excellent advice here, yet there no way control since using shiny.
next attempted play values in
p <- p + theme(plot.margin=unit(c(0,0,0,0), "cm") p <- p + panel.margin=unit(c(0,0,0,0), "cm"))
but alas did no good; merely created margins , further crunched things together, makes perfect sense.
my question -- how can change ggplot appears in shiny proper spacing?
good looking ggplot, not in shiny:
library("shiny") library("ggplot2") df_for_plotting <- structure(list(col1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), col2 = c(100, 100, 61.9433678425096, 10.7823906941804, 4.18175346165306, 3.24251454697229, 6.68573373055455, 14.945119260922, 18.9296271776082, 11.0742379220636 ), col3 = c(100, 100, 100, 12.8418470680653, 5.31239161296286, 4.42025167250118, 10.699998838647, 27.5067118056336, 20.6360723198699, 13.1476876837599), col4 = c(100, 100, 100, 100, 100, 100, 100, 100, 100, 100)), .names = c("col1", "col2", "col3", "col4"), row.names = c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"), class = "data.frame") hex=c("#cc0000", "#90bd31", "#178ccb") textsize <- c(9) number_of_variables <- 5 p <- ggplot() p <- p + scale_y_discrete(breaks = seq(10), labels = c("one", "two", "three", "four", "five")) breaks=c(0, 25, 50, 75, 100) break_labels=c("0%", "25%", "50%", "75%", "100%") p <- p + scale_x_continuous(breaks = breaks, labels=break_labels, name=percent_correct) ### inner loop (varnum in seq(1:number_of_variables)){ #<-- in tab. need make 3 segments 3 variables tab p <- p + geom_segment(data=df_for_plotting, aes_q(x=df_for_plotting$col1[varnum], xend=df_for_plotting$col2[varnum]-0.001, y=varnum, yend=varnum, colour='impaired'), size=textsize*2.5) + geom_segment(data=df_for_plotting, aes_q(x=df_for_plotting$col2[varnum], xend=df_for_plotting$col3[varnum]-0.001, y=varnum, yend=varnum, colour='normal'), size=textsize*2.5) + geom_segment(data=df_for_plotting, aes_q(x=df_for_plotting$col3[varnum], xend=df_for_plotting$col4[varnum]-0.001, y=varnum, yend=varnum, colour='optimal'), size=textsize*2.5) p <- p + scale_color_manual(values=c(impaired=hex[1], normal=hex[2], optimal=hex[3], white='#ffffff'), name="function key") } p
ui.r
shinyui(fluidpage(theme='test.css', fluidrow( column(2, fluidrow( h3("select customer:"), wellpanel(class="info", numericinput(inputid="num", label="select id:", value=nan), if(show_age_slider=='yes'){textoutput("")}, if(show_edu_slider=='yes'){textoutput("")}, if(show_gender_buttons=='yes'){textoutput("")} ))), #do.call call navbarpage function arguments in tabs list shinyui(fluidrow( column(12, "", do.call(navbarpage,tabs) ))))))
server.r
library("shiny") library("ggplot2") df_for_plotting <- structure(list(col1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), col2 = c(100, 100, 61.9433678425096, 10.7823906941804, 4.18175346165306, 3.24251454697229, 6.68573373055455, 14.945119260922, 18.9296271776082, 11.0742379220636 ), col3 = c(100, 100, 100, 12.8418470680653, 5.31239161296286, 4.42025167250118, 10.699998838647, 27.5067118056336, 20.6360723198699, 13.1476876837599), col4 = c(100, 100, 100, 100, 100, 100, 100, 100, 100, 100)), .names = c("col1", "col2", "col3", "col4"), row.names = c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"), class = "data.frame") hex=c("#cc0000", "#90bd31", "#178ccb") textsize=c(8) ############## shiny server starts here: ####################################################################### shinyserver(function(input, output) { # begin observe() block observe( lapply(seq(1:number_of_tabs),function(i) output[[paste0("plot",i)]] <- renderplot({ #<-- lapply fill each tab , create 1 ggplot plotindex <<- 0 list_of_ggplots <<- list() #although have 1 tab, extend having multiple tabs p <- ggplot() breaks=c(0, 25, 50, 75, 100, 115, 130) break_labels=c("0%", "25%", "50%", "75%", "100%") number_of_variables <- 10 ### inner loop (varnum in seq(1:number_of_variables)){ #<-- need make 3 segments 3 variables tab p <- p + scale_y_discrete(breaks = seq(10), labels = c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten")) p <- p + geom_segment(data=df_for_plotting, aes_q(x=df_for_plotting$col1[varnum], xend=df_for_plotting$col2[varnum]-0.001, y=varnum, yend=varnum, colour='impaired'), size=textsize*2.5) + geom_segment(data=df_for_plotting, aes_q(x=df_for_plotting$col2[varnum], xend=df_for_plotting$col3[varnum]-0.001, y=varnum, yend=varnum, colour='normal'), size=textsize*2.5) + geom_segment(data=df_for_plotting, aes_q(x=df_for_plotting$col3[varnum], xend=df_for_plotting$col4[varnum]-0.001, y=varnum, yend=varnum, colour='optimal'), size=textsize*2.5) p <- p + scale_color_manual(values=c(impaired=hex[1], normal=hex[2], optimal=hex[3], white='#ffffff'), name="function key") # p <- p + theme(plot.margin=unit(c(0,0,0,0), "cm")) # p <- p + theme(panel.margin=unit(c(0,0,0,0), "cm")) list_of_ggplots[["to_ui"]] <<- p # strange true; apparently arbitrary key works inserting plot list_of_ggplots } print(list_of_ggplots) #<-- send out ui }) ) ) #<-- end of observe function } #<-- end of brace in shinyserver function ) #<-- end shinyserver function
Comments
Post a Comment