前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ggplot2拓展功能:局部放大和边际密度图

ggplot2拓展功能:局部放大和边际密度图

作者头像
拴小林
发布2020-11-03 14:49:26
1.7K0
发布2020-11-03 14:49:26
举报
文章被收录于专栏:数据驱动实践数据驱动实践

01

ggExtra/ggpubr:边际密度示意图

ggplot拓展功能

第一部分的添加边际密度示意图。

代码语言:javascript
复制
#-----ggExtra:Add marginal density plots-------#
library(ggplot2)
# Create a scatter plot
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point(aes(color = Species), size = 3, alpha = 0.6) +
  scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
# Add density distribution as marginal plot
# install.packages("ggExtra")
library("ggExtra")
ggMarginal(p, type = "density")
# Change marginal plot type
ggMarginal(p, type = "boxplot")
代码语言:javascript
复制
#-----ggpubr-------#
# One limitation of ggExtra is that it can’t cope with multiple groups in the scatter plot and the marginal plots.
# A solution is provided in the function ggscatterhist() [ggpubr]:
library(ggpubr)
# Grouped Scatter plot with marginal density plots
ggscatterhist(
  iris, x = "Sepal.Length", y = "Sepal.Width",
  color = "Species", size = 3, alpha = 0.6,
  palette = c("#00AFBB", "#E7B800", "#FC4E07"),
  margin.params = list(fill = "Species", color = "black", size = 0.2)
)

# Use box plot as marginal plots
ggscatterhist(
  iris, x = "Sepal.Length", y = "Sepal.Width",
  color = "Species", size = 3, alpha = 0.6,
  palette = c("#00AFBB", "#E7B800", "#FC4E07"),
  margin.plot = "boxplot",
  ggtheme = theme_bw()
)

02

ggforce:局部放大

ggplot拓展功能

第二部分的局部放大。

代码语言:javascript
复制
#-----ggforce:Zoom in a scatter plot-------#
# Key function: facet_zomm() [in ggforce] (Pedersen 2016).
# Demo data set: iris. The R code below zoom the points where Species == "versicolor".
library(ggforce)
ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
  geom_point() +
  ggpubr::color_palette("jco") + 
  facet_zoom(x = Species == "versicolor")+
  theme_bw()
代码语言:javascript
复制
# x varible control
ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
  geom_point() +
  ggpubr::color_palette("jco") + 
  facet_zoom(x = Petal.Length < 2.5)+
  theme_bw()
代码语言:javascript
复制
# ylim control
ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
  geom_point() +
  ggpubr::color_palette("jco") + 
  facet_zoom(ylim = c(1,2))+
  theme_bw()

具体指引详见

http://www.sthda.com/english/articles/32-r-graphics-essentials/131-plot-two-continuous-variables-scatter-graph-and-alternatives/

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-11-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据驱动实践 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档