1.繪製散點圖ide
# 使用ggplot2 library(ggplot2) ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point()
2.繪製折線圖函數
# 使用ggplot library(ggplot2) # 繪製第一條折線附有數據點 g <- ggplot(data = pressure, aes(x = temperature, y = pressure)) + geom_line(color = "blue") + geom_point(color = "blue") g + # 繪製第二條折線附有數據點 geom_line(data = pressure, aes(temperature, pressure/2), color = "red") + geom_point(data = pressure, aes(temperature, pressure/2), color = "red")
3.繪製條形圖spa
# 使用ggplot2(左圖) library(ggplot2) g <- ggplot(data = BOD, aes(x = Time, y = demand)) + geom_bar(stat = "identity") # 調用 g # =========================================================== # 使用factor是爲了離散x軸取值,這樣沒有多餘的間隔(右圖) library(ggplot2) g <- ggplot(data = BOD, aes(x = factor(Time), y = demand)) + geom_bar(stat = "identity") # 調用 g
4.繪製直方圖 3d
# 繪製直方圖 "+" 運算經過賦值在調用 library(ggplot2) g <- ggplot(data = mtcars, aes(x = mpg)) + geom_histogram(binwidth = 4) # 調用 g
5.繪製函數圖code
# 使用ggplot2 # 設置函數 myFun <- function(x){ 1/x } g <- ggplot(data = data.frame(x = c(0, 20)), aes(x = x)) + stat_function(fun = myFun, geom = "line", col = "red") # col設置顏色 # 調用 g