R繪圖 第五篇:繪制散點圖(ggplot2)


ggplot2包中繪制點圖的函數有兩個:geom_point和 geom_dotplot,當使用geom_dotplot繪圖時,point的形狀是dot,不能改變點的形狀,因此,geom_dotplot 叫做散點圖(Scatter Plot),通過繪制點來呈現數據的分布,對點分箱的方法有兩種:點密度(dot-density )和直方點(histodot)。當使用點密度分箱(bin)方式時,分箱的位置是由數據和binwidth決定的,會根據數據進行變化,但不會大於binwidth指定的寬度;當使用直方點分箱方式時,分箱有固定的位置和固定的寬度,就像由點構成的直方圖(histogram)。

bin是分箱的意思,在統計學中,數據分箱是一種把多個連續值分割成多個區間的方法,每一個小區間叫做一個bin(bucket),這就意味着每個bin定義一個數值區間,連續值會落到相應的區間中。

對點進行分箱時,點的位置(Position adjustment)有多種調整方式:

  • identity:不調整
  • dodge:垂直方向不調整,只調整水平位置
  • nudge:在一定的范圍內調整水平和垂直位置
  • jitter:抖動,當具有離散位置和相對較少的點數時,抖動很有用
  • jitterdodge:同時jitter和 dodge
  • stack:堆疊,
  • fill:填充,用於條形圖

每個位置調整都對應一個函數position_xxx()。

當沿着x軸進行分箱,並沿着y軸堆疊時,y軸上的數字沒有意義。

當沿x軸進行分箱並沿y軸堆疊時,由於ggplot2的技術限制,y軸上的數字沒有意義。 您可以隱藏y軸(如其中一個示例中所示),也可以手動縮放y軸以匹配點數。

使用geom_dotplot()函數來繪制點圖:

geom_dotplot(mapping = NULL, data = NULL, position = "identity", ...,
  binwidth = NULL, binaxis = "x", method = "dotdensity",
  binpositions = "bygroup", stackdir = "up", stackratio = 1,
  dotsize = 1, stackgroups = FALSE, origin = NULL, right = TRUE,
  width = 0.9, drop = FALSE, na.rm = FALSE, show.legend = NA,
  inherit.aes = TRUE)

常用的參數注釋:

  • mapping:使用aes()來設置點圖美學特征,參數x是因子,參數y是數值
  • data:數據框對象
  • position:位置調整(Position adjustment),默認值是identity,表示不調整位置。
  • method:默認值是dotdensity(點密度分箱),或者histodot(直方點,固定的分箱寬度)
  • binwidth:該參數用於調整分箱的寬度,該參數受到method參數的影響,如果method是dotdensity,那么binwidth指定分箱的最大寬度;如果method是histodot,那么binwidth指定分箱的固定寬度,默認值是數據范圍(range of the data)的1/30。
  • binaxis:沿着那個軸進行分箱,默認值是x
  • stackdir:設置堆疊的方向,默認值是up,有效值是down、center、centerwhole和up。
  • stackratio:點堆疊的密集程度,默認值是1,值越小,堆集越密集;
  • dotsize:點的大小,相對於binwidth的直徑,默認值是1。

使用ToothGrowth數據來繪制點圖:

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

 一,繪制點圖

繪制基本的點圖

library(ggplot2)

p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_dotplot(binaxis='y', stackdir='center',
               stackratio=1.5, dotsize=1.2)

二,添加匯總數據

向點圖中添加匯總數據,使用ggplot2包中的函數

stat_summary(mapping = NULL, data = NULL, geom = "pointrange",
  position = "identity", ..., fun.data = NULL, fun.y = NULL,
  fun.ymax = NULL, fun.ymin = NULL, fun.args = list(), na.rm = FALSE,
  show.legend = NA, inherit.aes = TRUE)

常用參數注釋:

  • fun.data:指定一個函數(function),返回帶有變量ymin,y和ymax的數據框
  • 或者,指定三個單獨的函數,分別向每個函數傳遞一個向量,分別返回一個數字,用於表示ymin、y和ymax:
    • fun.y:
    • fun.ymax:
    • fun.ymin:
  • fun.args=list():可選的參數,用於指定傳遞給fun.xxx函數的參數

1,向點圖中增加均值和中位數 

# dot plot with mean points
p + stat_summary(fun.y=mean, geom="point", shape=18,
                 size=3, color="red")
# dot plot with median points
p + stat_summary(fun.y=median, geom="point", shape=18,
                 size=3, color="red")

2,向點圖中增加點范圍

fun.low.mean <- function(x){mean(x)-sd(x)}
fun.up.mean <- function(x){mean(x)+sd(x)}

ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_dotplot(binaxis='y', stackdir='center', stackratio=1.5, dotsize=1.2)+
  stat_summary(fun.y = mean, fun.ymin = fun.low.mean, fun.ymax = fun.up.mean, colour = "red", size = 0.7)

3,使用fun.data向點圖中增加點范圍

data_summary <- function(x) {
  m <- mean(x)
  ymin <- m-sd(x)
  ymax <- m+sd(x)
  return(c(y=m,ymin=ymin,ymax=ymax))
}

ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_dotplot(binaxis='y', stackdir='center', stackratio=1.5, dotsize=1.2)+
  stat_summary(fun.data =  data_summary, colour = "red", size = 0.7)

三,按照分組改變點圖的顏色

首先要對點圖分組,這通過aes(fill=dose)來實現,然后,使用scale_fill_manual()來設置每個分組的顏色: 

ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
  geom_dotplot(binaxis='y', stackdir='center')+
  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))

四,添加點圖的說明

 通過theme函數向點圖中增加說明,通過legend.position來控制說明的位置

ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
  geom_dotplot(binaxis='y', stackdir='center')+
  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
  theme(legend.position="top")

五,向點圖中增加標題和軸的標簽

通過labs()來添加標題和坐標軸的標簽 

ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
  geom_dotplot(binaxis='y', stackdir='center')+
  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
  labs(title="Plot of length  by dose",x="Dose (mg)", y = "Length")+
  theme_classic()

 

參考文檔:

ggplot2高效實用指南

ggplot2入門與進階(上)

Dot plot

ggplot2 dot plot : Quick start guide - R software and data visualization


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM