r 语言ylim = c(0 1),小白R语言数据可视化进阶练习一
原标题:小白R语言数据可视化进阶练习一
作者:路遥马亡R语言中文社区专栏作者
知乎ID:
https://zhuanlan.zhihu.com/c_135409797
00
布局参数
先介绍一个布局参数:
#par(mfrow=c(a,b)) #表示在PLOTS区域显示a行b列张图 par(mfrow=c(3,1)) x
01
VIOLIN PLOT(小提琴图)
下面进入正题,第一个要讲解的是VIOLIN PLOT,中文名为小提琴图。
它用于可视化数值型变量,与箱线图十分类似,但加入了密度分布这个属性,更能够显示数据的数据的数字特征。
# Library library(ggplot2) # mtcars data head(mtcars) # First type of color ggplot(mtcars, aes(factor(cyl), mpg)) + geom_violin(aes(fill = cyl)) # Second type ggplot(mtcars, aes(factor(cyl), mpg)) + geom_violin(aes(fill = factor(cyl)))
02
DENSITY PLOT(核密度图)
第二个分享的是DENSITY PLOT,中文名为核密度图,与直方图类似,但是可以在图中反映多组数据的分布情况:
# For the weatherAUS dataset. library(rattle) # To generate a density plot. library(ggplot2) , cities
# plot 1: Density of price for each type of cut of the diamond: ggplot(data=diamonds,aes(x=price, group=cut, fill=cut)) + geom_density(adjust=1.5) #adjust起到了调节曲线拟合程度的一个作用,默认参数为1 # plot 2: 归一化后的叠加图: ggplot(data=diamonds,aes(x=price, group=cut, fill=cut)) + geom_density(adjust=1.5, position="fill")
# plot 3 ggplot(diamonds, aes(x=depth, y=..density..)) + geom_density(aes(fill=cut), position="stack") + xlim(50,75) + theme(legend.position="none")#去掉图例
03
geom_boxplot()
第三个跟大家分享的是箱线图,geom_boxplot(),可以清晰地表现数据的极大值和极小值以及中位数:
library(ggplot2) head(mtcars) # A really basic boxplot. ggplot(mtcars, aes(x=as.factor(cyl), y=mpg)) + geom_boxplot(fill="slateblue", alpha=0.2) + xlab("cyl")+ ylab("mpg") # Set a unique color with fill, colour, and alpha ggplot(mpg, aes(x=class, y=hwy)) + geom_boxplot(color="red", fill="orange", alpha=0.2) # Set a different color for each group ggplot(mpg, aes(x=class, y=hwy, fill=class)) + geom_boxplot(alpha=0.3) + theme(legend.position="none")
#配合scale_fill_brewer()使用,方便调颜色 library(RColorBrewer) display.brewer.all()
ggplot(mpg, aes(x=class, y=hwy, fill=class)) + geom_boxplot(alpha=0.3) + theme(legend.position="none") + scale_fill_brewer(palette="BuPu")
ggplot(mpg, aes(x=class, y=hwy, fill=class)) + geom_boxplot(alpha=0.3) + theme(legend.position="none") + scale_fill_brewer(palette="Dark2")
如果你想单独强调某一组数据或者某几组数据:
# Create a new column, telling if you want to highlight or not mpg$type=factor(ifelse(mpg$class=="subcompact","Highlighted","Normal")) # control appearance of groups 1 and 2 ggplot(mpg, aes(x=factor(class), y=hwy, fill=type, alpha=type)) + geom_boxplot() + #手动设置透明度,取决于你赋值给fill的变量个数 scale_alpha_manual(values=c(0.1,1)) + #手动设置颜色,取决与你赋值给fill的变量个数 scale_fill_manual(values=c("forestgreen","red"))
有时候我们也需要通过调整一些参数,使一些极端值更加清晰地展现出来:
ggplot(mpg, aes(x=class, y=hwy)) + geom_boxplot( # custom boxes color="blue", fill="blue", alpha=0.2, # Notch? notch=TRUE, notchwidth = 0.8, # custom outliers outlier.colour="red", outlier.fill="red", outlier.size=3 )
04
构造词云
第四个分享的是如何用R语言构造词云:
library(wordcloud2) wordcloud2(demoFreq,size=0.8,color=rep_len(c("blue","yellow"),nrow(demoFreq)),backgroundColor = "pink",shape="star")
词的角度也可以更改:
#minRontatin与maxRontatin:字体旋转角度范围的最小值以及最大值,选定后,字体会在该范围内随机旋转; #rotationRation:字体旋转比例,如设定为1,则全部词语都会发生旋转; wordcloud2(demoFreq,size=0.8,color=rep_len(c("blue","yellow"),nrow(demoFreq)),backgroundColor = "pink",minRotation = -pi/6, maxRotation = -pi/6, = 1)
以上只是简单的讲解下词云构造,关于更详细的可以看这篇:如何用R语言做词云图,以某部网络小说为例
05
直方图
第五个讲讲直方图的画法,只需要输入一个数值型变量:
library(ggplot2) data
我们也可以自己设置不同的主题的背景:
# library library(ggplot2) # create data set.seed(123) var=rnorm(1000) # Without theme plot1
06
散点图
第六个讲讲一个非常简单实用的图——散点图。
先看看最简单的,不加任何修饰的画法:
# library library(ggplot2) # The iris dataset is proposed by R head(iris) # basic scatterplot ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) + geom_point()
# use options! #注意stroke表示散点外线的粗细 ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) + geom_point( color="red", fill="blue", shape=21, alpha=0.5, size=6, stroke = 2 )
#用shape和fill参数,把不同类别的length和width区分开来。 ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species, shape=Species)) + geom_point(size=6, alpha=0.6)
#此处并不能理解为什么color又负责填充色了,fill加了之后反而没有用。先死记吧。 ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Petal.Length, size=Petal.Length)) + geom_point(alpha=0.6)
也可以通过一些函数给散点添加标签:
data=head(mtcars, 30) #nudge_x,nudge_y分别表示标签距离散点距离,check_overlap表示标签出现覆盖,是否去掉被覆盖的那个标签。 # 1/ add text with geom_text, use nudge to nudge the text ggplot(data, aes(x=wt, y=mpg)) + geom_point() + geom_text(label=rownames(data), nudge_x = 0.25, nudge_y = 0.25, check_overlap = T)
ggplot(data, aes(x=wt, y=mpg)) + geom_point() + geom_label(label=rownames(data), nudge_x = 0.25, nudge_y = 0.2) # 3/ custom geom_label like any other geom. ggplot(data, aes(x=wt, y=mpg, fill=cyl)) + geom_label(label=rownames(data),color="white", size=5)
散点图是两个变量的值沿着两个轴绘制的图,所得到的点的模式揭示了存在的任何相关性。你可以很容易地在X轴和Y轴上添加rug,来说明点的分布:
ggplot(data=iris, aes(x=Sepal.Length, Petal.Length)) + geom_point() + geom_rug(col="skyblue",alpha=0.1, size=1.5)
当然,也可以通过增加折线图,直方图,箱线图来表示数据分布情况:
# library library(ggplot2) library(ggExtra) library(gridExtra) # The mtcars dataset is proposed in R head(mtcars) # classic plot : p=ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, size=cyl)) + geom_point() + theme(legend.position="none") # with marginal histogram a=ggMarginal(p, type="histogram") # marginal density b=ggMarginal(p, type="density") # marginal boxplot c=ggMarginal(p, type="boxplot") grid.arrange(p,a,b,c,ncol=2)
最后讲讲线性拟合:
library(ggployt2) data
07
热力图
最后分享一下热力图的画法:
# Lattice package require(lattice) #The lattice package provides a dataset named volcano. It's a square matrix looking like that : head(volcano) # The use of levelplot is really easy then : levelplot(volcano) #注意输入数据为矩阵,行列表示坐标,数值表示显色深浅。
## Example data x
- END -
小编语:
本篇是根据作者在知乎分享的R语言数据可视化周计划的内容整理而来。最后祝大家圣诞节快乐!
责任编辑:
r 语言ylim = c(0 1),小白R语言数据可视化进阶练习一相关推荐
- R语言入门第四集 实验三:数据可视化
R语言入门第四集 实验三:数据可视化 一.资源 [R语言]R语言数据可视化--东北大学大数据班R实训第三次作业 在r中rowsums_R语言初级教程(15): 矩阵(下篇) R语言环境变量的设置 环境 ...
- 小白的网络安全数据可视化笔记(一)
小白的网络安全数据可视化笔记 网络安全数据可视化 网络安全数据可视化综述 一.网络安全数据介绍 二,数据可视化的研究方向是什么 1.网络监控 2.异常检测 3.特征分析 4.关联分析 大规模网络安全数 ...
- r 语言ylim = c(0 1),今日R--条形图(barplot)
par(mfrow = c(1,1)) #布局 barplot(phylum_mean_1,col=brewer.pal(24,"Set3"),legend=rownames(ph ...
- 测评三个数据分析工具后,我终于找到了小白都会的数据可视化
曾经很多人问我,数据可视化重要吗?我们先来看一张图片. 以上这张图片比较普遍现象的数据链路.如果你是厨师,最重要的肯定是做菜和摆盘环节,也就是数据分析和数据可视化环节. 打个比方,很多人现在对菜的摆盘 ...
- c语言字符为0和1,//C语言:将一个由字符0和1组成的表示二进制数的字符串,转换成相应的十进制数返回。...
//函数fun:将一个由字符0和1组成的表示二进制数的字符串,转换成相应的十进制数返回. #include #pragma warning (disable:4996) #define N 16 /* ...
- c语言自动输入0到1000,c语言实现输入一组数自动从大到小排列
#include main() { int x; printf("请输入要排序数字个数:"); scanf("%d",&x); int i,j,k,a, ...
- c语言 vc++6.0 插入图片,C语言VC++6.0环境中如何插入图片
getimage / putimage / loadimage / saveimage 这一组命令和 IMAGE 对象可以实现图像处理的相关功能,下面逐个介绍. (有点类似 tc 中的 imagesi ...
- C语言余数为0输出intact,C语言中的宏是怎么展开的?
展开流程伪码:(自己总结的,不一定对) //loop: //将实参代入文本中 //if 在某个实参之前有符号"#"(字符串化)或"##"(连接)then // ...
- c语言如何输出0,怎么用c语言打印逆置整数,零不输出?
MinRam 只是大概思路,代码并未作测试.存储后输出:按照整数来处理:int num,temp; scanf("%d",&num); if(num<0){ ...
最新文章
- 纪念乔帮主-Jobes的生平视频
- Java内置函数的理解和加减乘除四个函数的学习
- CGI方式获取RTX中用户的电话和邮箱
- Android-----Resources
- SQL 学习最强刷题网站!
- ps中将图片拖不进ps的编辑区的解决方法
- File System Auditor 安装全程截图
- Linux 系统级开启文件句柄 调优
- php使用redis作为消息队列
- VMware的配置问题集锦(更新中......)
- [HTML] Prettify 代码高亮使用总结
- Java——NIO和IO的区别
- 【Tkinter】常用组件(二)
- 5G,仅仅是更快的网速吗?
- 小觅摄像头 VINS-MONO安装
- python中spider的用法_Scrapy Spider类的用法
- popupwindow拦截点击物理返回键
- 炒菜机器人的弊端_炒菜机器人用不了?那我就来个暴力拆解
- CentOS 7下安装Google Chrome浏览器
- java和js实现省市县级连