现在的位置: 首页 > 综合 > 正文

R Graph cookbook代码(chapter 1-4)

2018年02月19日 ⁄ 综合 ⁄ 共 16614字 ⁄ 字号 评论关闭
#CHAPTER 1
#Recipe 1. 散点图
plot(cars$dist~cars$speed)
plot(cars$dist~cars$speed, # y~x
main="Relationship between car distance & speed", #Plot Title
xlab="Speed (miles per hour)", #X axis title
ylab="Distance travelled (miles)", #Y axis title
xlim=c(0,30), #Set x axis limits from 0 to 30 ylim=c(0,140), #Set y axis limits 
from 0 to  30140  xaxs="i", #Set x axis style as internal 
yaxs="i", #Set y axis style as internal  
col="red", #Set the colour of plotting 
symbol to red pch=19) #Set the plotting symbol to 
filled dots

#Recipe 2. 折线图
sales  <- read.csv("dailysales.csv",header=TRUE)
plot(sales$units~as.Date(sales$date,"%d/%m/%y"),
type="l", #Specify type of plot as l for line
main="Unit Sales in the month of January 
2010",
xlab="Date",
ylab="Number of units sold",
col="blue")

lines(sales$units2~as.Date(sales$date,"%d/%m/%y"),col="red")



#Recipe 3. 条形图
sales<-read.csv("citysales.csv",header=TRUE)

#Vertical bars
barplot(sales$ProductA,names.arg= sales$City,col="black")
#Horizontal bars
barplot(sales$ProductA,names.arg= sales$City,horiz=TRUE,col="black")
#Grouped bars with legend
barplot(as.matrix(sales[,2:4]), beside= TRUE,legend=sales$City,col=heat.colors(5),border="white")
#Horizontal grouped bars with legend
barplot(as.matrix(sales[,2:4]),beside=TRUE,legend=sales$City,col=heat.colors(5),border="white",horiz=TRUE)

#Recipe 4. 直方图/密度曲线图
hist(rnorm(1000))
hist(islands)
plot(density(rnorm(1000)))

#Recipe 5.箱线图
metals<-read.csv("metals.csv",header=TRUE)
boxplot(metals,xlab="Metals",ylab="Atmospheric Concentration in ng per cubic metre",main="Atmospheric Metal Concentrations in London")
copper<-read.csv("copper_site.csv",header=TRUE)
boxplot(copper$Cu~copper$Source,xlab="Measurement Site",ylab="Atmospheric Concentration of Copper in ng per cubic metre",
main="Atmospheric Copper Concentrations in London")

#Recipe 6. 调整横纵坐标界限Adjusting X and Y axis limits
plot(cars$dist~cars$speed,xlim=c(0,30),ylim=c(0,150))
plot(cars$dist~cars$speed,xlim=c(0,30),ylim=c(0,150),xaxs="i",yaxs="i")
plot(cars$dist~cars$speed,xlim=c(30,0),ylim=c(0,150),xaxs="i",yaxs="i")

#Recipe 7. 热点图Creating heatmaps
heatmap(as.matrix(mtcars), Rowv=NA,Colv=NA,col = heat.colors(256),scale="column",margins=c(2,8),main = "Car characteristics by Model")
genes<-read.csv("genes.csv",header=T)
rownames(genes)<-colnames(genes)
image(x=1:ncol( genes),y=1:nrow(genes),z=t(as.matrix(genes)),axes=FALSE,xlab="",ylab="" ,main="Gene Correlation Matrix")
axis(1,at=1:ncol(genes),labels=colnames(genes),col="white",las=2,cex.axis=0.8)   
axis(2,at=1:nrow(genes),labels=rownames(genes),col="white",las=1,cex.axis=0.8)

#Recipe 8. 矩阵图Creating pairs plots
pairs(iris[,1:4])
plot(iris[,1:4],main="Relationships between characteristics of iris flowers",pch=19,col="blue",cex=0.9)

