今天写了四个关于R语言绘图的程序
1、hist画直方图
#读取数据
data<-read.table("household_power_consumption.txt",sep=";")
colname <- c()
for(x in data[1,])
{
colname = c(colname ,as.character(x))
}
colnames(data)<-colname
data <- data[-1,]
data1 <- data[,1]
i <- 1
#result <- 0
for(x in data1)
{
x <- as.Date(x, "%d/%m/%Y")
if(x == "2007-01-31")
{
begin <- i+1
}
if(x == "2007-02-02")
{
end <- i
}
i <- i+1
}
print(begin)
print(end)
intercept <- data[begin:end,]
options(warn=-1)
z <- intercept[,3]
a<-as.matrix(z)
a <- as.numeric(a)
hist(a,col="red",main = "Global Active Power",xlab="Global Active Power(kilowatts)",ylab="Frequency")
dev.copy(png, file = "plot1.png",width =480,height =480)
dev.off()
2、将点连成线显示
#读取数据
data<-read.table("household_power_consumption.txt",sep=";")
colname <- c()
for(x in data[1,])
{
colname = c(colname ,as.character(x))
}
colnames(data)<-colname
data <- data[-1,]
data1 <- data[,1]
i <- 1
#result <- 0
for(x in data1)
{
x <- as.Date(x, "%d/%m/%Y")
if(x == "2007-01-31")
{
begin <- i+1
}
if(x == "2007-02-02")
{
end <- i
}
i <- i+1
}
print(begin)
print(end)
intercept <- data[begin:end,]
da<-intercept[,1]
ti<-intercept[,2]
num <- end-begin+1
dati <- NULL
print(num)
for(x in 1:num)
{
dati<-c(dati,paste(as.character(da[x]),as.character(ti[x]),sep=" "))
}
e <- as.POSIXct(strptime(dati, format="%d/%m/%Y %H:%M:%S"))
#print(nrow(dati))
options(warn=-1)
active <- intercept[,3]
a<-as.matrix(active)
a <- as.numeric(a)
#print(nrow(a))
#hist(a,col="red",main = "Global Active Power",xlab="Global Active Power(kilowatts)",ylab="Frequency")
plot(e,a,type="l",ylab="Global Active Power(kilowatts)",xlab = "")
dev.copy(png, file = "plot2.png",width =480,height =480)
dev.off()
3、一个图中画多条线
#读取数据
data<-read.table("household_power_consumption.txt",sep=";")
colname <- c()
for(x in data[1,])
{
colname = c(colname ,as.character(x))
}
colnames(data)<-colname
data <- data[-1,]
data1 <- data[,1]
i <- 1
#result <- 0
for(x in data1)
{
x <- as.Date(x, "%d/%m/%Y")
if(x == "2007-01-31")
{
begin <- i+1
}
if(x == "2007-02-02")
{
end <- i
}
i <- i+1
}
print(begin)
print(end)
intercept <- data[begin:end,]
#extract three row data
s1<-intercept$Sub_metering_1
s2<-intercept$Sub_metering_2
s3<-intercept$Sub_metering_3
#draw graphics
plot(e,s1,type="l",xlab="",ylab="Energy sub metering")
lines(e,s2, type="l", pch=22, lty=1, col="red")
lines(e,s3, type="l", pch=22, lty=1, col="blue")
legend("topright", c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), cex=0.8, col=c("black","red","blue"), lty=1)
dev.copy(png, file = "plot3.png",width =480,height =480,bg="white")
dev.off()
4、将四个小图画到一个图中
#读取数据
data<-read.table("household_power_consumption.txt",sep=";")
colname <- c()
for(x in data[1,])
{
colname = c(colname ,as.character(x))
}
colnames(data)<-colname
data <- data[-1,]
data1 <- data[,1]
i <- 1
#result <- 0
for(x in data1)
{
x <- as.Date(x, "%d/%m/%Y")
if(x == "2007-01-31")
{
begin <- i+1
}
if(x == "2007-02-02")
{
end <- i
}
i <- i+1
}
print(begin)
print(end)
intercept <- data[begin:end,]
#相当于画布吧,2行2列
par(mfrow = c(2, 2))
#开始画了
active <- intercept[,3]
a<-as.matrix(active)
a <- as.numeric(a)
plot(e,a,type="l",ylab="Global Active Power(kilowatts)",xlab = "")
vol <- intercept[,5]
b<-as.matrix(vol)
b <- as.numeric(b)
plot(e,b,type="l",ylab="Voltage",xlab = "")
s1<-intercept[,7]
s2<-intercept[,8]
s3<-intercept[,9]
plot(e,s1,type="l",xlab="",ylab="Energy sub metering")
lines(e,s2, type="l", pch=22, lty=1, col="red")
lines(e,s3, type="l", pch=22, lty=1, col="blue")
legend("topright", c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), cex=0.8, col=c("black","red","blue"), lty=1)
vol <- intercept[,4]
d<-as.matrix(vol)
d <- as.numeric(d)
plot(e,d,type="l",xlab="",ylab="Global_reactive_power")
dev.copy(png, file = "plot4.png",width =480,height =480,bg="white")
dev.off()
参考链接:http://blog.sina.com.cn/s/blog_5de124240101q5vw.html
http://blog.sina.com.cn/s/blog_5de124240101pzqb.html