我正在尝试制作地图,然后在不同点上放置饼图。我正在尝试按照此指南制作地图:https://www.r-spatial.org/r/2018/10/25/ggplot2-sf-2.html
我的地图效果很好,我喜欢一个,但是现在我正尝试使用散点图软件包向其中添加饼图,并且不断出现错误“离散值提供给连续比例尺”,我认为这是因为不同我正在使用的包装使用不同的填充方法来绘制东西。有谁知道解决此问题的方法?这是我正在使用的地图的代码,效果很好:
library("ggplot2")
theme_set(theme_bw())
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")
latitude=c(37.5,37.5,37.3)
longitude=c(-89.2,-88.9,-88.9)
pos=c(4,6,40)
neg=c(3,28,138)
ILdata=data.frame(latitude,longitude,pos,neg)
# This makes the map base layer for the world
world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)
# This uses the library maps to make files for the states
library("maps")
states <- st_as_sf(map("state", plot = FALSE, fill = TRUE))
head(states)
states <- cbind(states, st_coordinates(st_centroid(states)))
# This uses the library 'maps' to extract the counties for each state
counties <- st_as_sf(map("county", plot = FALSE, fill = TRUE))
counties$area <- as.numeric(st_area(counties))
# This takes a subset of counties, you can specify a specific state here
countiesil <- subset(counties, grepl("illinois", counties$ID))
head(counties)
# This is a way to manually add in cities to the map, just type in the name and coordinates
ilcities <- data.frame(state = rep("Illinois", 2), city = c("Chicago", "Carbondale"), lat = c(41.883255, 37.728002), lng = c(-87.631034, -89.215887))
# This formats the cities correctly
(ilcities <- st_as_sf(ilcities, coords = c("lng", "lat"), remove = FALSE,
crs = 4326, agr = "constant"))
# This makes the final map
library("ggspatial")
ggplot(data = world) +
geom_sf(fill = "antiquewhite1") +
geom_sf(data = countiesil, aes(fill = area)) +
geom_sf(data = states, fill = NA)+
geom_point(data = ILdata, aes(x = longitude, y = latitude), size = 4,
shape = 23, fill = "darkred")+
geom_sf(data = ilcities) +
geom_text_repel(data = ilcities, aes(x = lng, y = lat, label = city),
fontface = "bold", nudge_x = c(1, -2), nudge_y = c(0.4,0.25)) +
annotation_scale(location = "bl", width_hint = 0.4) +
annotation_north_arrow(location = "bl", which_north = "true",
pad_x = unit(0.75, "in"), pad_y = unit(0.5, "in"),
style = north_arrow_fancy_orienteering) +
coord_sf(xlim = c(-92, -87), ylim = c(36.8, 42.8), expand = FALSE) +
xlab("Longitude") + ylab("Latitude") +
ggtitle("Sites", subtitle = "(14 sites across Illinois)") +
theme(panel.grid.major = element_line(color = gray(0.5), linetype = "dashed",
size = 0.5), panel.background = element_rect(fill = "aliceblue"))
我想以此添加散点图(与我的地图分开使用非常有效):
+geom_scatterpie(aes(x=longitude, y=latitude), data=ILdata, cols=c("pos", "neg"))
这项工作有效吗?还有其他方法可以将饼图添加到我的位置吗?我只希望它们是饼图,显示我的小数据集中的阳性和阴性的百分比。
任何帮助将不胜感激!谢谢!