[mac] R入门 四 数据可视化
2018-04-11 09:13
120 查看
1,plot()
2,使用ggplot2包,需要安装。install.packages("ggplot2")
library(ggplot2)
qplot(disp, mpg, ylim=c(0,35), data=mtcars) #ylim可以改变默认的坐标的起始点
qplot(cty, hwy, data=mpg, geom="jitter") #geom"jitter"引入了一些随机噪声点
ggplot(mtcars, aes(x=disp, y=mpg), ylim=0) + gem_line() + geom_point() #aes代表视图特性,geom是绘图几何,线,点等3,条形图(Bar graphs)barplot(BOD$demand, main="Graph of demand", name.arg = BOD$Time) #BOD的数据是R中自带的example数据,demand是相关的列。main可以用于设置图的标题。arg设置横坐标的
cylcount <- table(mtcars$cyl) #table()可用于统计相关列的数据个数。
barplot(cylcount) #画饼状图
qplot(factor(mtcars$cyl)) #方法二
ggplot(mtcars, aes(factor(cyl))) + geom_bar() #方法三
4,直方图hist(mydata$columnName, breaks = n) #columnName是列名
qplot(columname, data=mydata, binwidth=n)
ggplot(mydata, aes(x=columnName)) + geom_histogram(binwidth=n)
5,一些其他的图boxplot(mtcars$mpg) #展现最小值,最大值,第一四分位,带三四分位,中间值
boxplot(diamonds$x, diamonds$y, diamonds$z)corrplot package可以用来画概率分布图
6,使用颜色colors() 和 colours()#生成n个颜色
rainbow(n)
heat.colors(n)
terrain.colors(n)
topo.colors(n)
cm.colors(n)
ggplot(mtcars, aes(x=factor(cyl))) + geom_bar(fill=rainbow(3))
barplot(BOD$demand, col=rainbow(6)) #基本的绘图函数也可以使用颜色设置
barplot(BOD$demand, col="royalblue3") #使用单一的颜色
#example
testscores <- c(96, 71, 85, 92, 82, 78, 71, decreasing = TRUE) #使数据从大到小排列
#or
testscores <- c(96, 71, 85, 92, 82, 78, 71)
testscores_sorted <- sort(testscores, decreasing = TRUE)
barplot(testscores)
barplot(testscores, col="blue")
testcolors <- ifelse(testscores >= 80, "blue", "red") #设定颜色
barplot(testscores, col=testcolors, main="Test scores", ylim=c(0,100), las=1)
ggplot(results, aes(x=students, y=testscores)) + geom_bar(fill=testcolors, stat = "identity") #identity是强调y轴的是数值不是序号
qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(cyl)) #比较简单的通过qplot中的factor生成带颜色的条状图7,输出plot
(1)软件中点击按钮直接输出
(2)代码输出jpeg("myplot.jpg", width=350, height=420)
barplot(BOD$demand, col=rainbow(6))
dev.off() #存储画到container中
ggsave() #help文档中有详细的使用说明
plot(mtcars$disp, mtcars$mpg, xlab="Engine displacement", ylab="mpg", main="MPG compared with engine displacement", las=1) #x,y,x、y轴的lable,main是设置标题的,las=1,lable style设置使y轴的刻度旋转90度数字更适合阅读,默认散点图还有一些包可以选择安装,eg,biostatistics,finance。。。
2,使用ggplot2包,需要安装。install.packages("ggplot2")
library(ggplot2)
qplot(disp, mpg, ylim=c(0,35), data=mtcars) #ylim可以改变默认的坐标的起始点
qplot(cty, hwy, data=mpg, geom="jitter") #geom"jitter"引入了一些随机噪声点
ggplot(mtcars, aes(x=disp, y=mpg), ylim=0) + gem_line() + geom_point() #aes代表视图特性,geom是绘图几何,线,点等3,条形图(Bar graphs)barplot(BOD$demand, main="Graph of demand", name.arg = BOD$Time) #BOD的数据是R中自带的example数据,demand是相关的列。main可以用于设置图的标题。arg设置横坐标的
cylcount <- table(mtcars$cyl) #table()可用于统计相关列的数据个数。
barplot(cylcount) #画饼状图
qplot(factor(mtcars$cyl)) #方法二
ggplot(mtcars, aes(factor(cyl))) + geom_bar() #方法三
4,直方图hist(mydata$columnName, breaks = n) #columnName是列名
qplot(columname, data=mydata, binwidth=n)
ggplot(mydata, aes(x=columnName)) + geom_histogram(binwidth=n)
5,一些其他的图boxplot(mtcars$mpg) #展现最小值,最大值,第一四分位,带三四分位,中间值
boxplot(diamonds$x, diamonds$y, diamonds$z)corrplot package可以用来画概率分布图
6,使用颜色colors() 和 colours()#生成n个颜色
rainbow(n)
heat.colors(n)
terrain.colors(n)
topo.colors(n)
cm.colors(n)
ggplot(mtcars, aes(x=factor(cyl))) + geom_bar(fill=rainbow(3))
barplot(BOD$demand, col=rainbow(6)) #基本的绘图函数也可以使用颜色设置
barplot(BOD$demand, col="royalblue3") #使用单一的颜色
#example
testscores <- c(96, 71, 85, 92, 82, 78, 71, decreasing = TRUE) #使数据从大到小排列
#or
testscores <- c(96, 71, 85, 92, 82, 78, 71)
testscores_sorted <- sort(testscores, decreasing = TRUE)
barplot(testscores)
barplot(testscores, col="blue")
testcolors <- ifelse(testscores >= 80, "blue", "red") #设定颜色
barplot(testscores, col=testcolors, main="Test scores", ylim=c(0,100), las=1)
ggplot(results, aes(x=students, y=testscores)) + geom_bar(fill=testcolors, stat = "identity") #identity是强调y轴的是数值不是序号
qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(cyl)) #比较简单的通过qplot中的factor生成带颜色的条状图7,输出plot
(1)软件中点击按钮直接输出
(2)代码输出jpeg("myplot.jpg", width=350, height=420)
barplot(BOD$demand, col=rainbow(6))
dev.off() #存储画到container中
ggsave() #help文档中有详细的使用说明
相关文章推荐
- Python数据可视化-Matplotlib学习笔记(1)--折线图为例画图入门
- 基于Python数据可视化利器Matplotlib,绘图入门篇,Pyplot详解
- 数据可视化的优秀入门书籍有哪些,D3.js 学习资源汇总
- 读书笔记数据科学入门————可视化数据
- 数据可视化入门(一)
- 数据可视化入门(二)
- 数据可视化入门:柱状图、雷达图等六种基本图表的特点和适用场合
- 数据可视化——Matplotlib模块入门(一)
- 数据可视化(三)- Seaborn简易入门
- 利用tableau数据可视化工具创建圆环图——初入门篇
- Echarts助力大数据绘制可视化图表零基础入门-针对运维
- [Mac] R入门 三 基础数据分析的一些简单方法
- 数据科学入门—数据可视化
- [置顶] cesium学习记录(-)- 可视化空间数据Entity入门基础
- Python数据可视化利器Matplotlib,绘图入门篇,Pyplot介绍
- 数据可视化(一)-Matplotlib简易入门
- d3.js:数据可视化利器之快速入门
- 《数据图形化,分析更给力》:数据可视化入门级图书,新手可以翻翻
- 数据可视化 -- 入门(draft version)
- 数据可视化难在哪里?该如何入门