由ggplot2中的scale_fill_gradient()生成的图例返回空白的图例。我怎么能填充这个?

时间:2016-12-01 17:45:10

标签: r ggplot2 ggmap

具体来说,我正在尝试使用ggplot2geom_map中创建状态热图(请参阅下面的代码)。

我可以轻松地生成以下内容:

enter image description here

如您所见,右侧的图例不会产生任何颜色渐变。我不知道问题是什么,我希望有人可以帮助我!

我在下面创建了一个具有相同问题的虚拟示例。

提前致谢!

library(ggplot2)
library(data.table)
library(maps)
states_map <- map_data("state")

states <-data.table(
  stateAbb=as.factor(c("AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA",
                       "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME",
                       "MI", "MN", "MO", "MS",  "MT", "NC", "ND", "NE", "NH", "NJ", "NM",
                       "NV", "NY", "OH", "OK", "OR", "PA", "PR", "RI", "SC", "SD", "TN",
                       "TX", "UT", "VA", "VT", "WA", "WI", "WV", "WY")),
  stateName=as.factor(c("alaska","alabama","arkansas","arizona","california","colorado",
                        "connecticut","district of columbia","delaware","florida","georgia",
                        "hawaii","iowa","idaho","illinois","indiana","kansas","kentucky",
                        "louisiana","massachusetts","maryland","maine","michigan","minnesota",
                        "missouri","mississippi","montana","north carolina","north dakota",
                        "nebraska","new hampshire","new jersey","new mexico","nevada",
                        "new york","ohio","oklahoma","oregon","pennsylvania","puerto rico",
                        "rhode island","south carolina","south dakota","tennessee","texas",
                        "utah","virginia","vermont","washington","wisconsin",
                        "west virginia","wyoming"))
)

states$value <- sample(1:10, nrow(states), TRUE)

plot1 <- ggplot(states, aes(map_id = stateName, fill = value)) + 
  geom_map(map = states_map, color = "black") +
  scale_fill_gradient(name = "freq^.5", low = "yellow", high = "red") +
  expand_limits(x = states_map$long, y = states_map$lat) +
  coord_map("polyconic")

编辑:修复了丢失的库导入

0 个答案:

没有答案