2

我有一个非常简单的 Shiny 应用程序,代码位于问题的底部。

该应用程序允许我们查看 2000 年和 2001 年。在这两种情况下,加利福尼亚州都是最黑暗的州,因为它的值最高(分别为 500 和 1000)。

我的问题是我想设置颜色的比例以在两年内固定。请注意,加利福尼亚州第一年的颜色为深蓝色(对应于值 1000)。

在此处输入图像描述

现在请注意,加利福尼亚州第二年有完全相同的深蓝色(对应于 500 的值)。

在此处输入图像描述

在查看等值线时,很容易忽略这样一个事实,即多年来价值下降了一半(当然,其他州也以不同的方式发生这种情况)。我想要一种方法来修复跨图的范围。我怎样才能做到这一点?

df <- structure(list(region = c("alabama", "alabama", "alaska", "alaska", 
                                "arizona", "arizona", "arkansas", "arkansas", "california", "california", 
                                "colorado", "colorado", "connecticut", "connecticut", "delaware", 
                                "delaware", "district of columbia", "district of columbia", "florida", 
                                "florida", "georgia", "georgia", "hawaii", "hawaii", "idaho", 
                                "idaho", "illinois", "illinois", "indiana", "indiana", "iowa", 
                                "iowa", "kansas", "kansas", "kentucky", "kentucky", "louisiana", 
                                "louisiana", "maine", "maine", "maryland", "maryland", "massachusetts", 
                                "massachusetts", "michigan", "michigan", "minnesota", "minnesota", 
                                "mississippi", "mississippi", "missouri", "missouri", "montana", 
                                "montana", "nebraska", "nebraska", "nevada", "nevada", "new hampshire", 
                                "new hampshire", "new jersey", "new jersey", "new mexico", "new mexico", 
                                "new york", "new york", "north carolina", "north carolina", "north dakota", 
                                "north dakota", "ohio", "ohio", "oklahoma", "oklahoma", "oregon", 
                                "oregon", "pennsylvania", "pennsylvania", "rhode island", "rhode island", 
                                "south carolina", "south carolina", "south dakota", "south dakota", 
                                "tennessee", "tennessee", "texas", "texas", "utah", "utah", "vermont", 
                                "vermont", "virginia", "virginia", "washington", "washington", 
                                "west virginia", "west virginia", "wisconsin", "wisconsin", "wyoming", 
                                "wyoming"), date = c("2000", "2001", "2000", "2001", "2000", 
                                                     "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000", 
                                                     "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000", 
                                                     "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000", 
                                                     "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000", 
                                                     "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000", 
                                                     "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000", 
                                                     "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000", 
                                                     "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000", 
                                                     "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000", 
                                                     "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000", 
                                                     "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000", 
                                                     "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000", 
                                                     "2001"), value = c(19, 11, 83, 80, 87, 79, 87, 45, 1000, 500, 
                                                                        89, 163, 41, 101, 53, 3, 39, 55, 500, 347, 71, 89, 37, 43, 23, 
                                                                        41, 243, 175, 271, 215, 75, 3, 22, 33, 11, 15, 5, 55, 18, 60, 
                                                                        17, 79, 59, 61, 193, 165, 11, 65, 237, 299, 373, 233, 17, 7, 
                                                                        69, 21, 433, 81, 79, 63, 127, 95, 5, 111, 341, 373, 53, 53, 
                                                                        35, 63, 157, 81, 75, 35, 57, 23, 445, 511, 17, 15, 21, 79, 118, 
                                                                        88, 153, 167, 68, 471, 1, 83, 18, 8, 55, 21, 95, 35, 33, 47, 
                                                                        13, 23, 7, 17)), .Names = c("region", "date", "value"), row.names = c(NA, 
                                                                                                                                              -102L), class = c("tbl_df", "tbl", "data.frame"))


## app.R ##

library(dplyr)
library(choroplethr)
library(choroplethrMaps)
library(lubridate)

server <- function(input, output) {



  output$distPlot <- renderPlot({

    df1 <- filter(df, date==as.character(input$year))



    p <- state_choropleth(df1,
                          title      = "Population Estimates",
                          legend     = "Population",
                          num_colors = 1
    )
    print(p)

  })
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("year", "Year:", min = 2000, max = 2001, step=1, value = 2000, sep = "")
    ),
    mainPanel(plotOutput("distPlot"))
  )
)

shinyApp(ui = ui, server = server)
4

1 回答 1

2

由于state_choropleth()返回一个ggplot对象,您可以使用scale_fill_gradient(). 您可以使用 获取所有数据的范围range(df$value)

所以如果renderPlot()返回:

print(p + scale_fill_gradient(limits = range(df$value))

它应该完成这项工作。

于 2016-02-08T08:11:50.330 回答