R ggplot-won';我没有一张美国地图

R ggplot-won';我没有一张美国地图,r,ggplot2,rstudio,heatmap,R,Ggplot2,Rstudio,Heatmap,我正试图制作一张美国的热图,代码“起作用”,但数据不会填充到美国地图中——只显示州名——我也想填充,但美国的实际地图是最重要的 这是我的密码: #读入我的数据 rawdata_path以下代码将加载并绘制美国地图,其中包含“rawdata”中的坐标和文本: # Load the data rawdata_path <- 'C:/data.xlsx' rawdata <- readxl::read_excel(rawdata_path, sheet = 1, col_names = T

我正试图制作一张美国的热图,代码“起作用”,但数据不会填充到美国地图中——只显示州名——我也想填充,但美国的实际地图是最重要的

这是我的密码: #读入我的数据
rawdata_path以下代码将加载并绘制美国地图,其中包含“rawdata”中的坐标和文本:

# Load the data
rawdata_path <- 'C:/data.xlsx'
rawdata <- readxl::read_excel(rawdata_path, sheet = 1, col_names = TRUE)

#clean up the data
rawdata$Lattitude <- as.numeric(rawdata$Lattitude)
rawdata$Longitude <- as.numeric(rawdata$Longitude)

library(ggplot2)
library(maps)
# Load the map of the United State
all_states <- map_data("state")

ggplot() +
  geom_polygon( data=all_states, aes(x=long, y=lat, group = group), 
            colour="white", fill="blue" ) +
  geom_point(data=rawdata,
         aes(x=Longitude, y=Lattitude, colour='red', size=Count),
         alpha=I(0.5)) +
  geom_text(data = rawdata, aes(x=Longitude, y = Lattitude, label = State)) +
  scale_fill_gradientn(colours = rev(heat.colors(10)), na.value = "grey90")
#加载数据

rawdata_path如果您试图使用此数据生成choropleth贴图,则使用
choroplethr
非常简单。首先,您需要将states列重命名为“region”,将count列重命名为“value”

因此,输入数据如下所示:

df <- structure(list(region = structure(c(1L, 2L, 5L, 6L, 9L, 12L, 
15L, 19L, 20L, 21L, 22L, 23L, 11L, 8L, 18L, 14L, 4L, 3L, 7L, 
10L, 16L, 13L, 17L), .Label = c("arizona", "arkansas", "california", 
"florida", "georgia", "hawaii", "illinois", "indiana", "kansas", 
"kentucky", "louisiana", "maryland", "massachusetts", "michigan", 
"missouri", "new york", "north carolina", "oklahoma", "oregon", 
"pennsylvania", "rhode island", "tennessee", "texas"), class = "factor"), 
value = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2.1, 2.3, 2.7, 
3.8, 4.9, 5.1, 5.3, 5.7, 6.3, 9.5, 10), Group = 1:23), .Names = c("region", 
"value", "Group"), class = "data.frame", row.names = c(NA, -23L
))

df谢谢!!我使用的数据是上面的数据框。这是我正在阅读的名为heatdata的电子表格。我复制了你的Excel文件并更新了答案。这太棒了,正是我要找的!非常感谢你!我肯定会更多地利用这个软件包。一个问题-我不明白“L”在数据帧的结构部分来自哪里。你是如何得到这些数字的?为什么它们是按顺序排列的?@DanaOrinick这是
dput
函数的输出,我试图复制你发布的数据的重要部分。L后缀表示一个整数。就脚本而言,它们并不真正相关。您只需更改列名,并确保状态为小写。见:
# Load the data
rawdata_path <- 'C:/data.xlsx'
rawdata <- readxl::read_excel(rawdata_path, sheet = 1, col_names = TRUE)

#clean up the data
rawdata$Lattitude <- as.numeric(rawdata$Lattitude)
rawdata$Longitude <- as.numeric(rawdata$Longitude)

library(ggplot2)
library(maps)
# Load the map of the United State
all_states <- map_data("state")

ggplot() +
  geom_polygon( data=all_states, aes(x=long, y=lat, group = group), 
            colour="white", fill="blue" ) +
  geom_point(data=rawdata,
         aes(x=Longitude, y=Lattitude, colour='red', size=Count),
         alpha=I(0.5)) +
  geom_text(data = rawdata, aes(x=Longitude, y = Lattitude, label = State)) +
  scale_fill_gradientn(colours = rev(heat.colors(10)), na.value = "grey90")
df <- structure(list(region = structure(c(1L, 2L, 5L, 6L, 9L, 12L, 
15L, 19L, 20L, 21L, 22L, 23L, 11L, 8L, 18L, 14L, 4L, 3L, 7L, 
10L, 16L, 13L, 17L), .Label = c("arizona", "arkansas", "california", 
"florida", "georgia", "hawaii", "illinois", "indiana", "kansas", 
"kentucky", "louisiana", "maryland", "massachusetts", "michigan", 
"missouri", "new york", "north carolina", "oklahoma", "oregon", 
"pennsylvania", "rhode island", "tennessee", "texas"), class = "factor"), 
value = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2.1, 2.3, 2.7, 
3.8, 4.9, 5.1, 5.3, 5.7, 6.3, 9.5, 10), Group = 1:23), .Names = c("region", 
"value", "Group"), class = "data.frame", row.names = c(NA, -23L
))
library(ggplot2)
library(choroplethr)

choro <- state_choropleth(df) + scale_fill_brewer(palette = "Reds")
choro