在Shiny app中添加centile叠加到ggplot

时间:2017-04-21 21:06:46

标签: r ggplot2 shiny

我有一组在我的Shiny应用程序之外运行良好的代码,但是当我在应用程序中运行代码时,它将只绘制ggplot或centile部分。我知道这是因为我可以将centile部分注释掉并看到ggplot部分,但是如果我只是运行下面的代码,我只会看到我的Shiny应用程序中的centile。关于如何让他们在我的应用程序中一起策划的任何想法?谢谢!

库(有光泽) 库(GGPLOT2) 库(gamlss)

阅读数据

dat< - read.csv(' /Users/katiecolborn/Documents/ACCORDS/PRCshiny/fittedLMy90.csv') all< - read.csv(' /Users/katiecolborn/Documents/ACCORDS/PRCshiny/data_083116.csv')

为生成PRC

的应用程序定义UI

ui< - fluidPage(

# Application title
titlePanel("Personalized Reference Chart for Knew Flexion ROM"),

# Enter patient age with numericInput 
numericInput(inputId="age", label="Age", value=65, min=1, max=110),

# Enter patient baseline knee ROM with numericInput 
numericInput(inputId="bROM", label="Pre-op knee ROM", value=90, min=0, max=180),

# Show a plot of the generated knee arom PRC
  mainPanel(
     plotOutput("aromPRC")
  )

定义绘制直方图所需的服务器逻辑

server< - function(input,output){

输出$ aromPRC< - renderPlot({

  try <- 79.55 + input$age*0.25 + input$bROM*0.16

  dat$diff <- abs(dat$lmFit-try)
  dat1 <- dat[order(dat$diff),]
  dat2 <- dat1[1:50,"id"]

  all1 <- subset(all, patient_id %in% dat2)
  all1 <- all1[order(all1$patient_id),]
  all2 <- subset(all1, is.na(knee_arom_flexion)==F & time_elapsed>0)
  all3 <- all2[c("patient_id", "knee_arom_flexion", "time_elapsed")]

  plm<-gamlss(knee_arom_flexion~pb(time_elapsed),
              sigma.formula = ~pb(time_elapsed),
              data=na.omit(all3), family=NO)

  # factor for gridlines
  c_fle<-c(seq(40,140, by=10)) # MDC90 for flexion = 9.6 [Stratford 2010 PT can]
  c_vert<-c(seq(0,120, by=5))

  ggplot(data = all3, aes(x = time_elapsed, y = knee_arom_flexion)) +       
    geom_line(aes(group = patient_id), colour="light gray") + geom_point(colour="light gray")+
    ylim(40,150) +xlim(0,120)+theme_bw() + theme (axis.title.x=element_blank(),
                                                  axis.text.x=element_blank(),
                                                  axis.ticks.x=element_blank(),
                                                  axis.title.y=element_blank(),
                                                  axis.text.y=element_blank(),
                                                  axis.ticks.y=element_blank(),
                                                  panel.border=element_blank(),
                                                  panel.grid=element_blank(),
                                                  plot.margin = unit(c(2.0, .9, 2.45, 1.75), "cm"))

  par(new=T)
  centiles(plm, xvar=all3$time_elapsed, cent=c(10,25,50,75,90), 
           ylab= "Knee Flexion AROM (degrees)", xlab="Days following surgery", 
           xleg=100, yleg=90, col.centiles = c(2,6,1,6,2), xlim=range(0,120),
           ylim=range(40,150),
          lty.centiles = c(2,3,1,3,2),
           lwd.centiles =c(2,2,2.5,2,2), points=F)

}) }

运行应用程序

shinyApp(ui = ui,server = server)

0 个答案:

没有答案