ggplot对感兴趣的区域放大

这个功能今天工作的时候需要,就搜了一下,有个 ggforce 包可以做这个事情(之前还 mark 过,哈哈)。

简单用示例代码展示如何使用它,主要是根据数据的选择进行放大或者根据坐标范围进行放大。

安装:

install.packages("ggforce")

加载:

library(ggforce)

先看一个正常的图:

ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
  geom_point()

我们取一个分类的数据进行放大:

ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
  geom_point() +
  facet_zoom(x = Species == 'versicolor')

突然之间就有点逼格了,如果再配上个主题,再好不过了。

学术版本:

ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
    geom_point() +
    facet_zoom(x = Species == 'versicolor') + cowplot::theme_cowplot()

娱乐版本:

ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
    geom_point() +
    facet_zoom(x = Species == 'versicolor') + theme_dark()

还可以选择性展示数据:

ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
    geom_point() +
    facet_zoom(x = Species == 'versicolor', zoom.data = Species == 'versicolor') + cowplot::theme_cowplot()

再最后看下怎么按坐标范围放大吧:

ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
    geom_point() +
    facet_zoom(xlim = c(2, 4)) + cowplot::theme_cowplot()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值