您的位置:首页 > 其它

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

2018-04-11 09:13 120 查看
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文档中有详细的使用说明        
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: