首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言基础绘图教程——第4章:面积图和饼图

R语言基础绘图教程——第4章:面积图和饼图

作者头像
DoubleHelix
发布2019-08-09 16:52:38
1.9K0
发布2019-08-09 16:52:38
举报
文章被收录于专栏:生物信息云生物信息云

ggplot2绘制面积图

ggplot2绘制面积图的代码格式:

ggplot(sunspotyear, aes(x,y))+ geom_area()

绘制面积图,文件格式如下:

#draw simple area plot
ggplot(sunspotyear, aes(Year,Sunspots))+
  geom_area()

改变填充色

#change fill colour and alpha
ggplot(sunspotyear, aes(Year,Sunspots))+
  geom_area(colour="black", fill="blue", alpha=.2)

删除面积图下的黑线

#delete the black line under the area plot
ggplot(sunspotyear, aes(Year,Sunspots))+
  geom_area(fill="blue", alpha=.2) +
  geom_line()

按分组作图,文件格式如下:

library(ggplot2)

#read in data
data = read.table("area_plot2.txt", sep="\t", header=T)
levels(data$AgeGroup)
#relevel the factor 
data$AgeGroup = factor(as.character(data$AgeGroup), levels = c("<5", "5-14", "15-24", "25-34", "35-44", "45-54", "55-64", ">64"))

#draw area plot, amazing things will happen.
ggplot(data, aes(Year, Thousands, fill=AgeGroup)) +
  geom_area()

更改另一个配色面板并重新排列年龄组

#change another colorpanel and reorder the agegroup 
ggplot(data, aes(Year, Thousands, fill=AgeGroup)) +
  geom_area(color="black", size=0.2, alpha=0.4) +
  scale_fill_brewer(palette="Blues", breaks=rev(levels(data$AgeGroup)))

绘制饼图

pie()函数绘制饼图

案例数据格式:

代码:

#read in data
data = read.table("pie_plot.txt", sep="\t", header=T)
data

#draw pie chart using function "pie"
pie(data$Value, labels=data$Group)
pie(data$Value, labels=data$Group, radius=0.4)
pie(data$Value, labels=data$Group, radius=0.8, clockwise=T)
pie(data$Value, labels=data$Group, radius=0.8, clockwise=T, init.angle=90)
pie(data$Value, labels=data$Group, radius=0.8, clockwise=T, init.angle=90, density=20)
pie(data$Value, labels=data$Group, radius=0.8, clockwise=T, init.angle=90, density=20, col=rainbow(8))
pie(data$Value, labels=data$Group, radius=0.8, clockwise=T, init.angle=90, density=20, col=rainbow(8), border="black")
pie(data$Value, labels=data$Group, radius=0.8, clockwise=T, init.angle=90, density=NULL, col=rainbow(8), border="black", lty=2)
pie(data$Value, labels=data$Group, radius=0.8, clockwise=T, init.angle=90, density=NULL, col=rainbow(8), border="black", lty=2, main="My First Pie Chart using R")

优化参数,参数意思英文直译就是。

#用优化的参数绘制完美的par图
par(mar=c(0,6,6,6))
pie(data$Value, 
    labels=paste(data$Group,"(",substring(data$Value,0,4), ")"), 
    radius=0.8, 
    clockwise=T, 
    init.angle=9, 
    density=NULL, 
    col=rainbow(8), 
    border="black", 
    lty=2, 
    main="Fig1. XXX Value of different conditions"
)

绘制一个光碟,彩虹渐变色

#draw a round rainbow
par(mar=c(0,0,0,0))
#first start with 10
pie(rep(1,10), col=rainbow(10), lty=0, labels='', init.angle=90, border=NA)
#what will happen if set the number of the values to a big one, try 200 or even bigger one 2000
#Let's see the charm of R language
par(mar=c(0,0,0,0))
pie(rep(1,2000), col=rainbow(2000), lty=0, labels='', init.angle=90, border=NA)

绘制3D饼图

案例数据:

代码:

#read in data
data = read.table("3d_pie_plot.txt", sep="\t", header=T)
data

library(plotrix)
pie3D(data$Value, radius=0.8, height=0.1, theta=0.5,
      labels=paste(data$Group,substring(data$Value,0,4), sep="\n"), 
      explode=0.1, main="Fig1. XXX value of different conditions",
      labelrad=1.5, shade=0.5)

pie3D函数并没有设置顺或逆时针的参数,下面是解决方案。

#think of this, the pie3D function in package plotrix does NOT have the parameter "clockwise" like the function "pie"
#what should we do if we want to reverse the order of the pie chart? Think about that.
#Here is the answer.
data.rev = data[nrow(data):1,]
pie3D(data.rev$Value, radius=0.8, height=0.1, theta=0.5,
      labels=paste(data.rev$Group,substring(data.rev$Value,0,4), sep="\n"), 
      explode=0.1, main="Fig1. XXX value of different conditions",
      labelrad=1.5, shade=0.5)
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-08-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 MedBioInfoCloud 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ggplot2绘制面积图
  • 绘制饼图
相关产品与服务
图像处理
图像处理基于腾讯云深度学习等人工智能技术,提供综合性的图像优化处理服务,包括图像质量评估、图像清晰度增强、图像智能裁剪等。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档