R語言學習筆記(三):ggplot2

Yanwei Liu
7 min readNov 25, 2019

安裝與使用

#安裝
install.packages("ggplot2")
#引入套件
library(ggplot2)

ggplot簡單散佈圖(aes負責綁定X軸跟Y軸)

ggplot(cars, aes(x = speed, y = dist))geom_histogram()  #直方圖
geom_boxplot() #盒鬚圖
geom_line() #線圖
geom_point() #散佈圖
geom_bar() #長條圖
stat_function(fun, geom = "line") #曲線圖
#範例:繪製盒鬚圖(其他圖形只要抽換函式名稱即可)
ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_boxplot()

ggplot的一些功能

ggplot的繪圖功能通常透過 "+" 來進行樣式的堆疊。

我們可透過以下範例理解:
建立p1這張散佈圖後,使用"+"配上geom_smooth函式完成p12,再 ”+” geom_text函式組成p13。以此類推…….變成p17

#製作散佈圖。
pl <- ggplot(df,aes(x=CPI,y=HDI,color=Region)) + geom_point(size=4,shape=1)

#加入趨勢線,lm代表線性模式,se=false代表不表示信賴區間。
pl2 <- pl + geom_smooth(aes(group=1),method ='lm',formula = y~log(x),se=FALSE,color='red')

#標示資料名稱。
pl3 <- pl2 + geom_text(aes(label = Country), color = "gray20",
data = subset(df, Country %in% pointsToLabel),check_overlap = TRUE)

#加入經濟學人主題的表格。
pl4 <- pl3 + economist_white()

#加入x軸名稱,限制資料範圍0.9~10.5,並分成十格。
pl5 <- pl4 + scale_x_continuous(name = "Corruption Perceptions Index, 2011 (10=least corrupt)",limits = c(.9, 10.5),breaks=1:10)

#加入y軸名稱,限制範圍0.2~1.0。
pl6 <- pl5 + scale_y_continuous(name = "Human Development Index, 2011 (1=Best)",limits = c(0.2, 1.0))

#加入表格標題。
pl7 <- pl6 + ggtitle("Corruption and Human development")
print(pl7)

繪圖主題設定theme( )

theme()圖層:

  • axis.text.x參數:將X軸的文字做一些角度旋轉。
  • text參數:指定中文字型,以解決中文亂碼問題。

labs()圖層:

  • title: 圖標題
  • x: x軸標題
  • y: y軸標題

ggplot2 預設有格線,加入 theme() 進行格線隱藏設定:

#隱藏主要格線 
panel.grid.major = element_blank()
#隱藏次要格線
panel.grid.minor = element_blank()
#隱藏 X 軸主要格線
panel.grid.major.x = element_blank()
#隱藏 Y 軸主要格線
panel.grid.major.y = element_blank()
#隱藏 X 軸次要格線
panel.grid.minor.x = element_blank()
#隱藏 Y 軸次要格線
panel.grid.minor.y = element_blank()
範例:
library(ggplot2)

ggplot(cars, aes(x = speed, y = dist)) +
+ geom_point() +
+ theme(panel.grid.major = element_blank(),
+ panel.grid.minor = element_blank())
+ labs(title = '圖片標題',x = "X軸名稱",y = "Y軸名稱")
+ theme(text=element_text(family="黑體-繁 中黑"), # 解決中文亂碼問題 axis.text.x = element_text(angle = 45, vjust = 0.5),
plot.title = element_text(hjust = 0.5)) # 將title置中

coord_flip()水平方向繪圖

ggplot(ice_cream_df, aes(x = ice_cream_flavor)) +
+ geom_bar() +
+ coord_flip()

直方圖加上密度曲線

library(ggplot2)

set.seed(123)
norm_nums <- rnorm(1000)
hist_df <- data.frame(norm_nums = norm_nums)
ggplot(hist_df, aes(x = norm_nums)) +
geom_histogram(binwidth = 0.5, aes(y = ..density..), alpha= 0.5) +
geom_density()

調整資料點的形狀與顏色geom_point( )

ggplot(cars, aes(x = speed, y = dist)) +
+ geom_point(shape = 2, colour = "red") # 資料點改成紅色的空心三角形

調整「不同類型」的形狀與顏色geom_point()

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
+ geom_point(aes(shape = Species, colour = Species))

繪製多個圖形

library(ggplot2)
library(gridExtra) #用來建立2x2空間的套件

g1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_boxplot()
g2 <- ggplot(iris, aes(x = Species, y = Sepal.Width)) +
geom_boxplot()
g3 <- ggplot(iris, aes(x = Species, y = Petal.Length)) +
geom_boxplot()
g4 <- ggplot(iris, aes(x = Species, y = Petal.Width)) +
geom_boxplot()
grid.arrange(g1, g2, g3, g4, nrow = 2, ncol = 2) #建立2x2空間

調整繪圖色調

#列出所有色調模組
RColorBrewer::display.brewer.all()
#設定顏色"Set3"
scale_fill_brewer(palette = 'Set3')

--

--

No responses yet