Part 1 :氣泡圖
氣泡圖是散點圖的一種變體图筹,一般的散點圖反映的是兩個連續(xù)變量之間的關系帅刀。而氣泡圖通常可以反映三個變量之間的關系远剩,第三個變量一般體現(xiàn)在氣泡的大小扣溺。當然,如果賦予氣泡不同的顏色瓜晤,那么也可利用其反映四個變量之間的關系锥余。
在實際使用中,氣泡圖常用于展示基因富集分析的結(jié)果痢掠。本期使用R包gapminder中現(xiàn)有數(shù)據(jù)集驱犹,基于ggplot2制作氣泡圖
Part 2 :圖像與代碼
在加載數(shù)據(jù)并對數(shù)據(jù)進行簡單的篩選后嘲恍,很容易做出一個簡單的氣泡圖:
最基礎的氣泡圖
#加載相關包
library(ggplot2)
library(dplyr)
#install.packages("gapminder")
library(gapminder)
#簡單的數(shù)據(jù)篩選,篩選去year=2007的數(shù)據(jù)雄驹,同時將"year"一列刪除
data <- gapminder %>% filter(year=="2007") %>% dplyr::select(-year)
#基本的氣泡圖
bp1 = ggplot(data, aes(x=gdpPercap, y=lifeExp, size = pop)) +
geom_point(alpha=0.7)
#size = pop ,表示用數(shù)據(jù)中的pop值來表示氣泡的大小
在此基礎上佃牛,我們只需要增加億點點細節(jié),就可以得到如下的圖像:
增加細節(jié)后的氣泡圖
#加載相關包
library(ggplot2)
library(dplyr)
library(hrbrthemes)
library(viridis)
library(ggrepel)
#此處對數(shù)據(jù)做了簡單處理医舆,將pop統(tǒng)一縮蟹馈;把數(shù)據(jù)按pop(氣泡大惺呓)降序可以避免大圈出現(xiàn)在圖像s行方
tmp_data <- data %>%
mutate(pop=pop/1000000) %>%
arrange(desc(pop)) %>%
mutate(country = factor(country, country))
bp2 <- ggplot(tmp_data, aes(x = gdpPercap, y=lifeExp, size = pop, color = continent)) +
geom_point(alpha=0.5) +
scale_size(range = c(1.5, 20), name="Population (M)") +
scale_color_viridis(discrete=TRUE) +
theme_ipsum() +
theme(
legend.position = c(1, 0),
legend.justification = c(1, 0))+
geom_text_repel(data = tmp_data, aes(label=country), size=3) #安裝country給所有氣泡加注釋
上圖中爷速,我們?yōu)樗袣馀菰黾恿俗⑨專坪跤^感并不好霞怀。我們可以有選擇的為部分感興趣的氣泡加注釋遍希。如果將代碼
tmp_data <- data %>%
mutate(pop=pop/1000000) %>%
arrange(desc(pop)) %>%
mutate(country = factor(country, country))
#及
geom_text_repel(data = tmp_data, aes(label=country), size=3)
改為:
#篩選感興趣的數(shù)據(jù),并為其加注釋
tmp_data <- data %>%
mutate(
annotation = case_when(
gdpPercap > 5000 & lifeExp < 60 ~ "yes",
lifeExp < 30 ~ "yes",
gdpPercap > 40000 ~ "yes")
) %>%
mutate(pop=pop/1000000) %>%
arrange(desc(pop)) %>%
mutate(country = factor(country, country))
#及
geom_text_repel(data=tmp_data %>% filter(annotation=="yes"), aes(label=country), size=3 )
可以得到下圖:
完善后的氣泡圖
在此基礎上里烦,我們可以根據(jù)需要修改氣泡的大小凿蒜、配色方案等,以制作出所需氣泡圖
在上期散點圖(1)— 基礎散點圖中胁黑,我們復現(xiàn)了Nature Communications文章中的一幅散點圖废封,并給出了完整代碼。實際上丧蘸,該文章中還使用了如下的散點圖漂洋,在此我們補充給出復現(xiàn)代碼
library(ggplot2)
cols <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
#fig2:
crass_impact <- read.table("crass_impact.txt")
p = ggplot(crass_impact, aes(x = rel_crAss, y = rel_res, color = country)) +
geom_smooth(method = "lm") +
geom_point(aes(shape = crAss_detection), size =9 ) +
scale_x_log10() +
scale_y_log10() +
theme_classic() +
labs(y = "Normalized ARG abundance", x="Normalized crAssphage abundance",
color = "Study", shape = "crAssphage detection") + scale_colour_manual(values = cols)
library(ggplot2)
crass_wwtp <- read.table("crass_wwtp.txt")
p4 <- ggplot(crass_wwtp, aes(rel_crAss, rel_res, color = country_wwtp)) +
geom_smooth(method = "lm") +
geom_point(size = 8) +
scale_x_log10() +
scale_y_log10() +
theme_classic() +
scale_colour_manual(values = cols) +
labs(y = "Normalized ARG abundance", x="Normalized crAssphage abundance",
color = "Country:WWTP")+
theme(
legend.position = c(0.1, 1),
legend.justification = c(0.1, 1)) #注意,此處的刻度并非實際途中標尺刻度力喷;可以理解為繪圖區(qū)域為一個1x1的坐標系刽漂,0.5x0.5為中心點
以上使用到的數(shù)據(jù)集,均可在散點圖(1)— 基礎散點圖文中提供的鏈接中獲取
參考:
歡迎關注公眾號:生信小書生
定期分享各類生信知識弟孟、技能