ggplot2绘图:
点状图
data(mtcars)
df <- mtcars[, c("mpg","cyl","wt")]
dfcyl)转换
head(df)
- ggplot(data=df, aes(x=mpg, y=wt))+
geom_point() - ggplot(data=df, aes(x=mpg, y=wt))+geom_point(color="blue", size=2, shape=23)
面积图
set.seed(1234)
wdata <- data.frame(
sex=factor(rep(c("F", "M"), each=200)),
weight=c(rnorm(200, 55), rnorm(200, 58))
)
head(wdata)
ggplot(wdata, aes(x=weight))+stat_density()
对于每一种几何图形。ggplot2 基本都提供了 geom_()和 stat_()
colsums()列求和
rowsums()行求和
rbind(),cbind()
例如:
dfx <- c(1,3,10,16,33,70,150)
dfy <- c(5,8,18,20,45,90,200)
df <- data.frame(dfx,dfy)
colSums(df)
rowSums(df)
df$rowsum <- rowSums(df)
df
df[8,] <- colSums(df)
df
结果:
dfx <- c(1,3,10,16,33,70,150)
dfy <- c(5,8,18,20,45,90,200)
df <- data.frame(dfx,dfy)
colSums(df)
dfx dfy
283 386
rowSums(df)
[1] 6 11 28 36 78 160 350
df$rowsum <- rowSums(df)
df
dfx dfy rowsum
1 1 5 6
2 3 8 11
3 10 18 28
4 16 20 36
5 33 45 78
6 70 90 160
7 150 200 350
8 283 386
dfdfx+df$dfy
df
dfx dfy rowsum add
1 1 5 6 6
2 3 8 11 11
3 10 18 28 28
4 16 20 36 36
5 33 45 78 78
6 70 90 160 160
7 150 200 350 350
8 283 386 669 669