[mac] R入门 四 数据可视化

1,plot()

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文档中有详细的使用说明        



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值