R语言绘图功能:
提供实例:
demo(graphics)
demo(persp)
(二)绘图函数
(三)R内置数据集
如:
和
Iris(鸢尾花)数据集
(四)散点图
散点图的作用:
散点图表示因变量随自变量而变化的大致趋势
,据此可以选择合适的函数对数据点进行拟合
散点图集
(五)条形图
实例:
t=c(.20,.20,.60,.20,.30,.50,.10,.30,.6);
dim(t)=c(3,3);
barplot(t,beside=TRUE,xlab="城市",ylab="比例",legend.text=c("高中及以
下","大专","本科及以上"),names.arg=c("上海","广州","北京"));
(六)饼图和箱线图
箱形图(Box-plot)又称为盒须图、盒式图或箱线图,是一种用作显示一组数据分散情况资料的统计图
boxplot(x[2:4],col=c("red","green","blue"),notch=T)
(七)折线图
折线图
a=c(2,3,4,5,6)
b=c(4,7,8,9,12)
plot(a,b,type="l")
多条曲线的效果
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)
热力图:
heatmap(as.matrix(mtcars),Rowv=NA,Colv=NA,col = heat.colors(256),scale="column",margins=c(2,8),main = "Car characteristics byModel")
地图
library(maps)
• map("state", interior = FALSE)
• map("state", boundary = FALSE,
col="red",add = TRUE)
• map('world', fill =
TRUE,col=heat.colors(10))
R实验:社交数据可视化
通过设置坐标范围使焦点集中在美国周边,并且设置一些有关颜色
xlim <- c(-171.738281, -56.601563)
ylim <- c(12.039321,71.856229)
map("world", col="#f2f2f2",fill=TRUE, bg="white",lwd=0.05, xlim=xlim,ylim=ylim)
如下图:
lat_ca <- 39.164141
lon_ca <- -121.64062
lat_me <- 45.21300
lon_me <- -68.906250
inter <-gcIntermediate(c(lon_ca, lat_ca), c(lon_me,lat_me), n=50,addStartEnd=TRUE)
lines(inter)
lat_tx <- 29.954935
lon_tx <- -98.701172
inter2 <-gcIntermediate(c(lon_ca, lat_ca), c(lon_tx, lat_tx),n=50,addStartEnd=TRUE)
lines(inter2, col="red")
airports <- read.csv("http://datasets.flowingdata.com/tuts/maparcs/airports.csv",header=TRUE)
flights <- read.csv("http://datasets.flowingdata.com/tuts/maparcs/flights.csv",header=TRUE, as.is=TRUE)
map("world", col="#f2f2f2", fill=TRUE, bg="white", lwd=0.05, xlim=xlim, ylim=ylim)
fsub <- flights[flights$airline == "AA",]
for (j in 1:length(fsub$airline)) {
air1 <- airports[airports$iata == fsub[j,]$airport1,]
air2 <- airports[airports$iata == fsub[j,]$airport2,]
inter <- gcIntermediate(c(air1[1,]$long, air1[1,]$lat), c(air2[1,]$long, air2[1,]$lat), n=100,
addStartEnd=TRUE)
lines(inter, col="black", lwd=0.8)
}
(作图完结)