R繪圖 第九篇:繪制散點圖和氣泡圖(ggplot2)


繪制散點圖(scatterplots)使用geom_point()函數,氣泡圖(bubblechart)也是一個散點圖,只不過點的大小由一個變量(size)來控制。散點圖潛在的最大問題是過度繪圖:當一個位置或相鄰的位置上出現有多個點,就可能把點繪制在彼此之上, 這會嚴重扭曲散點圖的視覺外觀,你可以通過使點變得透明(geom_point(alpha = 0.05))或者設置點的形狀(geom_point(shape = "."))來幫助解決該問題。

geom_point(mapping = NULL, data = NULL, stat = "identity",
  position = "identity", ..., na.rm = FALSE, show.legend = NA,
  inherit.aes = TRUE)

參數注釋:

  • stat:統計轉換(statistical transformation),默認值是identity,表明變量的值是就是統計的值;而統計函數count 需要對變量的值進行計數,統計值是計數的結果。
  • position:位置調整(Position adjustment),默認值是identity,不調整
  • mapping:映射參數

點的位置調整(Position adjustment)有多種方式:

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

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

使用aes()函數來設置映射參數,geom_point()函數可以使用的映射有:

  • x
  • y
  • alpha:設置點重疊部分的透明度
  • colour:點的顏色
  • fill:點的填充色
  • group:分組
  • shape:點形狀
  • size:點的大小
  • stroke:描邊

這些參數用於修改散點圖的圖形屬性。 

一,繪制基本的點圖

使用mtcars數據集來繪制散點圖,並根據cyl字段來設置每個點的顏色:

library(ggplo2)

ggplot(mtcars, aes(wt, mpg))+
  geom_point(aes(colour = factor(cyl)))

二,繪制氣泡圖

使用geom_point(),繪制氣泡圖,並添加水平線:

library(ggplot2)

#win.graph(width=5, height=4,pointsize=8)

df <- data.frame(year=rep(c(2017,2018),3),
                 product=rep(c('ProductA','ProductB','ProductC'),2),
                 ratio=runif(6, min = 0, max = 1))

df <- df[order(df$year),]
df
<- within(df,{bubblesize<- sqrt(df$ratio*10/pi)}) df$product <- factor(df$product,levels=unique(df$product),ordered=TRUE) ratio.mean <- mean(df$ratio) y.min <- min(df$ratio) mytheme <- theme_minimal()+ theme( panel.grid.major.y=element_blank(), panel.grid.minor.y=element_blank(), axis.text.x = element_text(angle = 45, hjust = 1), plot.title=element_text(hjust =0.5), axis.line.y=element_line(linetype=1,color='grey'), axis.line.x=element_line(linetype=1,color='grey'), axis.ticks = element_line(linetype=2,color='grey'), panel.grid=element_line(linetype=2,color='grey'), legend.background = element_rect(fill="gray90", size=0,color='white'), legend.text=element_text(face="bold",size=8), legend.title=element_text(face="bold",size=8), axis.text=element_text(face="bold",size=8) ) ggplot(data=df, mapping=aes(x=product,y=ratio,color=factor(year)))+ geom_point(stat= "identity",aes(size=bubblesize),alpha=0.7,show.legend = TRUE)+ guides(color=guide_legend(title="Year"))+ scale_size(range = c(1, 30),guide=FALSE)+ scale_color_manual(values=c("#666666","#FF0016"))+ scale_y_continuous(labels = scales::percent,limits=c(y.min,1))+ labs(x='Product',y='Increase ratio',title='Product increase ratio')+ geom_text(aes(y=ratio,label=scales::percent(ratio),hjust=0.5), size=3,color="black",position = position_dodge(width=0.00),check_overlap = FALSE) + mytheme+ geom_hline(yintercept = ratio.mean,linetype='dashed')+ annotate(geom='text',x=0,y=ratio.mean,label=scales::percent(ratio.mean),hjust=-0.4,vjust=-0.5);

其中,scale_size()圖層用於指定bubble的大小,annotate()函數用於為水平線添加文本說明:

 

參考文檔:

ggplot2 geom_point

Creating and Tweaking Bubble Chart with ggplot2


免責聲明!

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



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