ggplot 局部放大

需要安装包:ggforce,下面以R自带数据做局部放大演示。

require(ggplot2)
require(ggforce)
require(reshape2)

data(CO2)
co2<-melt(CO2,id.vars=c("Plant","Type","Treatment"),variable.name = c("n1"))
head(co2)
ggplot(co2,aes(x=n1,y=value))+
  geom_bar(aes(fill=Type),stat="identity")

直接出上图的结果,可以看出两组数据相差很大,小的数据根本看不清对应的Y值落在哪里,当然这里可以使用数字标识在条形图顶部,要么局部放大。

ggplot(co2,aes(x=n1,y=value))+
  geom_bar(aes(fill=Type),stat="identity")+
  ggforce::facet_zoom(ylim=c(0,3000))

facet_zoom还有其他参数,如zoom.size = 0.5,可将放大部分的图缩减为主图的一半大小,默认2倍。

还有show.area 参数,默认True,主图放大部分加上阴影,上图可以看出来,不过很淡。 

posted @ 2020-04-14 23:22  天使不设防  阅读(1324)  评论(0编辑  收藏  举报