###################21-22包、帮助####################
#安装:install.packages('包名')
#加载:library(包名)
#卸载:remove.packages('包名')

help.start()
help(sum)#函数
?sum

library(dplyr)#包
help(dplyr)
help(package = dplyr)
??dplyr

#example(函数名)获取函数的案例并且自动运行
#eg:“persp”是R语言三维图像绘制函数
example('persp')

par(mfrow = c(2,2))
example('persp')

###################23.基本可视化绘图####################
#1、散点图(plot)
x<-c(1:8)
y<-x+2.5
plot(x,y)
plot(x,y,type = 'l')#type:画图类型;
cars
plot(cars)
plot(cars,main = '测视图')
names(cars)
plot(cars,main = '测视图',xlab = '速度',ylab = '距离')
plot(cars,main = '测视图',xlab = '速度',ylab = '距离',col = 'red')
#2、条形图(barplot)
barplot(c(1,2,3))
barplot(c(88,79,99),names.arg = c('小明','小虎','小狗'))#names.arg:变量名;
barplot(c(88,79,99),names.arg = c('小明','小虎','小狗'),ylim = c(0,100))
barplot(c(88,79,99),legend.text = c('小明','小虎','小狗'),ylim = c(0,100))#legend.text:变量标签;
barplot(c(88,79,99),legend.text = c('小明','小虎','小狗'),ylim = c(0,100),col = rainbow(3))
barplot(c(88,79,99),legend.text = c('小明','小虎','小狗'),ylim = c(0,100),col = rainbow(3),horiz = TRUE)#horiz:是否垂直;
barplot(c(88,79,99),legend.text = c('小明','小虎','小狗'),col = rainbow(3),horiz = TRUE)
barplot(c(88,79,99),legend.text = c('小明','小虎','小狗'),xlim = c(0,100),col = rainbow(3),horiz = TRUE)
#3、直方图(hist)
iris
hist(iris[,1])
hist(iris[,1],freq = TRUE)#freq:是否画频率分布直方图;
hist(iris[,1],freq = FALSE,col = rainbow(8))#密度
#4、饼图(pie)
pie(c(1,2,3))
pie(c(1,2,3),labels = c('中国','美国','俄罗斯'))
pie(c(1,2,3),labels = c('中国','美国','俄罗斯'),radius = 0.5)
#5、箱线图(boxplot)
boxplot(iris[,1])
boxplot(iris[,1],notch = TRUE)#notch:是否有缺口;
boxplot(iris[,1],notch = TRUE,)
head(iris)
boxplot(iris[,c(1:4)])
boxplot(iris[,c(1:4)],col = rainbow(4))
boxplot(iris[,c(1:4)],col = rainbow(4),names = c('sl','sw','pl','pw'))#names:变量名;
#par()开窗口
par(mfrow=c(2,2))
plot(c(1:3))
pie(c(1:3))
barplot(c(1:3))
boxplot(iris[,1])

###################24-25qplot()作图及批量生成、保存图片####################
#install.packages('ggplot2')
library(ggplot2)
attach(diamonds)
names(diamonds)
dim(diamonds)
dat<-diamonds[sample(nrow(diamonds),200),]
dat
plot(dat$carat,dat$price)
qplot(dat$carat,dat$price)
qplot(log(dat$carat),log(dat$price))
#属性 颜色:colour 形状:shape 大小:alpha=I(1/10)
unique(color)
qplot(carat,price,data = dat,colour = color)
dat$color
unique(dat$cut)
qplot(carat,price,data = dat,shape = cut)
qplot(carat,price,data = dat,colour = color,alpha = I(1/5))#点重叠颜色加深
#几何对象 geom:point boxplot line(线图) bar(柱状图)
qplot(carat,price,data = dat,geom = 'point')
qplot(carat,price,data = dat,geom = 'boxplot')
qplot(cut,price,data = dat,geom = 'boxplot')
qplot(cut,price,data = dat,geom = 'boxplot',colour = cut)
qplot(carat,price,data = dat,geom = 'line')
qplot(color,data = dat,geom = 'bar')
qplot(color,data = dat,geom = 'bar',colour = color)
#批量生成、保存图片
barplot(c(1:4),col = rainbow(4))
#setwd("B:/R/document/图片")
getwd()
jpeg('1.jpg')
plot(c(1,2,3,4,5))
dev.off()

jpeg('1.png')
barplot(c(1:5))
dev.off()

d<-c(1,2,3,2)
for(i in c(1:10)){
  filename<-paste0(i,'.jpg')
  jpeg(filename)
  plot(d)
  dev.off()
}

###################26-29txt文件读取、写入,csv文件(逗号分隔),xlsx文件####################
#txt文件读取、写入
dat<-read.table('1.txt',sep = ' ',header = TRUE)#header:第一行是否是数据 默认false
dat
dat2<-read.table('2.txt',sep = ',',header = FALSE)
dat2

write.table(dat,'11.txt',sep = ' ')
write.table(dat2,'22.txt',sep = ',')
write.table(dat2,'22.txt',sep = ',',col.names = FALSE,row.names = FALSE)
class(dat)#[1] "data.frame"
write.table(dat2,'33.txt',sep = '|____|',col.names = FALSE,row.names = FALSE)

#csv文件(逗号分隔)
dat2
read.csv('2.txt',header = FALSE)
write.csv(dat2,'2.csv',row.names = FALSE)

#xlsx文件(依赖包:rjava)
#需要安装java编译环境
#安装java:去java官网下载jdk,安装;
#配置环境变量:
#(1)新建系统变量JAVA_HOME;变量值为jdk的路径比如我习惯性的路径:D:\Program Files\Java\jdk1.7.0_79;
#(2)在PATH变量后加入:%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;末尾最好有分号,防止下次加时漏掉分号,养成好习惯;
#(3)在命令行窗口输入javac,会出现相关目录表示配置成功。
#(4)R命令install.packages(rjava), install.packages(xlsx)
#读文件:dat <- read.xlsx('df.xlsx',sheetIndex = 1)
#写文件:write.xlsx(dat,’test.xlsx’)

 

posted on 2020-04-18 10:24  晨曦生辉耀匕尖  阅读(337)  评论(0编辑  收藏  举报