R的画图
关于R基础
有3个需要总结的地方
- R的画图(统计学图,ggplot)
- R的基本语法
- R dataframe相关
Plot
plot(1,2)
plot(c(1, 2, 3, 4, 5), c(3, 7, 8, 9, 12))
x <- c(1, 2, 3, 4, 5)
y <- c(3, 7, 8, 9, 12)
plot(x, y)
其他参数:
type='l'
main="My Graph"
xlab="The x-axis"
ylab="The y axis"
col="red"
cex=2
, 点的大小,默认值为1。pch=25
形状(值从0-25)lwd=2
如果画的不是line,也会起作用lty=3
linestyle, 只有在type=line的时候才会起作用 (值从0-6)
0
removes the line1
displays a solid line2
displays a dashed line3
displays a dotted line4
displays a "dot dashed" line5
displays a "long dashed" line6
displays a "two dashed" line
#画两条线,一定是先plot后line
line1 <- c(1,2,3,4,5,10)
line2 <- c(2,5,7,8,9,10)
plot(line1, type = "l", col = "blue")
lines(line2, type="l", col = "red")
看完了line
abline(lm(y~x))
abline
画回归线lm() linear-model
用线性模型拟合回归线
title("AAA")
给图像添加题目
ggplot
library(ggplot)
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))
#区别
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = "blue"))
ggplot(data = diamonds) + geom_bar(aes(x = cut, fill = cut)) + scale_fill_brewer(palette = "Dark2")
ggsave(file = "mygraph.png", plot = p)
- geom_smooth()