R的画图

关于R基础
有3个需要总结的地方

  1. R的画图(统计学图,ggplot)
  2. R的基本语法
  3. 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 line
  • 1 displays a solid line
  • 2 displays a dashed line
  • 3 displays a dotted line
  • 4 displays a "dot dashed" line
  • 5 displays a "long dashed" line
  • 6 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()
posted @ 2022-12-15 18:04  爱吃番茄的玛丽亚  阅读(63)  评论(0编辑  收藏  举报