我在使用多个图层分隔ggplot2图形中的图例时遇到了麻烦。我的情节是根据在那里的纺织公司的数量来填补不同的城市,我还用geom_point
绘制工厂本地化。我的猜测是以某种方式使用aes.override()
,但我还是无法做到这一点。我读过的解决方案没有处理aes()
geom_point()
中详述的图表的不同变量。
如果你想测试下面的代码,你可以下载巴西市政府here的shapefile,使用readOGR
和fortify
,然后选择填写您喜欢的城市fill
并在巴西境内设置任意随机点,geom_point()
创建一个不同的变量,例如下面的lat_plant
和long_plant
。下面的region
列详细介绍了巴西地区 - 在这种情况下,“1”详细介绍了巴西北部地区。
#setting the ggplot
library(ggplot2)
gg2 < -ggplot(data = out[out$region =="1",],
aes(x = long, y = lat, group = group, fill = as.factor(companies))) +
geom_polygon() +
ggtitle("title") +
scale_fill_discrete(name = "Number of Textile Companies") +
theme(plot.title = element_text(size = 30, face = "bold")) +
theme(legend.text = element_text(size = 12),
legend.title = element_text(colour = "blue", size = 16, face = "bold"))
#graph output
gg2 +
geom_point(data = out[out$region =="1",], aes(x = long_plant, y = lat_plant), color = "red")
我得到的是传说:
我想把它分开,详细说明作为本地化的点和颜色作为该地区纺织公司数量的填充。
答案 0 :(得分:4)
我为你留下另一个选择。 hmgeiger将纺织公司的数量视为因素。但是,我宁愿将变量视为连续变量。由于没有可重现的数据,我自己创建了一个样本数据。在这里,我创建了巴西经度和纬度的随机样本,并确保一些数据点留在巴西。 whatever2
包含留在巴西的数据点。我也在这里做了一些技巧。我添加了一个名为Factory location
的新列。这是用于向最终图形中的数据点添加颜色的虚拟变量。 hmgeiger为您创建了包含字符的Dummy.var
。我宁愿在此列中留下""
,因为您可能不希望在图例中看到任何文字。
对于你的传奇问题,正如Antonio提到的和hmgeiger所做的那样,你需要在aes()
geom_point()
中添加颜色。这解决了它。我为你做了一件事。如果您不知道每个市政当局有多少工厂,您需要计算工厂数量。我使用poly.count()
包中的GISTools
完成了这项工作,并创建了另一个包含每个市政工厂数量的数据框。
当我绘制地图时,我有三层。一个用于多边形,另一个用于填充多边形的颜色。它们是使用geom_cartogram()
包中的ggalt
完成的。关键是你需要有map_id
的公共密钥列。第一个id
中的geom_cartogram()
和第二个ind
中的geom_cartogram()
是相同的信息。在geom_point()
中,您需要color
中的aes()
。图例中有一个连续的工厂数量栏和一个工厂位置点。旁边没有文字。所以这让传说变得整洁,我想。
library(raster)
library(tidyverse)
library(GISTools)
library(RColorBrewer)
library(ggalt)
library(ggthemes)
# Get polygon data for Brazil
brazil <- getData("GADM", country = "brazil", level = 1)
mymap <- fortify(brazil)
# Create dummy data staying in the polygons
# For more information: https://stackoverflow.com/questions/47696382/removing-data-outside-country-map-boundary-in-r/47699405#47699405
set.seed(123)
mydata <- data.frame(long = runif(200, min = quantile(mymap$long)[1], max = quantile(mymap$long)[4]),
lat = runif(200, min = quantile(mymap$lat)[1], max = quantile(mymap$lat)[4]),
factory = paste("factory ", 1:200, sep = ""),
stringsAsFactors = FALSE)
spdf <- SpatialPointsDataFrame(coords = mydata[, c("long", "lat")], data = mydata,
proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
whatever <- spdf[!is.na(over(spdf, as(brazil, "SpatialPolygons"))), ]
whatever2 <- as.data.frame(whatever) %>%
mutate(`Factory location` = "")
# Now I check how many data points (factories) exist in each polygon
# and create a data frame
factory.num <- poly.counts(pts = whatever, polys = brazil)
factory.num <- stack(factory.num)
ggplot() +
geom_cartogram(data = mymap, aes(x = long, y = lat, map_id = id),
map = mymap) +
geom_cartogram(data = factory.num, aes(fill = values, map_id = ind),
map = mymap) +
geom_point(data = whatever2, aes(x = long, y = lat, color = `Factory location`)) +
scale_fill_gradientn(name = "Number of factories", colours = brewer.pal(5, "Greens")) +
coord_map() +
theme_map()
答案 1 :(得分:1)
仅供参考,您发布的用于下载形状文件的链接非常慢,至少要下载到美国计算机上。
此链接的下载效果更好,还展示了如何读取形状数据:https://dioferrari.wordpress.com/2014/11/27/plotting-maps-using-r-example-with-brazilian-municipal-level-data/
我做了一个例子,使用区域而不是市政数据来保持简单。
我在此处使用的数据可供下载:https://drive.google.com/file/d/0B64xLcn8DZfwakNMbHFLQWo4YzA/view?usp=sharing
#Load libraries.
library(rgeos)
library(rgdal)
library(ggplot2)
#Read in and format map data.
regions_OGR <- readOGR(dsn="/Users/hmgeiger/Downloads/regioes_2010",
layer = "regioes_2010")
map_regions <- spTransform(regions_OGR,CRS("+proj=longlat +datum=WGS84"))
map_regions_fortified <- fortify(map_regions)
#We make there be 0, 1, or 3 textile companies.
#map_regions_fortified is in order by ID (region).
#So, we add a column with the number of textile companies
#repeated the right number of times for how many of each region there is.
num_rows_per_region <- data.frame(table(map_regions_fortified$id))
map_regions_fortified <- data.frame(map_regions_fortified,
Num.factories = factor(rep(c(1,0,1,3,1),times=num_rows_per_region$Freq)))
#First, plot without any location dots.
ggplot()+geom_polygon(data=map_regions_fortified,
aes(x = long,y = lat, group=group, fill=Num.factories),colour="black")
现在,让我们添加工厂位置。
#Set latitude and longitude based on the number of factories per region.
factory_locations <- data.frame(long = c(-65,-55,-51,-44,-42,-38),
lat = c(-5,-15,-27,-7,-12,-8))
#Add a dummy variable, which then allows the colour of the dots
#to be a part of the legend.
factory_locations <- data.frame(factory_locations,
Dummy.var = rep("One dot = one factory location",times=nrow(factory_locations)))
#Replot adding factory location dots.
#We will use black dots here since will be easier to see.
ggplot()+geom_polygon(data=map_regions_fortified,
aes(x = long,y = lat, group=group, fill=Num.factories),colour="black")
+ geom_point(data = factory_locations,aes(x = long,y = lat,colour = Dummy.var))
+ scale_colour_manual(values="black") + labs(colour="")
#Bonus: Let's change the color vector to something more color-blind friendly.
mycol <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442",
"#0072B2", "#D55E00", "#CC79A7","#490092")
ggplot()+geom_polygon(data=map_regions_fortified,
aes(x = long,y = lat, group=group, fill=Num.factories),colour="black")
+ geom_point(data = factory_locations,aes(x = long,y = lat,colour = Dummy.var))
+ scale_colour_manual(values="black") + labs(colour="")
+ scale_fill_manual(values=mycol)