#Recipe 9. 矩阵图Creating multiple plot matrix layouts
par(mfrow=c(2,3))
plot(rnorm(100),col="blue",main="Plot No.1")
plot(rnorm(100),col="blue",main="Plot No.2")
plot(rnorm(100),col="green",main="Plot No.3")
plot(rnorm(100),col="black",main="Plot No.4")
plot(rnorm(100),col="green",main="Plot No.5")
plot(rnorm(100),col="orange",main="Plot No.6")
par(mfcol=c(2,3))
plot(rnorm(100),col="blue",main="Plot No.1")
plot(rnorm(100),col="blue",main="Plot No.2")
plot(rnorm(100),col="green",main="Plot No.3")
plot(rnorm(100),col="black",main="Plot No.4")
plot(rnorm(100),col="green",main="Plot No.5")
plot(rnorm(100),col="orange",main="Plot No.6")
market<-read.csv("dailymarket.csv",header=TRUE)
par(mfrow=c(3,1))
plot(market$revenue~as.Date(market$date,"%d/%m/%y"),type="l", #Specify type of plot as l for line
main="Revenue",xlab="Date",ylab="US Dollars",col="blue")
plot(market$profits~as.Date(market$date,"%d/%m/%y"),type="l", #Specify type of plot as l for line
main="Profits",xlab="Date",ylab="US Dollars",col="red")
plot(market$customers~as.Date(market$date,"%d/%m/%y"),type="l", #Specify type of plot as l for line
main="Customer visits",xlab="Date",ylab="Number of people",col="black")

#Recipe 10. 添加图例Adding and formatting legends

rain<-read.csv("cityrain.csv",header=TRUE)
plot(rain$Tokyo,type="l",col="red",ylim=c(0,300),main="Monthly Rainfall in major cities",
xlab="Month of Year",ylab="Rainfall (mm)",lwd=2)
lines(rain$NewYork,type="l",col="blue",lwd=2)
lines(rain$London,type="l",col="green",lwd=2)
lines(rain$Berlin,type="l",col="orange",lwd=2) 
legend("topright",legend=c("Tokyo","New York","London","Berlin"),
col=c("red","blue","green","orange"),lty=1,lwd=2)

plot(rain$Tokyo,type="l",col="red",ylim=c(0,250),main="Monthly Rainfall in major cities",
xlab="Month of Year",ylab="Rainfall (mm)",lwd=2)
lines(rain$NewYork,type="l",col="blue",lwd=2)
lines(rain$London,type="l",col="green",lwd=2)
lines(rain$Berlin,type="l",col="orange",lwd=2)
legend("top",legend=c("Tokyo","New York","London","Berlin"),ncol=4,cex=0.8,bty="n",col=c("red","blue","green","orange"),lty=1,lwd=2)


#Recipe 11.地图 Creating graphs with maps

