1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > R绘图 第九篇:绘制散点图和气泡图(ggplot2)

R绘图 第九篇:绘制散点图和气泡图(ggplot2)

时间:2019-02-05 12:27:43

相关推荐

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和 dodgestack:堆叠,fill:填充,用于条形图

每个位置调整都对应一个函数position_xxx()。

使用aes()函数来设置映射参数,geom_point()函数可以使用的映射有:

xyalpha:设置点重叠部分的透明度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(,),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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。