Switch Editions?
Cancel
Channel: CodeSection,代码区,网络安全 - CodeSec
Viewing all articles

# 一小时掌握R语言数据可视化

0
0

ggplot2是R语言最为强大的作图软件包，有着自成一派的可视化理念，数据可视化是数据分析的重要一步，让我们通过由浅入深的掌握数据可视化的精髓。

ggplot2和其他作图工具不同，它是以图层覆盖图层的方式画出一个完美图像的，就像是photoshop里的图层，那么首先我们得有一张画布（如果没有安装R语言和ggplot2请见《 十八-R语言特征工程实战 》）

[root@centos \$] R
> library(ggplot2)
> ggplot()

> ggplot() + geom_hline(yintercept = 5)

> ggplot() + geom_vline(xintercept = 5)

> geom_abline(intercept = 2.5, slope=1)

> x <- c(1,2,3)
> y <- c(1,3,4)
> data <- data.frame(x,y)
> str(data)
'data.frame': 3 obs. of 2 variables:
\$ x: num 1 2 3
\$ y: num 1 3 4

> ggplot(data, aes(x = x, y = y)) + geom_point()

> ggplot(data, aes(x, y)) + geom_point(aes(colour = factor(y)))
> ggplot(data, aes(x, y)) + geom_point(aes(shape = factor(y)))

> ggplot(data, aes(x, y)) + geom_point(aes(colour = y))

> ggplot(data, aes(x, y)) + geom_point(aes(size = 3))
> ggplot(data, aes(x, y)) + geom_point(aes(size = y))

> ggplot(data, aes(x, y)) + geom_point(colour="red")
> ggplot(data, aes(x, y)) + geom_point(aes(colour="red"))

> ggplot(data, aes(x, y)) + geom_point(aes(colour = y)) + geom_abline(slope = 1, intercept = 1)

> ggplot(data, aes(x)) + geom_bar()
> ggplot(data, aes(x<2)) + geom_bar()

> ggplot(data, aes(x)) + geom_bar(aes(weight=y))
> ggplot(data, aes(x,y)) + geom_bar(stat = "identity")

> x <- rep(c(1,2), c(2,3))
> y <- rep(c(3,2), c(1,4))
> data <- data.frame(x,y)
> ggplot(data, aes(x)) + geom_bar(aes(fill=factor(y)))

> ggplot(data, aes(x)) + geom_bar(aes(fill=factor(y)), position="dodge")
> ggplot(data, aes(x)) + geom_bar(aes(fill=factor(y)), position="fill")

> x <- rep(c(1,3,7,11,23,50,60),c(1,30,400,60,4,55,11))
> y <- rep(c(1,3,7,11,23,50,60),c(1,30,400,60,4,55,11))
> data <- data.frame(x,y)
> ggplot(data, aes(x)) + geom_density()

> ggplot(data, aes(x)) + geom_density(adjust = 1/5)

> ggplot(data, aes(x, colour = factor(y))) + geom_density(adjust = 1/5)
> ggplot(data, aes(x, fill = factor(y))) + geom_density(adjust = 1/5)
> ggplot(data, aes(x, colour = factor(y), fill = factor(y))) + geom_density(adjust = 1/5, alpha = 0.1)

> ggplot(data, aes(x, fill = factor(y))) + geom_density(adjust = 1/5, position='fill')
> ggplot(data, aes(x, fill = factor(y))) + geom_density(adjust = 1/5, position='stack')

> ggplot(data, aes(x, y, label=rownames(data))) + geom_point(aes(colour = y)) + geom_abline(slope = 1, intercept = 1) + geom_text(check_overlap = TRUE)
> ggplot(data, aes(x, y, label=rownames(data))) + geom_point(aes(colour = y)) + geom_abline(slope = 1, intercept = 1) + geom_label()

Viewing all articles

### 出售: Focal JM Lab Chorus 706

More Pages to Explore .....