ggplot2图集汇总(一)

欢迎关注天善智能,我们是专注于商业智能BI,人工智能AI,大数据分析与挖掘领域的垂直社区,学习,问答、求职一站式搞定!

对商业智能BI、大数据分析挖掘、机器学习,python,R等数据领域感兴趣的同学加微信:tstoutiao,邀请你进入数据爱好者交流群,数据爱好者们都在这儿。

作者:路遥马亡  R语言中文社区专栏作者

知乎ID:https://zhuanlan.zhihu.com/c_135409797

前言

上次推文小白R语言数据可视化进阶练习一汇总了一部分的图集,这次推文接上一篇再次汇总,此图集汇总将不断更新!

08

相关图

相关图,通常分析多个因素之间的相关性时都会计算相关性系数,通过作图的方式,让相关性可视化,更利于数据分析。

1library(GGally)

2

3# Create data 

4sample_data <- data.frame( v1 = 1:100 + rnorm(100,sd=20), v2 = 1:100 + rnorm(100,sd=27), v3 = rep(1, 100) + rnorm(100, sd = 1))

5sample_data$v4= sample_data$v1** 2

6sample_data$v5= -(sample_data$v1** 2)

7

8# Check correlation between variables

9cor(sample_data)#计算相关性系数

10

11# Check correlations (as scatterplots), distribution and print corrleation coefficient 

12ggpairs(sample_data)#上三角表示各个因素之间的相关性系数,对角线就是各个因素的密度图,

13#下三角就是任意两个元素绘成的散点图

14

15# Nice visualization of correlations

16ggcorr(sample_data, method = c("everything","pearson"),label = T)

3901436-d126aabb56928d01
3901436-65e6844f97ff70cc

1# Libraries

2library(ellipse)

3library(RColorBrewer)

4

5# Use of the mtcars data proposed by R

6data=cor(mtcars)

7

8# Build a Pannel of 100 colors with Rcolor Brewer

9my_colors <- brewer.pal(5,"Spectral")#需要5个“spectral”色系的颜色

10my_colors=colorRampPalette(my_colors)(100)#将数值映射到不同的颜色上,这时就需要一系列的颜色梯度,

11#100代表100种颜色,根据之前的五种基本色,调处100种新的颜色。

12

13# Order the correlation matrix

14ord <- order(data[1, ])

15data_ord = data[ord, ord]#根据第一个因素与其他因素的相关系数大小调整原矩阵

16plotcorr(data_ord , col=my_colors[data_ord*50+50], mar=c(0,0,0,0 )   )#mar()用于调整图形整体大小

17

18#这个图挺有意思的,椭圆越瘪,相关性越强

3901436-d6864b200ca55333

下面讲一点图外话,如何利用R绘画表格并把它放入图中(主要是学了大半天,发现这个和相关图并没有什么关系,但是还是放进来了,不喜欢的直接跳过)

1library(ggplot2)

2library(gridExtra)

3

4#Create data : we take a subset of the mtcars dataset provided by R:

5mydata <- data.frame(a=1:50, b=rnorm(50))

6mytable <- cbind(sites=c("site 1","site 2","site 3","site 4"),mydata[10:13,])

7

8# --- Graph 1 : If you want ONLY the table in your image :

9# First I create an empty graph with absolutely nothing :

10qplot(1:10,1:10, geom ="blank") + theme_bw() + theme(line = element_blank(), text = element_blank()) +

11# Then I add my table :

12annotation_custom(grob = tableGrob(mytable))

13#法二

14library(grid)

15d<-head(iris,3)

16g<-tableGrob(d)

17grid.newpage()

18grid.draw(g)

19

20

21# --- Graph 2 : If you want a graph AND a table on it :

22my_plot <- ggplot(mydata,aes(x=a,y=b)) + geom_point(colour="blue") +   geom_point(data=mydata[10:13, ], aes(x=a, y=b), colour="red", size=5) +

23annotation_custom(tableGrob(mytable), xmin=35, xmax=50, ymin=-3, ymax=-1.5)

24my_plot

3901436-c42c39c39168849e
3901436-32b0fc6b6e1ef36d

09

气泡图

气泡图可将三维变量反映在二维平面上,第三位用点的大小表示。有个不足就是如果数据过多,很多气泡会出现重叠,难以达到预期的效果。

1library(ggplot2)

2library(tidyverse)

3library(dplyr)

4

5# Let's use the diamonds data set (available in base R)

6data = diamonds %>% sample_n(200)

7

8# A basic scatterplot = relationship between 2 values:

9ggplot(data, aes(x=carat, y=price)) +

10geom_point()

11

12# Now we see there is a link between caract and price

13# But what if we want to know about depth in the same time?

14ggplot(data, aes(x=carat, y=price, size=depth)) +

15geom_point(alpha=0.2)

3901436-06cd00d3d4133790

即使是气泡图,各个数据间的大小比较并不是很明显,所以需要时使用scale_size_continuous()函数。

1ggplot(data, aes(x=carat, y=price, size=depth)) +

2geom_point(alpha=0.2) +

3scale_size_continuous(range = c(0.5, 15))#控制最大气泡和最小气泡,调节气泡相对大小

3901436-41f4cd6586d596ad

1# Note that you can add a transformationtoyour size variable.

2#Forexampleifyou wanttohighlight very high variables, you can use a exponential transformation.

3# Available:"asn","atanh","boxcox","exp","identity","log","log10","log1p","log2","logit","probability","probit","reciprocal","reverse"and"sqrt"

4ggplot(data, aes(x=carat, y=price, size=depth)) +

5geom_point(alpha=0.2) +

6scale_size_continuous( trans="exp", range=c(1,25))#转化为指数,这样可以把大小差距拉开

3901436-55e6fd7fde2b2219

也可以通过颜色的深浅导入第四个变量,但似乎效果不是很好

1ggplot(data, aes(x=carat, y=price, size=depth,color=carat)) +

2geom_point(alpha=0.4) +

3scale_size_continuous( trans="exp",range=c(1,25))

3901436-7546f5db8dc82b36

10

折线图

这里主要介绍用于时间序列的折线图。

1# library

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值