在qqplot和Shiny中命名传奇

时间:2017-03-11 11:26:45

标签: r ggplot2 shiny

我试图用Shiny命名一个ggplot的传说。问题是,由于用户可以关闭并在线上更改每行的名称,因此图例的名字与第一条可见线相关联。

我需要一种方法来重命名传奇。

数据:

  

data_raw< - 结构(列表(组1)=结构(c(1L,2L,3L,4L,5L,6L,7L,1L,2L,3L,4L,5L,7L,1L,2L,3L) ,4L,5L,6L,7L),                           .Label = c(" 2016-07-01"," 2016-08-01"," 2016-09-01"," 2016- 10-01"," 2016-11-01"," 2016-12-01"," 2017-01-01"),                           class =" factor"),Group.2 = c(101,101,101,101,101,101,101,103,103,103,103,103,103,105,105,105, 105,105,105,105),                           x = c(" 57976"," 42933"," 49731"," 46808"," 48938",& #34; 41937"," 41932"," 48900"," 37625"," 57700"," 41700&# 34;," 57600"," 48800"," 46102"," 35796"," 42490",&# 34; 46755"," 46757"," 46810"," 45232")),                           .Names = c(" Group.1"," Group.2"," x"),                           row.names = c(NA,20L),class =" data.frame")

守则:

library(shiny)
library(ggplot2)

ui <- fluidPage(

    titlePanel("Legends"),

    sidebarLayout(position = "left",
                  sidebarPanel(checkboxGroupInput("checkGroup", 
                                                  label = h3("Postnumber"), 
                                                  choices = list( 
                                                          "101" = 101, 
                                                          "103" = 103, 
                                                          "105" = 105),
                                                    selected = "101"
                                                 )),
             mainPanel("main panel", 
                    plotOutput('myplot'))
    ))

server <- function(input, output) { 

    output$myplot <- renderPlot({

    grouped_data <- data_raw[data_raw$Group.2 %in% input$checkGroup,]

    p <- ggplot(grouped_data, aes(Group.1, x, group = Group.2)) +
            scale_colour_manual(values=c("#999999", "#E69F00", "#56B4E9"), 
                                labels=c("postnr 101", "postnr 103", "postnr 105"))
    p + geom_line() +
            theme(axis.title.y=element_text(margin=margin(0,20,0,0))) +
            geom_line(data = grouped_data, aes(colour = factor(Group.2)))

})
    }
shinyApp(ui = ui, server = server) # this launches your app

我是按原样保留图例(没有任何重命名),它使用checkboxGroupInput下选项列表中的正确名称,但我需要能够重命名它。

1 个答案:

答案 0 :(得分:1)

我修改了你的服务器功能:

server <- function(input, output) { 
    output$myplot <- renderPlot({

    grouped_data <- data_raw[data_raw$Group.2 %in% input$checkGroup,]

    grouped_data$Group.2 <- paste("postnr ", grouped_data$Group.2, sep = "")

    grouped_data$Group.2 <- factor(grouped_data$Group.2,
                                   levels = c("postnr 101", "postnr 103", "postnr 105"),
                                   ordered = TRUE) 

    ggplot(grouped_data,
                aes_string(x = "Group.1", y = "x", group = "Group.2",
                           colour = "Group.2")) +
      geom_line() + 
      scale_colour_manual(values=c("#999999", "#E69F00", "#56B4E9"),
                          guide = guide_legend(title = "Postnr"), drop = FALSE) +
      theme(axis.title.y=element_text(margin=margin(0,20,0,0)))

  }) }

标签项目(101,103,105)使用以下方式重命名为(&#34; postnr 101&#34;等):

    grouped_data$Group.2 <- paste("postnr ", grouped_data$Group.2, sep = "") 

然后将它们转换为有序因子(以便每个项目,例如&#34; postnr 101&#34;总是得到相同的颜色):

    grouped_data$Group.2 <- factor(grouped_data$Group.2,
                                   levels = c("postnr 101", "postnr 103", "postnr 105"),
                                   ordered = TRUE) 

最后,我修改了ggplot以使用aes_string(因为在data.frame中有一个名为x的变量)并使用guide = guide_legend(title = "Postnr")为图例赋予标题。 drop = FALSE可确保一致的颜色(因为未使用的因子级别不会被删除)。

如果未选中复选框,则会出现错误,因此在生成绘图之前,您需要检查grouped_data data.frame是否至少有一行。如果nrow(grouped_data) == 0,则返回NULL(无情节)。