R语言绘图

今天写了四个关于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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值