ggsubplot (ggplot2) 中的饼图
Posted
技术标签:
【中文标题】ggsubplot (ggplot2) 中的饼图【英文标题】:Pie Charts in ggsubplot (ggplot2) 【发布时间】:2013-12-01 20:48:03 【问题描述】:另一个有趣的 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]
关于如何仅将极坐标合并到子图的任何想法?非常感谢。
【问题讨论】:
嗨!拥有minimal reproducible example 会非常有帮助,这样我们就可以自己重现问题。 是的,感谢您的建议。进行了应该允许更基本复制的编辑。ggsubplot
不在 CRAN 上。请帮助人们通过使您的代码可重现来帮助您,即添加运行平稳的代码,包括用于安装必要软件包的代码。谢谢。
对,对不起。似乎它已经打开和关闭 CRAN,并且可能很快就会恢复。现在,看起来您可以从 cran 获取以前的版本,或者使用我在上面添加的代码通过 github 安装它。谢谢!
这确实解释了如何在基本图形包中执行此操作,但现在可能在 ggplot2 中添加了 ggsubplot。感谢您的链接!
【参考方案1】:
尝试多图功能 http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/ i 允许您在一个窗口上绘制多个图
或
ggplot, facet, piechart: placing text in the middle of pie chart slices
这就是您要搜索的内容
【讨论】:
【参考方案2】:这里是ggplot2
和ggtree
最新版本的解决方案:
library(maps)
library(mapproj)
library(ggplot2)
library(ggtree)
library(dplyr)
library(magrittr)
world = map_data("world")
p <- ggplot(data=world, aes(x=long, y=lat, group =group)) + geom_polygon(colour="grey40", fill="grey40")
for (name in unique(loc_pie[['Region']]))
loc_region <- filter(loc_pie, Region %in% name)
pie_tmp <- ggplot(data = loc_region, aes(x = factor(1), y = div, fill = Group3)) +
geom_bar(width = 1, stat = "identity") + coord_polar(theta = "y") +
scale_fill_discrete(drop = FALSE) +
xlab(NULL) + ylab(NULL) + theme_tree() +
theme_transparent()
lat_region <- loc_region[[1,'lat']]
long_region <- loc_region[[1,'long']]
p %<>% subview(pie_tmp, long_region, lat_region, width = .07, height = .07)
p
宽度和高度是手动选择的,它们对应于子视图相对于完整视图的比例。
为了完整起见,loc_pie
可以通过以下方式获得:
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"
)
【讨论】:
【参考方案3】:以下类型的作品,但不使用ggsubplot
。
library(sp)
library(plotGoogleMaps)
loc_pie$divc = 1-loc_pie$div
coordinates(loc_pie) <- ~long+lat
proj4string(loc_pie) <- CRS("+proj=longlat")
m <- segmentGoogleMaps(loc_pie,zcol=c('div','divc'),
scalelist=FALSE,
max.radius=1000000,
mapTypeId = "ROADMAP",
legend = FALSE, control = FALSE)
我无法弄清楚如何获得该功能以使所有气泡大小相同。它似乎忽略了一些参数,如 scalelist 和 bounds。 Plotly 也可以,只要您愿意将 div 的单个值编码为颜色或大小:
library(plotly)
loc_pie$hover <- paste(loc_pie$div*100,"%")
g <- list(
scope = 'world',
projection = list(type = "mercator"),
showland = TRUE,
landcolor = toRGB("gray85"),
subunitwidth = 1,
countrywidth = 1,
subunitcolor = toRGB("white"),
countrycolor = toRGB("white")
)
plot_ly(loc_pie, lon = long, lat = lat, text = hover,
marker = list(size = 100),
type = 'scattergeo', color=div,
locationmode = 'country names',geo=g)
【讨论】:
以上是关于ggsubplot (ggplot2) 中的饼图的主要内容,如果未能解决你的问题,请参考以下文章