我想製作美國的熱圖,代碼「有效」,但數據不會填充到美國地圖中 - 只有州的名稱顯示 - 我也想填充,但美國的實際地圖是最重要的。ggplot不會填充美國地圖
這裏是我的代碼: #閱讀在我的數據 rawdata_path < - C( 「〜/ R/heatdata.xlsx」)
# Import the data into RStudio:
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)
#plot the data
library(ggplot2)
library(maps)
ggplot(rawdata, aes(x=Longitude, y = Lattitude, group = rawdata$State))+
geom_polygon(aes(fill=FinalCount))+
geom_path()+
geom_text(data = rawdata, aes(x=Longitude, y = Lattitude, label = State))+
scale_fill_gradientn(colours = rev(heat.colors(10)), na.value = "grey90")+
coord_map()
這是數據幀的頭叫什麼, RAWDATA的樣子,我想狀態由Count柱填充 - 1爲最輕,10爲暗紅色:
State Count Group Lattitude Longitude Rev
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
arizona 1.0 1 33.50000 -112.05000 0.0
arkansas 1.0 2 36.36000 -94.20000 0.0
georgia 1.0 3 33.82000 -84.32000 0.0
hawaii 1.0 4 21.30000 -157.85000 0.0
kansas 1.0 5 38.97167 -95.23525 0.0
maryland 1.0 6 38.98000 -77.08000 0.0
missouri 1.0 7 39.09000 -94.58000 0.0
oregon 1.0 8 45.51000 -122.68000 0.0
pennsylvania 1.0 9 40.43000 -79.97000 0.0
rhode island 1.0 10 41.82000 -71.41000 0.0
tennessee 1.0 11 35.10000 -90.00000 0.0
texas 1.0 12 29.76043 -95.36980 0.0
louisiana 2.1 13 30.44000 -91.12000 209250.0
indiana 2.3 14 38.30000 -85.72000 231605.9
oklahoma 2.7 15 35.22000 -97.34000 274377.9
michigan 3.8 16 42.73000 -84.48000 381528.5
florida 4.9 17 30.43826 -84.28073 498338.5
california 5.1 18 34.06000 -118.24000 511472.0
illinois 5.3 19 41.83000 -87.68000 537913.5
kentucky 5.7 20 38.22000 -85.74000 562077.0
new york 6.3 21 40.75000 -73.99000 630642.8
massachusetts 9.5 22 42.33038 -71.16619 908952.0
north carolina 10.0 23 36.07000 -79.82000 1571923.8
誰能幫助我在這?
謝謝!我使用的數據是上面的數據框。這是我正在閱讀的名爲heatdata的電子表格。 –
我複製了你的Excel文件並更新了答案。 –