探索两个变量

来源:互联网 发布:阿里云隐藏域名信息 编辑:程序博客网 时间:2024/06/10 11:30

散点图

给qplot两个参数,就会生成散点图

qplot(x=age, y=friend_count, data=pf)# 等效ggplot语法ggplot(aes(x = age, y = friend_count), data = pf) +   geom_point()

设置透明度

# alpha=1/20表示20个点等效1个黑点# geom_jitter用于添加一点抖动,是geom_point(position='jitter')的简写版,geom_point用于绘制散点图ggplot(aes(x=age, y=friend_count), data=pf)+  geom_jitter(alpha=1/20)

最后声称效果如图所示:

这里写图片描述

但是这样直接添加coord_trans(y=’sqrt’)会遇到一点问题,因为jitter抖动产生的噪声是在真值正负之间都有可能的事情,
如果有人发起结交新朋友个数为0,一抖动,取平方根可能就会出问题。可以这样写,就避免了问题:

ggplot(aes(x=age, y=friendships_initiated), data=pf)+  geom_point(alpha=1/10, position=position_jitter(h=0))+  coord_trans(y='sqrt')

条件均值

library(dplyr)# group_by(pf, age)表示按照age进行分组age_groups <- group_by(pf, age)pf.fc_by_age <- summarise(age_groups,                  friend_count_mean=mean(friend_count),                  friend_count_median=median(friend_count),                  n=n())# arrange是按照年龄重新排序pf.fc_by_age <- arrange(pf.fc_by_age, age)# 这些语句最后生成一个pf.fc_by_age表单,无法直接读取,可以使用head来读取头部数据head(pf.fc_by_age)# 方法二;或者写成这样:age_groups <- pf%>%              group_by(age)%>%              summarise(  friend_count_mean=mean(friend_count),                          friend_count_median=median(friend_count),                          n=n()) %>%             arrange(age)head(age_groups)

两个数据图进行叠加

# 要表示在 fun.y 指定的函数上设置的参数,请使用 fun.args 参数# geom_line(stat = 'summary'用于绘制两个图像叠加的曲线,stat = 'summary'表示绘制汇总曲线,fun.y=mean表示将平均值传递给fun.y# fun.y=quantile表示将中位数传递给fun.yggplot(aes(x=age, y=friend_count), data=pf)+  geom_point(alpha=1/20, position=position_jitter(h=0),              color='orange')+  coord_cartesian(xlim = c(13,90))+  coord_trans(y='sqrt')+  geom_line(stat = 'summary', fun.y=mean)+# 这是10%中位数汇总曲线,后面是50%和90%  geom_line(stat= 'summary', fun.y=quantile, fun.args = list(probs = .1),            linetype= 2, color='blue')+  geom_line(stat= 'summary', fun.y=quantile, fun.args = list(probs = .5),            linetype= 2, color='blue')+  geom_line(stat= 'summary', fun.y=quantile, fun.args = list(probs = .9),            linetype= 2, color='blue')

绘制结果如下图所示:

这里写图片描述

相关性

计算相关性的方法如下

cor.test(pf$age, pf$friend_count, method = 'pearson')# 方法二# 使用宽度函数with,宽度函数允许我们在从数据构造的环境中对R表达式求值with(pf, cor.test(age, friend_count, method = 'pearson'))

利用子集对相关性的范围进行约束

# 如果对cor.test不指定method那么它默认使用pearson方法# 这里使用子集,必须要把pf写在前面with(subset(pf, age<70), cor.test(age, friend_count))

Pearson积矩关系默认衡量两个变量之间的关联强度,我们也有其他单调性关联强度的度量,比如等级相关度量,如Spearman

绘制具有强相关性的图像

# 使用xlim和ylim对相关性进行约束# 使用0作为下边界,使用收到点赞的95%作为上边界,# geom_smooth添加此平滑涂层,并设置为线性模型lm,ggplot(aes(x = www_likes_received, y = likes_received), data = pf) +   geom_point()+  xlim(0, quantile(pf$www_likes_received, 0.95))+  ylim(0, quantile(pf$likes_received, 0.95))+  geom_smooth(method = 'lm', color='red')

如果不加约束条件,直接绘制散点图效果如图所示:

这里写图片描述

添加约束条件之后散点图变为如下形式:

这里写图片描述

在 R 中,当参数没有名称时,参数匹配较为复杂

首先,参数可以通过名称进行匹配。如果一个参数精确匹配,会从参数列表中“删除”它,并且余下的未命名参数按照它们在函数定义中的顺序进行匹配。

R 通过以下方式匹配参数:

  • 检查命名参数的精确匹配
  • 检查参数的部分匹配
  • 检查位置匹配

如果 R 无法找到参数的匹配项,它通常会引发“未使用(unused)”参数错误。

数据叠加

土壤温度和时间数据中,统计的月份有203个月,希望将月份按一年为周期进行叠加处理显示,操作如下:

# %%代表求余,这样保证了余数始终在0到11之间,实现了在x轴的叠加ggplot(aes(x=(Month%%12),y=Temp),data=Mitchell)+   geom_point() 

效果如图所示:

这里写图片描述

参考资料

ggplot2 geoms详细展示:http://ggplot2.tidyverse.org/reference/

ggplot2教程:http://bbs.ceb-institute.org/

dplyr简介:https://www.r-bloggers.com/hadley-wickham-presents-dplyr-at-user-2014/

dplyr教程(第1部分):https://www.r-bloggers.com/hadley-wickhams-dplyr-tutorial-at-user-2014-part-1/

dplyr教程(第2部分):https://www.r-bloggers.com/hadley-wickhams-dplyr-tutorial-at-user-2014-part-2/

相关分析法: Pearson’s r, Spearman’s ρ 及 Kendall’s τ:http://www.statisticssolutions.com/correlation-pearson-kendall-spearman/

线性回归:https://en.wikipedia.org/wiki/Linear_regression#Assumptions

原创粉丝点击