以R为单位绘制月度百分比

以R为单位绘制月度百分比,r,ggplot2,shiny,shinydashboard,R,Ggplot2,Shiny,Shinydashboard,我有一个闪亮的应用程序,在一个闪亮的仪表板上有四个不同的情节。我希望所有的情节都是基于今年的幻灯片。我让它工作得很好。。。但是,我希望其中一个图在x轴上有几个月,而不是像其他图一样有几年,但仍然对年份sliderInput作出反应,并相应地过滤数据帧。我也做到了,但我似乎不知道如何将月份从数字改为x轴上的实际月份名称 这是我想要的(不是像方框图那样只想要x轴): df1<-structure(list(stdate = structure(c(16611, 16611, 16611

我有一个闪亮的应用程序,在一个闪亮的仪表板上有四个不同的情节。我希望所有的情节都是基于今年的幻灯片。我让它工作得很好。。。但是,我希望其中一个图在x轴上有几个月,而不是像其他图一样有几年,但仍然对年份sliderInput作出反应,并相应地过滤数据帧。我也做到了,但我似乎不知道如何将月份从数字改为x轴上的实际月份名称

这是我想要的(不是像方框图那样只想要x轴):

    df1<-structure(list(stdate = structure(c(16611, 16611, 16611, 16615, 
16615, 16615, 14004, 14004, 14004, 16616, 16616, 16616, 16616, 
16616, 16616, 17485, 17483, 16678, 14000, 14000, 14000, 17211, 
17210, 16615, 16611, 16756, 14098, 16674, 16674, 14096), class = "Date"), 
    sttime = structure(c(37800, 37800, 37800, 35100, 35100, 35100, 
    42600, 42600, 42600, 38700, 38700, 38700, 32400, 32400, 32400, 
    35400, 33000, 49800, 34200, 34200, 34200, 37800, 30600, 35100, 
    37800, 37800, 35400, 37800, 32400, 37200), class = c("hms", 
    "difftime"), units = "secs"), locid = c("USGS-01388500", 
    "USGS-01388500", "USGS-01388500", "USGS-01464585", "USGS-01464585", 
    "USGS-01464585", "USGS-01464515", "USGS-01464515", "USGS-01464515", 
    "USGS-01407330", "USGS-01407330", "USGS-01407330", "USGS-01466500", 
    "USGS-01466500", "USGS-01466500", "USGS-01387500", "USGS-01395000", 
    "USGS-01400860", "USGS-01377000", "USGS-01377000", "USGS-01377000", 
    "USGS-01367625", "USGS-01398000", "USGS-01464585", "USGS-01388500", 
    "USGS-01390630", "USGS-01396090", "USGS-01407810", "USGS-01407821", 
    "USGS-01391540"), charnam = c("Chloride", "Total dissolved solids", 
    "Specific conductance", "Chloride", "Total dissolved solids", 
    "Specific conductance", "Specific conductance", "Total dissolved solids", 
    "Chloride", "Chloride", "Total dissolved solids", "Specific conductance", 
    "Chloride", "Total dissolved solids", "Specific conductance", 
    "Specific conductance", "Specific conductance", "Specific conductance", 
    "Specific conductance", "Total dissolved solids", "Chloride", 
    "Specific conductance", "Specific conductance", "Total dissolved solids", 
    "Total dissolved solids", "Total dissolved solids", "Specific conductance", 
    "Specific conductance", "Specific conductance", "Specific conductance"
    ), val = c(109, 294, 525, 31.9, 119, 184, 226, 155, 33, 36.4, 
    155, 203, 3.38, 43, 41, 674, 466, 312, 540, 328, 105, 844, 
    683, 100, 275, 432, 342, 200, 237, 805), valunit = c("mg/l", 
    "mg/l", "uS/cm @25C", "mg/l", "mg/l", "uS/cm @25C", "uS/cm @25C", 
    "mg/l", "mg/l", "mg/l", "mg/l", "uS/cm @25C", "mg/l", "mg/l", 
    "uS/cm @25C", "uS/cm @25C", "uS/cm @25C", "uS/cm @25C", "uS/cm @25C", 
    "mg/l", "mg/l", "uS/cm @25C", "uS/cm @25C", "mg/l", "mg/l", 
    "mg/l", "uS/cm @25C", "uS/cm @25C", "uS/cm @25C", "uS/cm @25C"
    ), LatDeg = c(40.969722, 40.969722, 40.969722, 40.041667, 
    40.041667, 40.041667, 40.176944, 40.176944, 40.176944, 40.319167, 
    40.319167, 40.319167, 39.885, 39.885, 39.885, 41.098056, 
    40.618889, 40.418333, 40.999167, 40.999167, 40.999167, 41.040278, 
    40.473333, 40.041667, 40.969722, NA, 40.860556, 40.2075, 
    40.211667, 40.867778), LongDeg = c(-74.281944, -74.281944, 
    -74.281944, -74.766944, -74.766944, -74.766944, -74.598889, 
    -74.598889, -74.598889, -74.160556, -74.160556, -74.160556, 
    -74.505278, -74.505278, -74.505278, -74.162778, -74.283333, 
    -74.787222, -73.989167, -73.989167, -73.989167, -74.629722, 
    -74.827778, -74.766944, -74.281944, NA, -74.76, -74.328889, 
    -74.295833, -74.094722), HUC14 = c("HUC02030103110020", "HUC02030103110020", 
    "HUC02030103110020", "HUC02040201100030", "HUC02040201100030", 
    "HUC02040201100030", "HUC02040201060020", "HUC02040201060020", 
    "HUC02040201060020", "HUC02030104070070", "HUC02030104070070", 
    "HUC02030104070070", "HUC02040202030070", "HUC02040202030070", 
    "HUC02040202030070", "HUC02030103100030", "HUC02030104050060", 
    "HUC02030105090020", "HUC02030103170060", "HUC02030103170060", 
    "HUC02030103170060", "HUC02020007010010", "HUC02030105030060", 
    "HUC02040201100030", "HUC02030103110020", NA, "HUC02030105010040", 
    "HUC02030104100010", "HUC02030104100010", "HUC02030103140070"
    ), WMA = c(3L, 3L, 3L, 20L, 20L, 20L, 20L, 20L, 20L, 12L, 
    12L, 12L, 19L, 19L, 19L, 3L, 7L, 10L, 5L, 5L, 5L, 2L, 8L, 
    20L, 3L, NA, 8L, 12L, 12L, 4L), year = c(2015, 2015, 2015, 
    2015, 2015, 2015, 2008, 2008, 2008, 2015, 2015, 2015, 2015, 
    2015, 2015, 2017, 2017, 2015, 2008, 2008, 2008, 2017, 2017, 
    2015, 2015, 2015, 2008, 2015, 2015, 2008), month = c(6, 6, 
    6, 6, 6, 6, 5, 5, 5, 6, 6, 6, 6, 6, 6, 11, 11, 8, 5, 5, 5, 
    2, 2, 6, 6, 11, 8, 8, 8, 8)), .Names = c("stdate", "sttime", 
"locid", "charnam", "val", "valunit", "LatDeg", "LongDeg", "HUC14", 
"WMA", "year", "month"), row.names = c(NA, -30L), class = c("tbl_df", 
"tbl", "data.frame"))
parameters<-unique(df1$charnam)


sidebar<-dashboardSidebar(
  selectInput("parameter_input","Select Parameter",
              parameters,selected = "Chloride"),
  sliderInput("date","Select Year Range",
              min = min(df1$year),
              max = max(df1$year),
              value = c(min,max),
              sep = "",
              step = 1)
)
header<-dashboardHeader()
body<- dashboardBody(
          box(plotOutput("plot1")))

ui<-dashboardPage(header=header,
                  sidebar = sidebar,
                  body = body)



server<- function(input,output,session){

  month_percent_over<-reactive({
      df1%>%
        filter(charnam == input$parameter_input)%>%
        mutate(val = as.numeric(val),greater = val >230) %>%
        group_by(month,year) %>%
        summarize(n_greater = sum(greater),percentage=mean(greater)*100)%>%
        filter(year >= input$date[1])%>%
        filter(year <= input$date[2])

  }) 




### Creates % > Standard month plots ###
output$plot1<- renderPlot({
    ggplot(data = month_percent_over(),aes(x=month,y = percentage))+
      geom_point(aes(colour="blue"),size=3,stat = "identity")+
      geom_smooth(method = "lm", se=FALSE,linetype = "dashed",aes(y=percentage,colour="red"))+
      scale_color_manual("",
                         #breaks= c("per"),
                         values = c("blue","red"),
                         labels=c("% Samples > Standard","Trendline"),
                         guide=guide_legend(override.aes=list(linetype=c(0,2), lwd=c(3,0.5))))+
      scale_x_discrete(labels=c("1" = "January", "2" = "Feburary",
                                "3" = "March","4"="April","5"="May",
                                "6"="June","7"="July",
                                "8"="August","9"="September","10"="October",
                                "11"="November","12"="December"))


})
}
shinyApp(ui,server)

样本数据:

    df1<-structure(list(stdate = structure(c(16611, 16611, 16611, 16615, 
16615, 16615, 14004, 14004, 14004, 16616, 16616, 16616, 16616, 
16616, 16616, 17485, 17483, 16678, 14000, 14000, 14000, 17211, 
17210, 16615, 16611, 16756, 14098, 16674, 16674, 14096), class = "Date"), 
    sttime = structure(c(37800, 37800, 37800, 35100, 35100, 35100, 
    42600, 42600, 42600, 38700, 38700, 38700, 32400, 32400, 32400, 
    35400, 33000, 49800, 34200, 34200, 34200, 37800, 30600, 35100, 
    37800, 37800, 35400, 37800, 32400, 37200), class = c("hms", 
    "difftime"), units = "secs"), locid = c("USGS-01388500", 
    "USGS-01388500", "USGS-01388500", "USGS-01464585", "USGS-01464585", 
    "USGS-01464585", "USGS-01464515", "USGS-01464515", "USGS-01464515", 
    "USGS-01407330", "USGS-01407330", "USGS-01407330", "USGS-01466500", 
    "USGS-01466500", "USGS-01466500", "USGS-01387500", "USGS-01395000", 
    "USGS-01400860", "USGS-01377000", "USGS-01377000", "USGS-01377000", 
    "USGS-01367625", "USGS-01398000", "USGS-01464585", "USGS-01388500", 
    "USGS-01390630", "USGS-01396090", "USGS-01407810", "USGS-01407821", 
    "USGS-01391540"), charnam = c("Chloride", "Total dissolved solids", 
    "Specific conductance", "Chloride", "Total dissolved solids", 
    "Specific conductance", "Specific conductance", "Total dissolved solids", 
    "Chloride", "Chloride", "Total dissolved solids", "Specific conductance", 
    "Chloride", "Total dissolved solids", "Specific conductance", 
    "Specific conductance", "Specific conductance", "Specific conductance", 
    "Specific conductance", "Total dissolved solids", "Chloride", 
    "Specific conductance", "Specific conductance", "Total dissolved solids", 
    "Total dissolved solids", "Total dissolved solids", "Specific conductance", 
    "Specific conductance", "Specific conductance", "Specific conductance"
    ), val = c(109, 294, 525, 31.9, 119, 184, 226, 155, 33, 36.4, 
    155, 203, 3.38, 43, 41, 674, 466, 312, 540, 328, 105, 844, 
    683, 100, 275, 432, 342, 200, 237, 805), valunit = c("mg/l", 
    "mg/l", "uS/cm @25C", "mg/l", "mg/l", "uS/cm @25C", "uS/cm @25C", 
    "mg/l", "mg/l", "mg/l", "mg/l", "uS/cm @25C", "mg/l", "mg/l", 
    "uS/cm @25C", "uS/cm @25C", "uS/cm @25C", "uS/cm @25C", "uS/cm @25C", 
    "mg/l", "mg/l", "uS/cm @25C", "uS/cm @25C", "mg/l", "mg/l", 
    "mg/l", "uS/cm @25C", "uS/cm @25C", "uS/cm @25C", "uS/cm @25C"
    ), LatDeg = c(40.969722, 40.969722, 40.969722, 40.041667, 
    40.041667, 40.041667, 40.176944, 40.176944, 40.176944, 40.319167, 
    40.319167, 40.319167, 39.885, 39.885, 39.885, 41.098056, 
    40.618889, 40.418333, 40.999167, 40.999167, 40.999167, 41.040278, 
    40.473333, 40.041667, 40.969722, NA, 40.860556, 40.2075, 
    40.211667, 40.867778), LongDeg = c(-74.281944, -74.281944, 
    -74.281944, -74.766944, -74.766944, -74.766944, -74.598889, 
    -74.598889, -74.598889, -74.160556, -74.160556, -74.160556, 
    -74.505278, -74.505278, -74.505278, -74.162778, -74.283333, 
    -74.787222, -73.989167, -73.989167, -73.989167, -74.629722, 
    -74.827778, -74.766944, -74.281944, NA, -74.76, -74.328889, 
    -74.295833, -74.094722), HUC14 = c("HUC02030103110020", "HUC02030103110020", 
    "HUC02030103110020", "HUC02040201100030", "HUC02040201100030", 
    "HUC02040201100030", "HUC02040201060020", "HUC02040201060020", 
    "HUC02040201060020", "HUC02030104070070", "HUC02030104070070", 
    "HUC02030104070070", "HUC02040202030070", "HUC02040202030070", 
    "HUC02040202030070", "HUC02030103100030", "HUC02030104050060", 
    "HUC02030105090020", "HUC02030103170060", "HUC02030103170060", 
    "HUC02030103170060", "HUC02020007010010", "HUC02030105030060", 
    "HUC02040201100030", "HUC02030103110020", NA, "HUC02030105010040", 
    "HUC02030104100010", "HUC02030104100010", "HUC02030103140070"
    ), WMA = c(3L, 3L, 3L, 20L, 20L, 20L, 20L, 20L, 20L, 12L, 
    12L, 12L, 19L, 19L, 19L, 3L, 7L, 10L, 5L, 5L, 5L, 2L, 8L, 
    20L, 3L, NA, 8L, 12L, 12L, 4L), year = c(2015, 2015, 2015, 
    2015, 2015, 2015, 2008, 2008, 2008, 2015, 2015, 2015, 2015, 
    2015, 2015, 2017, 2017, 2015, 2008, 2008, 2008, 2017, 2017, 
    2015, 2015, 2015, 2008, 2015, 2015, 2008), month = c(6, 6, 
    6, 6, 6, 6, 5, 5, 5, 6, 6, 6, 6, 6, 6, 11, 11, 8, 5, 5, 5, 
    2, 2, 6, 6, 11, 8, 8, 8, 8)), .Names = c("stdate", "sttime", 
"locid", "charnam", "val", "valunit", "LatDeg", "LongDeg", "HUC14", 
"WMA", "year", "month"), row.names = c(NA, -30L), class = c("tbl_df", 
"tbl", "data.frame"))
parameters<-unique(df1$charnam)


sidebar<-dashboardSidebar(
  selectInput("parameter_input","Select Parameter",
              parameters,selected = "Chloride"),
  sliderInput("date","Select Year Range",
              min = min(df1$year),
              max = max(df1$year),
              value = c(min,max),
              sep = "",
              step = 1)
)
header<-dashboardHeader()
body<- dashboardBody(
          box(plotOutput("plot1")))

ui<-dashboardPage(header=header,
                  sidebar = sidebar,
                  body = body)



server<- function(input,output,session){

  month_percent_over<-reactive({
      df1%>%
        filter(charnam == input$parameter_input)%>%
        mutate(val = as.numeric(val),greater = val >230) %>%
        group_by(month,year) %>%
        summarize(n_greater = sum(greater),percentage=mean(greater)*100)%>%
        filter(year >= input$date[1])%>%
        filter(year <= input$date[2])

  }) 




### Creates % > Standard month plots ###
output$plot1<- renderPlot({
    ggplot(data = month_percent_over(),aes(x=month,y = percentage))+
      geom_point(aes(colour="blue"),size=3,stat = "identity")+
      geom_smooth(method = "lm", se=FALSE,linetype = "dashed",aes(y=percentage,colour="red"))+
      scale_color_manual("",
                         #breaks= c("per"),
                         values = c("blue","red"),
                         labels=c("% Samples > Standard","Trendline"),
                         guide=guide_legend(override.aes=list(linetype=c(0,2), lwd=c(3,0.5))))+
      scale_x_discrete(labels=c("1" = "January", "2" = "Feburary",
                                "3" = "March","4"="April","5"="May",
                                "6"="June","7"="July",
                                "8"="August","9"="September","10"="October",
                                "11"="November","12"="December"))


})
}
shinyApp(ui,server)
df1%
过滤器(年标准月图)###

输出$plot1FYI,这不是正确的二月拼写。无论如何,请尝试使用格式字符串格式化日期值,以便完整拼写月份。@42-哎哟,打字错误..感谢您指出这一点。由于我是R的新手,我不明白您的意思。标签的参数可能是“格式(stdate,%B)”没有测试,因为我的手机上没有R。我应该把它放在哪里?好像你在Y轴上画“val”。如果你总结一下,“val”数据列已删除。您可能需要在该列存在的情况下创建另一个反应数据框。然后,您需要在ggplot中指定该数据框作为数据来绘制箱线图。还需要执行x=as.factor(as.character(month))将数值转换为箱线图的系数