我目前正试图在秘鲁马努国家公园创建几种动物物种的地理范围地图。我想绘制先前观察到的地理范围以及新的估计范围。我认为最好的方法是制作一个以平均高程为点的区间图,然后是表示最小和最大高程的误差条。这是我创建的以下代码:
solm <- data.frame(mean =
c(1160,690,380,365,375,475,675,2685,675,415,905,
1135,3350,400,1460,385, 675, 415,2875),
std.dev = c(760,310,0,15,50,250,300,765,300,35,555,
785,0,50,0,15, 325, 65,425),
class = c("previous"),
group = c("D.marsupialis","D.novemcinctus",
"P.maximus","T.tetrodactyla",
"C.albifrons","S.ignitus","C. paca",
"C. taczanowskii","D.punctata",
"A.microtis","P.flavus","E.barbara",
"M.frenata","L.pardalis","L.tigrinus",
"L.weidii","P.onca", "M.americana",
"M.chunyi"))
solm
##Observed Elevation Range
obsm <- data.frame(mean =
c(1290,825,460,930,920,1165,1050,2740,1275,565,1226,
1900.5,2742.5,550,2363.5,445, 1050, 550,2750),
std.dev = c(890,445,80,580,570,815,700,820,925,185,876,
1550.5,814.5,200,1097.5,75, 700, 200,550),
class = c("observed"),
group = c("D.marsupialis","D.novemcinctus",
"P.maximus","T.tetrodactyla",
"C.albifrons","S.ignitus","C. paca",
"C. taczanowskii","D.punctata",
"A.microtis","P.flavus","E.barbara",
"M.frenata","L.pardalis","L.tigrinus",
"L.weidii","P.onca", "M.americana",
"M.chunyi"))
obsm
##Combo
ggplot()+geom_pointrange(data=obsm, aes(x=group, y=mean, ymax=mean+std.dev,
ymin=mean-std.dev, color="red"))+
geom_pointrange(data=solm, aes(x=group, y=mean, ymax=mean+std.dev,
ymin=mean-std.dev, color="blue"))+
geom_pointrange(position = position_dodge(width = 0.2))+
labs(x="Mammal Species", y="Elevation (m)")+ theme_bw() +
theme(panel.grid.major = element_blank(),panel.grid.minor =
element_blank(),
panel.border = element_blank())+
theme (axis.text.x = element_text(angle = 45, hjust = 1,
face="bold.italic"))+
scale_y_continuous(breaks=c(300, 500, 1000, 1500, 2000, 2500,
3000, 3500, 4000))+
scale_colour_manual(values=c("red", "blue"))
这很好,但是ggplot2将范围叠加在一起。我想稍微抖动点数,但我尝试过的一切都没有用。有什么想法吗?
答案 0 :(得分:0)
我会以稍微不同的方式做到这一点: 首先生成一个数据帧来绘制:
obsm<-cbind(obsm,data=rep("obsm",nrow(obsm)))
solm<-cbind(solm,data=rep("solm",nrow(solm)))
df<-rbind(obsm,solm)
然后使用:
绘图ggplot()+geom_pointrange(data=df, aes(x=data, y=mean, ymax=mean+std.dev, ymin=mean-std.dev,col=data))+facet_grid(.~group,scal="free")+scale_colour_manual(values=c("red", "blue"))+theme(axis.text.x= element_blank())+theme(strip.text.x = element_text(size = 14,angle=90,face=c('bold')))
答案 1 :(得分:0)
建议仅使用一个数据帧作为ggplot2输入。 然后你可以尝试
#rbind together
d <- rbind(solm,obsm)
# and the plot
ggplot(d,aes(x=group, y=mean, col=class)) +
geom_pointrange(aes(ymin = mean+std.dev, ymax = mean-std.dev), position = position_dodge(0.5))+
theme (axis.text.x = element_text(angle = 45, hjust = 1,
face="bold.italic"))