前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >统计绘图还不熟?!这个超强技巧了解一下~~

统计绘图还不熟?!这个超强技巧了解一下~~

作者头像
DataCharm
发布2021-12-01 16:52:26
3960
发布2021-12-01 16:52:26
举报

今天给大家介绍一个小编在查阅资料时发现的一个非常棒的可视化绘制工具-R-WVPlots包,该包提供多个优秀可视化绘制函数,如:ROC曲线、增益曲线(gain curve)、具有边际分布的散点图(scatter plot with marginal distributions)、具有边际密度的条件散点图(conditioned scatter plot with marginal densities) 等。具体内容包括:

  • R-WVPlots包简介
  • R-WVPlots包样例展示

R-WVPlots包简介

R-WVPlots包作为一个优秀的第三方包,其目的是为了提供更多便捷的绘图函数,快速绘制一些统计图表,其官网为:https://winvector.github.io/WVPlots/index.html ,更多关于此包的诞生及相关介绍可以参考:R-WVPlots包介绍[1]。

R-WVPlots包样例展示

由于其提供给的绘图函数较多,我们直接列出几类进行展示,详细内容如下:

「Scatterplots」:

代码语言:javascript
复制
#生成样例数据
set.seed(34903490)
x = rnorm(50)
y = 0.5*x^2 + 2*x + rnorm(length(x))
frm = data.frame(
  x = x,
  y = y,
  yC = y>=as.numeric(quantile(y,probs=0.8)),
  stringsAsFactors = FALSE)
frm$absY <- abs(frm$y)
frm$posY = frm$y > 0

开始绘图:

代码语言:javascript
复制
plot01 <- ScatterHist(frm, "x", "y", title="Example Fit",contour = TRUE,
                               point_color = "#EFC000", 
                               hist_color = "#0073C2", 
                               smoothing_color = "#CD534C", 
                               density_color = "#08519c", 
                               contour_color = "#9e9ac8") 

Example Of WVPlots::ScatterHist()

「Gain Curves」:

代码语言:javascript
复制
set.seed(34903490)
y = abs(rnorm(20)) + 0.1
x = abs(y + 0.5*rnorm(20))

frm = data.frame(
  model=x, 
  value=y,
  stringsAsFactors = FALSE)

frm$costs=1
frm$costs[1]=5
frm$rate = with(frm, value/costs)

gainx = 0.10  # get the top 10% most valuable points as sorted by the model

# make a function to calculate the label for the annotated point
labelfun = function(gx, gy) {
  pctx = gx*100
  pcty = gy*100
  
  paste("The top ", pctx, "% most valuable points by the model\n",
        "are ", pcty, "% of total actual value", sep='')
}

plot2 <- WVPlots::GainCurvePlotWithNotation(frm, "model", "value", 
                                   title="Example Gain Curve with annotation", 
                          gainx=gainx,labelfun=labelfun) +
labs(
    title = "Example of <span style='color:#D20F26'>WVPlots::GainCurvePlotWithNotation function</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>GainCurvePlotWithNotation()</span>",
    caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  hrbrthemes::theme_ipsum(base_family = "Roboto Condensed")  +
  theme(
    plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
                                  size = 20, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
    plot.caption = element_markdown(face = 'bold',size = 12))

Example Of WVPlots::GainCurvePlotWithNotation()

「ROC Plots」:

代码语言:javascript
复制
set.seed(34903490)
x1 = rnorm(50)
x2 = rnorm(length(x1))
x3 = rnorm(length(x1))
y = 0.2*x2^2 + 0.5*x2 + x1 + rnorm(length(x1))
frm_m = data.frame(
   x1 = x1,
   x2 = x2,
   x3 = x3,
   yC = y >= as.numeric(quantile(y,probs=0.8)))
plot3 <- WVPlots::ROCPlotPairList(
   frame = frm_m,
   xvar_names = c("x1", "x2", "x3"),
   truthVar = "yC", truthTarget = TRUE,
   title = "Example ROC list plot") +
  labs(
    title = "Example of <span style='color:#D20F26'>WVPlots::ROCPlotPairList function</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>ROCPlotPairList()</span>",
    caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  hrbrthemes::theme_ipsum(base_family = "Roboto Condensed")  +
  theme(
    plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
                                  size = 20, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
    plot.caption = element_markdown(face = 'bold',size = 12))

Example Of WVPlots::ROCPlotPairList

「Shadow Plots」

代码语言:javascript
复制
set.seed(354534)
N = 100

dframe = data.frame(
  x = rnorm(N), 
  gp = "region 2", 
  stringsAsFactors = FALSE)
dframe$gp = with(dframe, ifelse(x < -0.5, "region 1", 
                                ifelse(x > 0.5, "region 3", gp)))

colormap = c("#1F968BFF", "#29AF7FFF", "#55C667FF")

WVPlots::ShadowHist(dframe, "x", "gp", title = "X values by region", palette=NULL) +
  ggplot2::scale_fill_manual(values=colormap) +
  hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  labs(
    title = "Example of <span style='color:#D20F26'>WVPlots::ShadowHist function</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>ShadowHist()</span>",
    caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  hrbrthemes::theme_ipsum(base_family = "Roboto Condensed")  +
  theme(
    plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
                                  size = 20, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
    plot.caption = element_markdown(face = 'bold',size = 12))

Example Of WVPlots::ShadowHist()

「Distribution and Count Plots」

代码语言:javascript
复制
set.seed(52523)
d <- data.frame(wt=100*rnorm(100),
                stringsAsFactors = FALSE)
WVPlots::PlotDistCountNormal(d,'wt','example',binWidth = 20,
                              hist_color = "#0073C2",
                              normal_color = "#868686",
                              mean_color = "red",
                              sd_color = "blue") +
labs(
    title = "Example of <span style='color:#D20F26'>WVPlots::PlotDistCountNormal function</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>PlotDistCountNormal()</span>",
    caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed")+
theme(
    plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
                                  size = 20, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
    plot.caption = element_markdown(face = 'bold',size = 12))

Example Of WVPlots::PlotDistCountNormal()

「Density Plot with Shaded Tail」:

代码语言:javascript
复制
set.seed(52523)
d = data.frame(meas=rnorm(100))
boundaries = c(-1.5, 1.5)
WVPlots::ShadedDensityCenter(d, "meas", boundaries,linecolor = "black",
                       title="Example center-shaded density plot")+
labs(
    title = "Example of <span style='color:#D20F26'>WVPlots::ShadedDensityCenter function</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>ShadedDensityCenter()</span>",
    caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed")+
theme(
    plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
                                  size = 20, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
    plot.caption = element_markdown(face = 'bold',size = 12))

Example Of WVPlots::ShadedDensityCenter

更多其他绘图函数可参考:R-WVPlots包样例[2]

总结

今天的这篇推文,小编简单的介绍了R-WVPlots包的绘图函数,其中有很多实用方便的统计绘图函数,希望感兴趣的小伙伴可以看下哈~

参考资料

[1]R-WVPlots包介绍: https://github.com/WinVector/WVPlots。

[2]R-WVPlots包样例: https://winvector.github.io/WVPlots/articles/WVPlots_examples.html。

喜欢就点个 在看 呗 👇

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

本文分享自 DataCharm 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • R-WVPlots包简介
  • R-WVPlots包样例展示
  • 总结
    • 参考资料
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档