在Shiny中使用ggplot时如何保留绘图布局特征?

时间:2015-05-11 22:21:02

标签: r canvas ggplot2 shiny

当制作一个简单的ggplot时,画布大小将延伸,以使绘图的高度合适,允许变量之间有一些间距。例如,如果我需要在一个图上显示10个变量,那么一切都很好地绘制:

enter image description here

时间做一些闪亮的事!让我们看看我是否可以在tabPanel中嵌入这个图。

我们走了:
enter image description here

看起来很棒,但现在我的横向部分看起来像一堆热狗一样狭窄。水平geom_segments之间没有空格。

这个问题一直困扰着我。

我觉得ggplot有自己的大脑,当谈到帆布尺寸时,这很好,但我不能撬开头骨来适当控制它如何绘制自己。

引用Forrest R. Stevens, “......获得一致画布和所有其他尺寸的关键是直接控制图形输出设备。”

我完全同意他并使用他的出色建议here,但由于我们使用了Shiny,因此无法控制它。

接下来,我尝试使用

中的值
           p <- p + theme(plot.margin=unit(c(0,0,0,0), "cm")
           p <- p + panel.margin=unit(c(0,0,0,0), "cm"))

但是它没有好处;它只是创造了利润并进一步将事物混合在一起,这非常有意义。

我的问题是 - 如何更改我的ggplot,使其以适当的间距显示在Shiny中?

好看的ggplot,不是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
  for (varnum in seq(1:number_of_variables)){ #<-- we are already in the Tab. Now we need to make all three segments for all three variables for this 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 will call the navbarPage function with the arguments in the 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 the observe() block
observe(


lapply(seq(1:number_of_tabs),function(i) output[[paste0("plot",i)]] <- renderPlot({   #<-- lapply will fill up each tab and create one ggplot
  plotindex <<- 0
  list_of_ggplots <<- list() #although we only have one tab, we could easily extend to 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
  for (varnum in seq(1:number_of_variables)){ #<-- We need to make all three segments for all three variables for this 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 # this is strange but true; apparently any arbitrary key works for inserting the plot into the list_of_ggplots
}   
      print(list_of_ggplots)  #<-- to send out to UI
})
)


) #<-- end of observe function
}  #<-- end of brace in shinyserver function
)  #<-- end the shinyserver function

0 个答案:

没有答案