用ggsubplot指定绘图顺序 [英] Specifying plot order with ggsubplot
问题描述
我使用ggplot和ggsubplot为地图添加条形图,但无法弄清楚如何指定首先绘制哪个图。我想首先绘制偏北的地图,然后坐在任何重叠的地块后面。低填充alpha这些应该仍然是可见的。这是工作流程:
library(ggsubplot)
library(ggplot2)
library(maps)
库(plyr)
world_map = map_data(world)
(p = ggplot()+ geom_polygon(data = world_map,aes(x = long,y = lat, (世界地图,。(地区),总结,长=平均(长),拉特=平均(拉特))
d = d [样本(1) (d),50),]
d = rbind(d,d)
d $ cat = rep(c('A','B'),each = nrow(d)/ 2)
d $ value = sample(1:10,nrow(d),rep = T)
head(d)
p + geom_subplot(data = d,aes(long,lat ,group = region,subplot = geom_bar(aes(cat,value,fill = cat),
col ='black',alpha = 0.9,stat =identity)),width = 30,height = 30)
你可以看到图表顺序似乎很随机。所以我试图改变地区(国家)为一个有序的因素:
d $ region = factor(d $ region,ordered = T)
(ord = count(d [,c('region','lat')],vars = c('region','lat')))
ordered_levels = order(ord $ lat,decrease = T)
print(ord [ordered_levels,])
levels(d $ region)= levels(d $ region)[ordered_levels]
levels(d $ region)
p + geom_subplot(data = d,aes(long,lat,group = region,subplot = geom_bar(aes(cat,value,fill = cat),
col ='black',alpha = 0.9,stat =identity)),width = 30,height = 30)
似乎没有解决问题。非常感谢任何建议。
您需要订购 d
按照纬度指示,并且在调用时也使用
。 group = lat
geom_subplot(...)
set.seed(1)
d = ddply (world_map,。(region),summary,long = mean(long),lat = mean(lat))
d = d [sample(1:nrow(d),50),]
d = rbind (d,d)
d $ cat = rep(c('A','B'),each = nrow(d)/ 2)
d $ value = sample(1:10,nrow(d ),rep = T)
d <-d [order(d $ lat),]
head(d)
p + geom_subplot(data = d,aes(long,lat,group = lat,
subplot = geom_bar(aes(cat,value,fill = cat),
col ='black',alpha = 0.9,stat =identity)),width = 30,height = 30 )
I'm adding bar-plots to maps using ggplot and ggsubplot, but cannot figure out how to specify which to plot first. I'd like to plot the northerly ones first so they sit behind any overlapping plots. With a low fill alpha these should still be viewable. This is the workflow:
library(ggsubplot)
library(ggplot2)
library(maps)
library(plyr)
world_map = map_data("world")
(p = ggplot() + geom_polygon(data = world_map, aes(x=long, y=lat,group=group)))
d = ddply(world_map,.(region),summarize,long=mean(long),lat=mean(lat))
d = d[sample(1:nrow(d), 50),]
d = rbind(d,d)
d$cat = rep(c('A','B'), each=nrow(d)/2)
d$value = sample(1:10, nrow(d), rep=T)
head(d)
p + geom_subplot(data=d, aes(long, lat, group=region, subplot = geom_bar(aes(cat, value, fill=cat),
col='black', alpha=0.9, stat="identity")), width = 30, height=30)
As you can see the plot order seems pretty random. So I tried to change region (country) to an ordered factor:
d$region = factor(d$region, ordered=T)
(ord = count(d[,c('region','lat')], vars=c('region','lat')))
ordered_levels = order(ord$lat, decreasing=T)
print(ord[ordered_levels,])
levels(d$region) = levels(d$region)[ordered_levels]
levels(d$region)
p + geom_subplot(data=d, aes(long, lat, group=region, subplot = geom_bar(aes(cat, value, fill=cat),
col='black', alpha=0.9, stat="identity")), width = 30, height=30)
But this does not seem to solve the problem. Very grateful for any suggestions.
Is this what you had in mind?
You need to order d
by latitude, as you pointed out, and also use group=lat
in the call to geom_subplot(...)
.
set.seed(1)
d = ddply(world_map,.(region),summarize,long=mean(long),lat=mean(lat))
d = d[sample(1:nrow(d), 50),]
d = rbind(d,d)
d$cat = rep(c('A','B'), each=nrow(d)/2)
d$value = sample(1:10, nrow(d), rep=T)
d <- d[order(d$lat),]
head(d)
p + geom_subplot(data=d, aes(long, lat, group=lat,
subplot = geom_bar(aes(cat, value, fill=cat),
col='black', alpha=0.9, stat="identity")), width = 30, height=30)
这篇关于用ggsubplot指定绘图顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!