install.packages("maps")
library(maps)
map()
map('world', fill = TRUE,col=heat.colors(10))
map("state", interior = FALSE)
map("state", boundary = FALSE, col="red", 
add = TRUE) 
install.packages("sp")
library(sp)
load(url("http://gadm.org/data/rda/GBR_adm1.RData"")                                                                            spplot(gadm,"Shape_Area")

#Recipe 12.保存输出图像 Saving and exporting graphs

png("scatterplot.png")
plot(rnorm(1000))
dev.off()
png("scatterplot.png", height=600, width=600)
plot(rnorm(1000))                                                                                                              dev.off()                                                                                                                         png("scatterplot.png", height=4, width=4,units="in")
plot(rnorm(1000))
dev.off()

png("scatterplot.png",res=600)
plot(rnorm(1000))
dev.off()

pdf("scatterplot.pdf")
plot(rnorm(1000))
dev.off()

 

#CHAPTER 2
#Recipe 1.设置颜色 Setting colors of points, lines and bars
plot(rnorm(1000),
col="red")
sales <- read.csv("dailysales.csv",header=TRUE)
plot(Sales$units~as.Date(Sales$date,"%d/%m/%y"),type="l", #Specify type of plot as l for line col="blue")
barplot(Sales$ProductA~Sales$City,col="blue")
#Vector of colors
heat.colors(5)
barplot(as.matrix(sales[,2:4]), beside=T,legend=sales$City,col=c("red","blue","green","orange","pink"),border="white")
barplot(as.matrix(sales[,2:4]), beside=T,legend=sales$City,col=c("red","blue","green","orange"),border="white")
barplot(as.matrix(sales[,2:4]), beside=T,legend=sales$City,col=heat.colors(length(sales$City)),border="white")

#Recipe 2. 设置背景色Setting plot background colors
par(bg="gray")
plot(rnorm(100))
plot(rnorm(1000),type="n")
x<-par("usr")
rect(x[1],x[3],x[2],x[4],col="lightgray ")
points(rnorm(1000))

#Recipe 3.设置文本、标签等颜色 Setting colors for text elements: axis labels, titles, plot titles and legends
plot(rnorm(100),main="Plot Title",col.axis="blue",col.lab="red",col.main="darkblue")
par(col.axis="black",col.lab="#444444",col.main="darkblue")
plot(rnorm(100),main="plot")
title("Sales Figures for 2010", col.main="blue")
title(xlab="Month",ylab="Sales",col.lab="red")
title(xlab="X axis",col.lab="red")
title(ylab="Y axis",col.lab="blue")

#Recipe 4. 调色板Choosing color combinations and palettes
palette(c("red","blue","green","orange"))
palette("default")
install.packages("RColorBrewer")
library(RColorBrewer)
display.brewer.all()
brewer.pal(7,"YlOrRd")
display.brewer.pal(7,"YlOrRd")
palette(brewer.pal(7,"YlOrRd"))
pal1<- brewer.pal(7,"YlOrRd")

#Recipe 5. 设置注释标题的字体Setting fonts for annotations and titles 
par(family="serif",font=2)
names(pdfFonts())

#Recipe 6.选择绘图点符号样式和大小 Choosing plotting point symbol styles and sizes
rain<-read.csv("cityrain.csv")
plot(rnorm(100),pch=19,cex=2)
plot(rain$Tokyo,ylim=c(0,250),main="Monthly Rainfall in major cities",xlab="Month of Year",ylab="Rainfall (mm)",pch=1)
points(rain$NewYork,pch=2)
points(rain$London,pch=3)
points(rain$Berlin,pch=4)
legend("top",
legend=c("Tokyo","New York","London","Berlin"),ncol=4,cex=0.8,bty="n",pch=1:4)

#Recipe 7.选择线条样式和宽度 Choosing line styles and width
plot(rain$Tokyo,ylim=c(0,250),main="Monthly Rainfall in major cities",xlab="Month of Year",ylab="Rainfall (mm)",type="l",lty=1,
lwd=2)
lines(rain$NewYork,lty=2,lwd=2) 
lines(rain$London,lty=3,lwd=2)
lines(rain$Berlin,lty=4,lwd=2)
legend("top",legend=c("Tokyo","New York","London","Berlin"),ncol=4,cex=0.8,bty="n",lty=1:4,lwd=2)

#Recipe 8. 选择框风格Choosing box styles
par(bty="l")
plot(rnorm(100))
par(oma=c(1,1,1,1))
plot(rnorm(100),bty="l")
box(which="figure")

#Recipe 9.调整轴标签和刻度线的风格 How to adjust axis labels and tick marks styles
plot(rnorm(100),xaxp=c(0,100,10))

#Recipe 10.格式日志轴 How to format log axes
plot(10^c(1:5),log="y",type="b")

#Recipe 11.设置图形利润率和维度 Setting graph margins and dimensions 
par(fin=c(6,6), 
pin=c(4,4))
par(mai=c(1,1,1,1),
omi=c(0.1,0.1,0.1,0.1))
#CHAPTER 3
#Recipe 1.一个散点图显示分组数据 Grouping data points within a scatter plot
library(lattice)
xyplot(mpg~disp,
data=mtcars,
groups=cyl,
auto.key=list(corner=c(1,1)))
install.packages("gplot2")
library(ggplot2)
qplot(disp,mpg,data=mtcars,col= as.factor(cyl))

#Recipe 2. 高亮显示分组数据点的颜色、大小和符号类型Highlighting grouped data points by colour, size and symbol type
library(ggplot2)
#By shape
qplot(disp,mpg,data=mtcars,shape=as.factor(cyl)) 
#By size
qplot(disp,mpg,data=mtcars,size=as.factor(cyl))

#Recipe 3.数据点加标签 Labelling data points
plot(mpg~disp, data=mtcars)
text(258,22,"Hornet")
health<-read.csv("HealthExpenditure.csv",header=TRUE)
plot(health$Expenditure,health$Life_Expectancy,type="n")
text(health$Expenditure,health$Life_Expectancy,health$Country)

#Recipe 4. 相关系数矩阵Correlation matrix using pairs plot
panel.cor <- function(x, y, ...)
{
    par(usr = c(0, 1, 0, 1))
    txt <- as.character(format(cor(x, y), digits=2))
    text(0.5, 0.5, txt,  cex = 6* abs(cor(x, y)))
}
pairs(iris[1:4], upper.panel=panel.cor)

#Recipe 5. 增加误差线Adding error bars
plot(mpg~disp,data=mtcars)
arrows(x0=mtcars$disp,
y0=mtcars$mpg*0.95,
x1=mtcars$disp,
y1=mtcars$mpg*1.05,
angle=90,
code=3,
length=0.04,
lwd=0.4)

#Horizontal
arrows(x0=mtcars$disp*0.95,y0=mtcars$mpg,x1=mtcars$disp*1.05,y1=mtcars$mpg,angle=90,code=3,length=0.04,lwd=0.4)


#Recipe 6.使用抖动区分密集数据点 Using jitter to distinguish closely packed data points
x <- rbinom(1000, 10, 0.25)
y <- rbinom(1000, 10, 0.25)
plot(x,y)
plot(jitter(x), jitter(y))


#Recipe 7.增加回归线 Adding linear model lines
plot(mtcars$mpg~mtcars$disp)
lmfit<-lm(mtcars$mpg~mtcars$disp)
abline(lmfit)	


#Recipe 8.增加非线性回归线 Adding non-linear model curves
x <- -(1:100)/10
y <- 100 + 10 * exp(x / 2) + rnorm(x)/10
nlmod <- nls(y ~  Const + A * exp(B * x), trace=TRUE)
plot(x,y)
lines(x, predict(nlmod), col="red")

#Recipe 9.增加非参数模型曲线 Adding non-parametric model curves with lowess
plot(cars, main = "lowess(cars)")
lines(lowess(cars), col = 揵lue")	
lines(lowess(cars, f=0.3), col = 搊range")


#Recipe 10. 三维散点图Making  3-dimensional scatter plots
install.packages("scatterplot3d") 
library(scatterplot3d)
scatterplot3d(x=mtcars$wt,y=mtcars$disp,z=mtcars$mpg)
scatterplot3d(wt,disp,mpg,pch=16, highlight.3d=TRUE, angle=20,xlab="Weight",ylab="Displacement",zlab="Fuel Economy (mpg)",type="h",main="Relationships between car specifications")


#Recipe 11.QQ图 Making Quantile-Quantile plots
qqnorm(mtcars$mpg)
qqline(mtcars$mpg)
lmfit<-lm(mtcars$mpg~mtcars$disp)
par(mfrow=c(2,2))
plot(lmfit)


#Recipe 12. 坐标轴显示数据密度Displaying data density on axes
x<-rnorm(1000)
plot(density(x),type="l") 
rug(x)
metals<-read.csv("metals.csv")
plot(Ba~Cu,data=metals,xlim=c(0,100))
rug(metals$Cu)
rug(metals$Ba,side=2,col="red",ticksize=0.02)


#Recipe 13.带平滑曲线的散点图 Making scatter plots with smoothed density representation
n <- 10000
x  <- matrix(rnorm(n), ncol=2)
y  <- matrix(rnorm(n, mean=3, sd=1.5), ncol=2)
smoothScatter(x,y)
#CHAPTER 4
#Recipe 1.对多条线增加图例 Adding customized legends for multiple line graphs
rain<-read.csv("cityrain.csv")
plot(rain$Tokyo,type="b",lwd=2,
xaxt="n",ylim=c(0,300),col="black",
xlab="Month",ylab="Rainfall (mm)",
main="Monthly Rainfall in major cities")
axis(1,at=1:length(rain$Month),labels=rain$Month)
lines(rain$Berlin,col="red",type="b",lwd=2)
lines(rain$NewYork,col="orange",type="b",lwd=2)
lines(rain$London,col="purple",type="b",lwd=2)

legend("topright",legend=c("Tokyo","Berlin","New York","London"),
lty=1,lwd=2,pch=21,col=c("black","red","orange","purple"),
ncol=2,bty="n",cex=0.8,
text.col=c("black","red","orange","purple"),
inset=0.01)

legend(1,300,legend=c("Tokyo","Berlin","New York","London"),
lty=1,lwd=2,pch=21,col=c("black","red","orange","purple"),
horiz=TRUE,bty="n",bg="yellow",cex=1,
text.col=c("black","red","orange","purple"))

#Recipe 2.使用边缘标签替代图例 Using margin labels instead of legends for multiple line graphs
gdp<-read.table("gdp_long.txt",header=T)
library(RColorBrewer)
pal<-brewer.pal(5,"Set1")
par(mar=par()$mar+c(0,0,0,2),bty="l")
plot(Canada~Year,data=gdp,type="l",lwd=2,lty=1,ylim=c(30,60),col=pal[1],main="Percentage change in GDP",ylab="") 
mtext(side=4,at=gdp$Canada[length(gdp$Canada)],text="Canada",col=pal[1],line=0.3,las=2)
lines(gdp$France~gdp$Year,col=pal[2],lwd=2)
mtext(side=4,at=gdp$France[length(gdp$France)],text="France",col=pal[2],line=0.3,las=2)
lines(gdp$Germany~gdp$Year,col=pal[3],lwd=2)
mtext(side=4,at=gdp$Germany[length(gdp$Germany)],text="Germany",col=pal[3],line=0.3,las=2)
lines(gdp$Britain~gdp$Year,col=pal[4],lwd=2)
mtext(side=4,at=gdp$Britain[length(gdp$Britain)],text="Britain",col=pal[4],line=0.3,las=2)
lines(gdp$USA~gdp$Year,col=pal[5],lwd=2)
mtext(side=4,at=gdp$USA[length(gdp$USA)]-2,text="USA",col=pal[5],line=0.3,las=2)

#Recipe 3.增加水平和垂直网格线 Adding horizontal and vertical grid lines
rain<-read.csv("cityrain.csv")
plot(rain$Tokyo,type="b",lwd=2,
xaxt="n",ylim=c(0,300),col="black",
xlab="Month",ylab="Rainfall (mm)",
main="Monthly Rainfall in Tokyo")
axis(1,at=1:length(rain$Month),labels=rain$Month)
grid()
grid(nx=NA, ny=8,lwd=1,lty=2,col="blue")

#Recipe 4.在指定横纵轴位置增加标记线 Adding marker lines at specific X and Y values using abline
Rrain <- read.csv("cityrain.csv")
plot(rain$Tokyo,type="b",lwd=2,
xaxt="n",ylim=c(0,300),col="black", 
xlab="Month",ylab="Rainfall (mm)",
main="Monthly Rainfall in Tokyo")
axis(1,at=1:length(rain$Month),labels=rain$Month)                                                                                  abline(v=9) 
abline(h=150,col="red",lty=2)

#Recipe 5.波形图(迷你图) Creating sparklines
rain <- read.csv("cityrain.csv")
par(mfrow=c(4,1),mar=c(5,7,4,2),omi=c(0.2,2,0.2,2)) 
for(i in 2:5)
{
	plot(rain[,i],ann=FALSE,axes=FALSE,type="l",col="gray",lwd=2) 
		mtext(side=2,at=mean(rain[,i]),names(rain[i]),
		las=2,col="black")
	mtext(side=4,at=mean(rain[,i]),mean(rain[i]),
	las=2,col="black")
	points(which.min(rain[,i]),min(rain[,i]),pch=19,col="blue")
	points(which.max(rain[,i]),max(rain[,i]),pch=19,col="red")
}

#Recipe 6.对数据集中变量作图 Plotting functions of a variable in the dataset
rain <- read.csv("cityrain.csv")
plot(rain$Berlin-rain$London,type="l",lwd=2, xaxt="n",col="blue",xlab="Month",ylab="Difference in Rainfall (mm)",
main="Difference in Rainfall between Berlin and London (Berlin-London)")
axis(1,at=1:length(rain$Month),labels=rain$Month)
abline(h=0,col="red")

x<-1:100
y<-x^3-6*x^2+5*x+10
plot(y~x,type="l",main=expression(f(x)==x^3-6*x^2+5*x+10))

#Recipe 7.时间序列作图 Formatting time series data for plotting
sales<-read.csv("dailysales.csv")
d1<-as.Date(sales$date,"%d/%m/%y")
d2<-strptime(sales$date,"%d/%m/%Y")

install.packages(“zoo")
library(zoo) 
	
d3<-zoo(sales$units,as.Date(sales$date,"%d/%m/%y"))


#Recipe 8.横轴上对日期或时间变量作图 Plotting the date or time variable on the X axis
sales<-read.csv("dailysales.csv")
plot(sales$units~as.Date(sales$date,"%d/%m/%y"),type="l",xlab="Date",ylab="Units Sold")
plot(strptime(sales$date,"%d/%m/%Y"),sales$units,type="l",xlab="Date",ylab="Units Sold")
library(zoo)
plot(zoo(sales$units,as.Date(sales$date,"%d/%m/%y")))
air<-read.csv("openair.csv")
plot(air$nox~as.Date(air$date,"%d/%m/%Y %H:%M"),type="l",xlab="Time", ylab="Concentration (ppb)",main="Time trend of Oxides of Nitrogen")
plot(zoo(air$nox,as.Date(air$date,"%d/%m/%Y %H:%M")),xlab="Time", ylab="Concentration (ppb)",main="Time trend of Oxides of Nitrogen")


#Recipe 9.不同可读时间格式的注释轴标Annotating axis labels in different human readable time formats
air<-read.csv("openair.csv")
plot(air$nox~as.Date(air$date,"%d/%m/%Y %H:%M"),type="l",
xaxt="n",
xlab="Time", ylab="Concentration (ppb)",
main="Time trend of Oxides of Nitrogen")
xlabels<-strptime(air$date, format = "%d/%m/%Y %H:%M")
axis.Date(1, at=xlabels[xlabels$mday==1 ], format="%b-%Y")


#Recipe 10.增加垂直标记来表示特定时间事件Adding vertical markers to indicate specific time events
air<-read.csv("openair.csv")
plot(air$nox~as.Date(air$date,"%d/%m/%Y %H:%M"),type="l",xlab="Time", ylab="Concentration (ppb)",main="Time trend of Oxides of Nitrogen")
abline(v=as.Date("25/12/2003","%d/%m/%Y"),col="red")
markers<-seq(from=as.Date("25/12/1998","%d/%m/%Y"),to=as.Date("25/12/2004","%d/%m/%Y"),by="year")	
abline(v=markers,col="red")

#Recipe 11.不同时间作图 Plotting data with varying time averaging periods
air<-read.csv("openair.csv")
air$date = as.POSIXct(strptime(air$date, format = "%d/%m/%Y %H:%M", "GMT"))
means <- aggregate(air["nox"], format(air["date"],"%Y -%U"),mean, na.rm = TRUE)
means$date <- seq(air$date[1], air$date[nrow(air)],length = nrow(means))
plot(means$date, means$nox, type = "l")

means <- aggregate(air["nox"], format(air["date"],"%Y-%j"),mean, na.rm = TRUE)
means$date <- seq(air$date[1], air$date[nrow(air)],length = nrow(means))
plot(means$date, means$nox, type = "l",
xlab="Time", ylab="Concentration (ppb)",
main="Daily  Average Concentrations of Oxides of Nitrogen")

#Recipe 12. 股票K线图Creating stock charts 
install.packages("tseries")
library(tseries)
aapl<-get.hist.quote(instrument = "aapl", quote = c("Cl", "Vol"))
goog <- get.hist.quote(instrument = "goog", quote = c("Cl", "Vol"))
msft <- get.hist.quote(instrument = "msft", quote = c("Cl", "Vol"))
plot(msft$Close,main = "Stock Price Comparison" ,,ylim=c(0,800),col="red",type="l",lwd=0.5, ,pch=19  ,cex=0.6  ,xlab="Date" ,ylab="Stock Price (USD)")

lines(goog$Close,col="blue",lwd=0.5)
lines(aapl$Close,col="gray",lwd=0.5)
legend("top",horiz=T,legend=c("Microsoft","Google","Apple"),
col=c("red","blue","gray"),lty=1,bty="n")

install.packages("quantmod")
library(quantmod)
getSymbols("AAPL",src="yahoo") 
barChart(AAPL)
candleChart(AAPL,theme="white")     




抱歉!评论已关闭.