R语言中的数据可视化

第六章 数据可视化

1.散点图的绘制

# 在1-10范围内 y = x
# 在11-20范围内 y = 1
# 同时混合正太分布噪声波动

x <- 1:20
f <- rep(0:1, each = 10)
y <- x + f - x * f + rnorm(20, sd = 0.5)
plot(x, y, type = "b")

plot(
  x,
  y,
  main = "sample",
  type = "o",
  pch = 17,
  cex = 2 ,
  lty = 2,
  lwd = 2,
  col = rainbow(10)
)

2.颜色的处理

colors()
palette()

pie(1:8, labels = "", col = palette())

pal <- palette()
pal <- rainbow(100)
pal
pie (rep(1, length(pal)),
     labels = sprintf("%d (%s)", seq_along(pal), pal),
     col = pal)

pie (rep(1, length(pal)),
     labels = "",
     col = pal)

pie(rep(1, 10), col = terrain.colors(10))
pie(rep(1, 10), col = topo.colors(10))
pie(rep(1, 10), col = heat.colors(10))
pie(rep(1, 10), col = cm.colors(10))

3. 图表细节刻度

x <- 1:20
y <- x ^ 2
plot (x, y, ann = FALSE , col = "tomato")

title (
  main = "TITLE",
  col.main = "red",
  sub = "Sub-title",
  col.sub = "brown",
  xlab = "x-lable",
  ylab = "y-lable",
  col.lab = "navy",
  cex.main = 2,
  cex.sub = 1.25,
  font.sub = 3
)

plot(
  1:25,
  cex = 3,
  lwd = 3,
  pch = 1:25,
  col = rainbow(25),
  bg = c(rep(NA, 20), terrain.colors(5)),
  main = "plot rainbow" ,
  col.main = "cyan",
  sub = "rainbow rainbow",
  col.sub = "magenta",
  xlab = "x-index",
  ylab = "y-value",
  col.lab = "tomato",
  col.axis = "chocolate"
)

4. 自定义坐标轴

x <- 1:20
y <- x ^ 2
plot (x,
      y ,
      col = "tomato",
      axes = F,
      #x,y是否等比例
      #asp = 1
      )
axis(side = 1,
     at = seq(0, 20, 2),
     las = 1)
axis(side = 2,
     at = seq(0, 400, 50),
     las = 0,pos = c(0,0))

5. 自定刻度线参考线

x <- 1:20
y <- x ^ 2
plot (x, y, col = "tomato")

install.packages("Hmisc")
library(Hmisc)
#次要刻度线
minor.tick(nx = 5, ny = 5, tick.ratio = 0.5)

#参考线
#在特定的横纵位置画一条参考线
abline(
  h = 200 ,
  v = 10,
  col = "green",
  lty = 2,
  lwd = 2
)
# 绘制多条参考线
abline(
  h = seq(0, 400, 50) ,
  v = seq(0, 20, 1),
  col = "green",
  lty = 2,
  lwd = 2
)

#绘制截距斜率参考线
abline(
  a = 2,
  b = 100 ,
  col = "blue",
  lty = 1,
  lwd = 2
)
#y = ax + b

6.绘制图例

legend(
  x = 15,
  y = 150,
  pch = 1:4,
  col = rainbow(4),
  legend = c("A", "B", "C", "D"),
  title = "tuli",
  title.col = "black",
  text.col = rainbow(4),
  cex = 0.5
)

7. 添加标注

plot (1:20, (1:20)^2, main = "exponential 
	points", xlab = "x", ylab="values")
text (10,200, expression(y==x^2),
	cex=1.2, col="blue")
for (s in 1:4) 
	mtext(paste("mtext(...,side=", s ,")"), 
		side = s, font=s)

8.描述性统计图

柱状图/条形图
barplot
饼图
pie
直方图hist
箱图boxplot

9. 其他图形

data("iris")
head(iris)
str(iris)
iris$Species
par(mfrow = c(2, 2))
with(iris, plot(Species, Sepal.Length, main = colnames(iris)[1]))
with(iris, plot(Species, Sepal.Width, main = colnames(iris)[2]))
with(iris, plot(Species, Petal.Length, main = colnames(iris)[3]))
with(iris, plot(Species, Petal.Width, main = colnames(iris)[4]))

pairs(iris)
pairs(iris[,1:4])
pairs(iris[,1:4],col=rainbow(3))
pairs(iris[,1:4],col=iris[,5])
pairs(iris[-5], gap = 0)

10.函数绘图

#funciton curve
curve(x^3 - 5*x, from=-4,to=4)


#sigmoid函数
myfunc <- function(x){
  1/(1+exp(-x + 10 ))
}
curve(myfunc(x),from = 0,to=20)

11. ggplot2

#qplot快速绘图
#basic use qplot
library("ggplot2")
#查看数据集
View(pressure)
?pressure
#绘制散点图
qplot(temperature,pressure,data = pressure)
#绘制变化散点图
qplot(log(temperature),log(pressure),data = pressure)

#改变散点图的颜色,透明度,大小
qplot(waiting,eruptions,data=faithful,colour=waiting,alpha = I(1/0.5),size=4)
qplot(Sepal.Length,Petal.Length,data=iris,colour=Species,alpha = I(1/2),shape=Species,size=(Sepal.Width*Petal.Width))



#添加回归包
library(splines)
#快速绘制回归模型参考线
qplot(waiting,eruptions,data=faithful,geom = c("point","smooth"),method="lm")

#画描述性统计图
qplot(Species,Sepal.Length,data = iris,geom = "boxplot",fill=Species)
qplot(Sepal.Length,data = iris,geom = "histogram",fill=Species)
qplot(Sepal.Length,data = iris,geom = "density",color=Species,linetype=Species)

### ggplot2

#数据层 +美学层(x,y)+ geom几何层(点、线、柱状、箱图) + 
#面层 + 统计层 + 坐标层 + 主题
ggplot(data = airquality, aes(x = Wind, y = Temp)) + 
  geom_point(col = "steelblue", alpha = 0.4, size  = 5) +
  stat_smooth(method = "lm", se = F)

with(airquality, plot(x = Wind, y = Temp))
with(subset(airquality, Month == 9), points(Wind, Temp, col = "red", pch = 17))


#逐层绘制
b <- ggplot(data = mtcars, aes(x=wt, y=mpg))
b
b+geom_point()
b + geom_point(aes(color = factor(cyl), shape = factor(cyl)))


ggplot(data = faithful, aes(x=eruptions, y=waiting))+
  geom_point(col = "steelblue", alpha = 0.4,)+geom_smooth(method = "lm",se=F)

ggplot(data = faithful, aes(x=eruptions, y=waiting))+
  geom_point(col = "steelblue", alpha = 0.4,)+geom_smooth(method = "lm",se=F)+
  geom_jitter(width = 0.5,height = 0.5)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值