另一个有趣的ggplot2挑战!我正在尝试创建一个ggsubplot调用,以在世界地图上给出饼图。困难的事情似乎是ggplot2中的饼图是极坐标下的堆积条形图,并且coord_polar的添加不仅影响子图几何图形,还影响整个地图本身。有人知道如何仅将特定坐标方案应用于子图调用的某个部分吗?这是我目前的代码:
library(ggplot2)
library(maps)
library(mapproj)
#install.packages("devtools")
library(devtools)
# install ggsubplot from github repo, not currently on CRAN
install_github(username="garrettgman", repo="ggsubplot")
world = map_data("world")
loc_pie = structure(list(Region = structure(c(3L, 5L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 15L, 16L, 2L, 14L, 2L, 4L, 5L, 6L, 7L, 9L, 10L
), .Label = c("", "ANT/SO", "ARC", "EPR/GAL", "GOM/CAR", "IND",
"MAR", "MED", "N-ATL", "NE-ATL", "NE-PAC", "NW-ATL", "NW-PAC",
"SE-ATL", "SE-PAC", "SW-ATL", "SW-PAC"), class = "factor"), Group3 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L), .Label = c("Annelida", "Choanoflagellata", "Chordata",
"Cnidaria", "Crustacea", "Echinodermata", "Foraminifera", "Mollusca",
"Nematoda", "Other", "Platyhelminthes", "Porifera"), class = "factor"),
ones = c(1, 1, 5, 1, 1, 1, 18, 3, 1, 4, 8, 1, 1, 2, 1, 1,
6, 1, 2, 5), tot = c(5, 30, 11, 16, 28, 22, 51, 25, 78, 13,
32, 57, 61, 57, 15, 30, 20, 11, 28, 22), div = c(0.2, 0.0333333333333333,
0.454545454545455, 0.0625, 0.0357142857142857, 0.0454545454545455,
0.352941176470588, 0.12, 0.0128205128205128, 0.307692307692308,
0.25, 0.0175438596491228, 0.0163934426229508, 0.0350877192982456,
0.0666666666666667, 0.0333333333333333, 0.3, 0.0909090909090909,
0.0714285714285714, 0.227272727272727), lat = c(71.4493167,
19.9897167, 23.5874333, 37.6802167, 55.13365, 36.6889333,
35.9565333, 35.53935, 30.4266, -30.32195, -33.2038, -65.8756333,
-17.12415, -65.8756333, 0.1135, 19.9897167, -14.5800667,
23.5874333, 55.13365, 36.6889333), long = c(-1.0550667, -81.3430667,
-41.2278667, 15.9298833, -30.4984333, -17.4906167, -149.4363333,
-63.01795, 156.3570833, -110.23255, -31.20155, -25.4557,
0.0881833, -25.4557, -101.07455, -81.3430667, 77.4312667,
-41.2278667, -30.4984333, -17.4906167)), .Names = c("Region",
"Group3", "ones", "tot", "div", "lat", "long"), row.names = c(NA,
20L), class = "data.frame")
ggplot(data=loc_pie) + geom_polygon(data=world, aes(x=long, y=lat, group =group),colour="grey40", fill="grey40") + geom_subplot(height=12, aes(long, lat, group=Region, subplot = (geom_bar(aes(x = factor(1), y=div, fill=factor(Group3)), width =1, height = 2,stat="identity"))))
看起来我无法发布图片,因为我是新用户。
![带有堆叠条形图子图的世界地图][1]
如何将极坐标仅用于子图?非常感谢。
ggsubplot
不在 CRAN 上。请通过添加可重现的代码(包括安装必要包的代码)来帮助他人帮助您。谢谢。 - Henrik