设置ggplot中的因子水平,并根据r markdown中的滑动条更改值

时间:2016-04-14 20:10:19

标签: r ggplot2 shiny

我有一个闪亮的应用程序,它根据由滑动条控制的日期顺序显示点和路径。 ggplot颜色美学被映射到表示季节的因素(冬季,夏季,没有)。如果所有点都绘制在shiny之外且没有滑块,则红点为夏季,蓝点为冬季,白点为无。

然而,在shiny范围内,随着移动滑块并添加点数,颜色会发生变化,可能是因为随着季节因素的更多级别的添加,因子级别也会发生变化。

是否可以将颜色图例设置为具有季节因子的所有级别,无论显示多少个点,还可以指定颜色以使红色=夏天,蓝色=冬天,白色=无?换句话说,当shiny应用程序打开时(仅显示两个点和单个路径),我希望在图例中显示的所有级别的季节都不会随着点的添加而改变。

数据如下所示,并使用dupt包含在下面。

  AcquisitionStartTime GPSUTMNorthing GPSUTMEasting             PosiGMT Season
1  2013.02.09 00:00:00        4947787        600201 2013-02-09 00:00:00 winter
2  2013.02.09 06:00:00        4947945        600910 2013-02-09 06:00:00 winter
5  2013.02.10 00:00:00        4947957        600911 2013-02-10 00:00:00 winter
6  2013.02.10 06:00:00        4947954        600907 2013-02-10 06:00:00 summer
8  2013.02.10 18:00:00        4947797        601052 2013-02-10 18:00:00 summer
9  2013.02.11 00:00:00        4947835        601038 2013-02-11 00:00:00 summer

我的可重现shiny应用程序如下。任何建议都将不胜感激。

library(ggplot2)
library(shiny)


shinyApp(
  ui <- fluidPage(
    titlePanel("GPS Data Summary"),
    sliderInput(inputId = "Date",
                label = "Sequance of Observations",  
                min = as.Date(min(dat$PosiGMT)), max = as.Date(max(dat$PosiGMT)),
                value = as.Date(min(dat$PosiGMT))),
    #animate = animationOptions(interval=75, loop=T)),
    plotOutput("PointPlot")
  ),

  server <- function(input, output) {
    output$PointPlot <- renderPlot({
      p <- ggplot(dat[as.Date(dat$PosiGMT) <= input$Date ,], 
                  (aes(x = GPSUTMEasting, y = GPSUTMNorthing ))) + 
        geom_point(aes(color = Season), size = 2) + geom_path(aes(color = Season)) + 
        scale_color_manual(values = c("white" ,"red","blue"), labels = c("Spring/Fall", "Summer","Winter"))+
                xlim( min(dat$GPSUTMEasting), max(dat$GPSUTMEasting))+
        ylim( min(dat$GPSUTMNorthing), max(dat$GPSUTMNorthing))
      print(p)
    })
  }

)

数据

dat <- structure(list(AcquisitionStartTime = structure(c(1L, 2L, 5L, 
                                                  6L, 8L, 9L, 10L, 12L, 13L, 14L, 15L), .Label = c("2013.02.09 00:00:00", 
                                                                                                   "2013.02.09 06:00:00", "2013.02.09 12:00:00", "2013.02.09 18:00:00", 
                                                                                                   "2013.02.10 00:00:00", "2013.02.10 06:00:00", "2013.02.10 12:00:00", 
                                                                                                   "2013.02.10 18:00:00", "2013.02.11 00:00:00", "2013.02.11 06:00:00", 
                                                                                                   "2013.02.11 12:00:00", "2013.02.11 18:00:00", "2013.02.12 00:00:00", 
                                                                                                   "2013.02.12 06:00:00", "2013.02.12 12:00:00"), class = "factor"), 
               GPSUTMNorthing = c(4947787L, 4947945L, 4947957L, 4947954L, 
                                  4947797L, 4947835L, 4947825L, 4947784L, 4947842L, 4947839L, 
                                  4947789L), GPSUTMEasting = c(600201L, 600910L, 600911L, 600907L, 
                                                               601052L, 601038L, 601031L, 601066L, 600998L, 600995L, 601058L
                                  ), PosiGMT = structure(c(1360393200, 1360414800, 1360479600, 
                                                           1360501200, 1360544400, 1360566000, 1360587600, 1360630800, 
                                                           1360652400, 1360674000, 1360695600), class = c("POSIXct", 
                                                                                                          "POSIXt"), tzone = ""), Season = c("winter", "winter", "winter", 
                                                                                                                                             "summer", "summer", "summer", "None", "None", "None", "None", 
                                                                                                                                             "None")), .Names = c("AcquisitionStartTime", "GPSUTMNorthing", 
                                                                                                                                                                  "GPSUTMEasting", "PosiGMT", "Season"), row.names = c(1L, 2L, 
                                                                                                                                                                                                                       5L, 6L, 8L, 9L, 10L, 12L, 13L, 14L, 15L), class = "data.frame")

0 个答案:

没有答案