R繪圖 第九篇:繪製散點圖和睦泡圖(ggplot2)

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

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

參數註釋:app

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

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

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

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

使用aes()函數來設置映射參數,geom_point()函數能夠使用的映射有:ui

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

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

一,繪製基本的點圖

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

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

二,繪製氣泡圖

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

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()函數用於爲水平線添加文本說明:htm

 

參考文檔:blog

ggplot2 geom_point

Creating and Tweaking Bubble Chart with ggplot2

相關文章
相關標籤/搜索