这个功能今天工作的时候需要,就搜了一下,有个 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()

更多学习见包文档:https://ggforce.data-imaginist.com/

网友评论