我有一个非常简单的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)
答案 0 :(得分:2)
由于state_choropleth()
会返回ggplot
个对象,因此您可以使用scale_fill_gradient()
。您可以使用range(df$value)
获取所有数据的范围。
如果renderPlot()
返回:
print(p + scale_fill_gradient(limits = range(df$value))
它应该完成这项工作。