R语言基本图形

1.饼图

pie.sales=c(0.12, .3, 0.26, 0.16, 0.04,  0.12)    #数据
names(pie.sales) = c("Blueberry", "Cherry", "Apple", "Boston Cream", "Other", "Vanilla Cream")   #设置数据标签
pie.col = c("purple", "violetred1", "green3", "cornsilk", "cyan", "white")  #设置颜色
pie(x=pie.sales, col = pie.col)   #绘制饼图

在这里插入图片描述

require(scales)
x=pie.sales
pie(x, col=hue_pal()(length(x)), border = "white")   #绘制饼图

在这里插入图片描述

2.直方图

palette("default") 
par(mfrow=c(2,2), mar=c(2,3,2, 0.5))   #绘图多张
data(geyser, package="MASS")   #调用数据
hist(geyser$waiting, main = "(1) freq = TRUE", xlab = "waiting")
hist(geyser$waiting, freq = FALSE, xlab = "waiting", main = "(2) freq = FALSE")    
hist(geyser$waiting, breaks = 5, density = 10, xlab = "waiting", main = "(3) breaks = 5")#breaks = 40:指定了直方图的区间(bin)数目
hist(geyser$waiting, breaks = 40, col = "red", xlab = "waiting", main = "(4) breaks = 40")

在这里插入图片描述

3.条形图

par(mfrow=c(2,3), mar=c(2,3,2,0.5))
barplot(height=1:3)  #输入为向量时的图
barplot(height=1:3, col=2:4, space=0.5, names.arg=LETTERS[1:3])  #输入为向量时更改颜色和条图间的间距
barplot(height=1:3, col=2:4, space=0.5, horiz=T, names.arg=LETTERS[1:3]) #条图方向改为水平,添加条形图的标签
#matrix(runif(6), 2,3)是生成6个0-1的随机数并2*#的矩阵
barplot(height=matrix(runif(6), 2,3), col=2:3)   #height输入为矩阵时
barplot(height=matrix(runif(6), 2,3), beside=T, col=2:3)   #height输入为矩阵时,beside设置为T, 并更改颜色
barplot(height=matrix(runif(6), 2,3), beside=T, col=c("red", "green"),ylim=c(0,  2.3), legend.text = c("red", "green") ) #添加图例

在这里插入图片描述

4.线箱图

data=data.frame(x=rnorm(12), y=rep(1:3, each=4))
par(mfrow=c(2,2), mar=c(2,3,2, 0.5))
boxplot(data$x)
boxplot(data$x~data$y)
boxplot(data$x~data$y, col=2:4)
boxplot(data$x~data$y, col=2:4, horizontal=T)

在这里插入图片描述

5.核密度图

n <- 10000
#ncol = 2:这个参数指定了将这个随机数向量排列成一个矩阵时的列数
x1  <- matrix(rnorm(n), ncol = 2)
x2  <- matrix(rnorm(n, mean = 3, sd = 1.5), ncol = 2)
x   <- rbind(x1, x2)
plot(x, pch=16,cex=0.3)
smoothScatter(x, nrpoints = 0, 
              xlab="",ylab="")
points(x, cex=0.3, pch=16)
rect(-1,-1,1,1,border="red")
rect(1.5,1.5,4.5,4.5,border="red")

在这里插入图片描述

6.热图

?mtcars
x  <- as.matrix(mtcars)
rc <- rainbow(nrow(x), start = 0, end = .3)
cc <- rainbow(ncol(x), start = 0, end = .3)
hv <- heatmap(x, col = cm.colors(256), 
              scale = "column",
              RowSideColors = rc, 
              ColSideColors = cc, 
              margins = c(5,10),
              xlab = "specification variables", 
              ylab =  "Car Models",
              main = "heatmap")

在这里插入图片描述

7.等值线图

?volcano
(x <- 10*1:nrow(volcano))
(y <- 10*1:ncol(volcano))
contour(x, y, volcano, col = "blue", lty = "solid",
        vfont = c("sans serif", "plain") )

在这里插入图片描述

8.栅格图image

x <- 10*1:nrow(volcano)
y <- 10*1:ncol(volcano)
#par(mfrow=c(1,2), mar=rep(1,4),ann=F)
#image(x, y, volcano, col=terrain.colors(100), axes=F)    #绘制image图,并设置颜色
#box(lwd=2)
image(x, y, volcano, col=terrain.colors(100), xlab="",ylab="") 
contour(x, y, volcano, nlevels=10, lwd=1.5, add=T)  #将等高线叠加在上面
box(lwd=2)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值