![d253dbd101048ae0421632a6f8883d9e.png](https://i-blog.csdnimg.cn/blog_migrate/1432959ffa46891faef9fc91b5fe1a3e.jpeg)
![5a5478ff78d9d251616d6cee5fc36b63.png](https://i-blog.csdnimg.cn/blog_migrate/b3c369df6da2720e38de983c5969ced2.jpeg)
写在前面
patternplot包,提供了丰度的图形可视化填充选项,但是目前我尽然没忽悠看到一篇推文来介绍和学习这个R包的。
大家都知道,柱状图我们在中文中常见填充的除了颜色,还有形状,用不同的线填充,区分不同分组,因为中文期刊彩色版面费贵一些,所以很多老师都会使用形状填充柱状图来节省经费。这样也显得低调和朴素。
但是你们有没有想过,这些填充不同线条的图形几乎都不是R语言做的。说狭隘一点,R语言不并没有成熟的解决方案。
今天我介绍的这个R包,patternplot包可以很完美的解决这个应用,我想相信这回cover大部分人的需求,因为我们在R语言中做出这种线条区分的图形实在是太少了。
patternplot 包
安装R包,这个包依赖ggplot,还是很可以的,但是就是使用方法不是很ggplot,如果作者能够将这种方式继续进行改造,相信引用会很高的。目前这个包已经上了cran,大家直接运行下面代码即可安装,这里我注释掉了,大家需要去除“#”即可。
# install.packages("patternplot")
library(patternplot)
library(png)
library(ggplot2)
饼图
data <- read.csv(system.file("extdata", "vegetables.csv", package="patternplot"))
data $pct
下面我们通过一个简单的例子 演示用法
有三个参数是必要的,必须设置,就是下面三个:
分组,数据,分组标签,填充模式。
#--填充样式
pattern.type<-c('hdashes', 'vdashes', 'bricks')
pie1<-patternpie(
group=data$group,
pct=data$pct,# 饼图分组名称
label=data$label, # 标签
pattern.type=pattern.type,
)
pie1
![da51842cbb082a4c400385cc069760e5.png](https://i-blog.csdnimg.cn/blog_migrate/8eac88fd39bef5a39e2525ee6ac2cc26.png)
可选参数
主要有三种:
- label,标签设置
- frame边框设置
- density 填充密度
?patternpie
#--填充样式
pattern.type<-c('hdashes', 'vdashes', 'bricks')
pie1<-patternpie(
group=data$group,
pct=data$pct,# 饼图分组名称
label=data$label, # 标签
label.size=4,
label.color='black', # 标签
label.distance=1.3, # 标签距离
pattern.type=pattern.type,
pattern.line.size=c(10, 10, 2), # 设置填充的线尺寸
frame.color='red', # 每个部分边框颜色
frame.size=1,# 边全部框的粗细
pixel=12, # 分辨率,图形的
density=c(8, 8, 30)# 设置填充的密度
#
)
pie1
pie1<-pie1+ggtitle('(A) Black and White with Patterns')
pie1
![1cc637029bca09374946af47364e6b6d.png](https://i-blog.csdnimg.cn/blog_migrate/024bf6fa376d91e499c58113bd67dc8e.jpeg)
全部黑白 中文期刊格式
?patternpie
#Example 1
pattern.type<-c('hdashes', 'vdashes', 'bricks')
pie1<-patternpie(group=data$group,
pct=data$pct,
label=data$label,
label.size=4,
label.color='black',
label.distance=1.3,
pattern.type=pattern.type,
pattern.line.size=c(10, 10, 2),
frame.color='black',
frame.size=1.5,
pixel=12,
density=c(8, 8, 10))
pie1<-pie1+ggtitle('(A) Black and White with Patterns')
pie1
![f2111c5dc82dedd8e5c3d91dd971b52c.png](https://i-blog.csdnimg.cn/blog_migrate/0fb92d646a51906bcda83fba6ccb3afe.png)
这里学习pattern.color:设置每种模式的颜色,pattern.color设置每块背景颜色
#Example 2
pattern.color<-c('red3','green3', 'white' )
background.color<-c('dodgerblue', 'lightpink', 'orange')
pie2<-patternpie(group=data$group,
pct=data$pct,
label=data$label,
label.distance=1.3,
pattern.type=pattern.type,#设置样式
pattern.color=pattern.color,# 设置颜色
background.color=background.color,
pattern.line.size=c(10, 10, 2), frame.color='grey40',frame.size=1.5, pixel=12, density=c(8, 8, 10))
pie2<-pie2+ggtitle('(B) Colors with Patterns')
pie2
![da74f16cdb8ed9de52ba10afee958bd5.png](https://i-blog.csdnimg.cn/blog_migrate/3c72818ed12f39091f553ff65c189dba.png)
使用grid进行拼图
library(gridExtra)
grid.arrange(pie1,pie2, nrow = 1)
![9cacb3ceead12990f341c255230845bf.png](https://i-blog.csdnimg.cn/blog_migrate/8a4c7f502933218d8b0d8e2aad287b9a.jpeg)
使用自定义图形进行填充
只需要将各自的图形赋值给pattern.type。即可
这些图片作为列表赋值给pattern.type
library(patternplot)
library(ggplot2)
library(jpeg)
Tomatoes <- readJPEG(system.file("img", "tomatoes.jpg", package="patternplot"))
Peas <- readJPEG(system.file("img", "peas.jpg", package="patternplot"))
Potatoes <- readJPEG(system.file("img", "potatoes.jpg", package="patternplot"))
#Example 1
data <- read.csv(system.file("extdata", "vegetables.csv", package="patternplot"))
pattern.type<-list(Tomatoes,Peas,Potatoes)
imagepie(group=data$group,
pct=data$pct,
label=data$label,
pattern.type=pattern.type,
label.distance=1.3,
frame.color='burlywood4',
frame.size=0.8,
label.size=6,
label.color='forestgreen')+ggtitle('Pie Chart with Images')
![9d2a13ce972d80073fe730dedc6866fb.png](https://i-blog.csdnimg.cn/blog_migrate/b5c7518388408cbfe2f99633dbd41b1f.jpeg)
patternring1函数:用于环状饼图绘制
group1<-c('New_England', 'Great_Lakes','Plains', 'Rocky_Mountain', 'Far_West','Southwest', 'Southeast', 'Mideast')
pct1<-c( 12, 11, 17, 15, 8, 11, 16, 10)
#--设置标签分行
label1<-paste(group1, " n ", pct1, "%", sep="")
#---设置填充模式
pattern.type1<-c("hdashes", "blank", "grid", "blank", "hlines", "blank", "waves", "blank")
#--中间空
pattern.type.inner<-"blank"
#-颜色为白色
pattern.color1<-rep("white", 8)
#--背景颜色设置
background.color1<-c("darkgreen", "darkcyan", "chocolate", "cadetblue1", "darkorchid", "yellowgreen", "hotpink", "lightslateblue")
density1<-rep(11.5, length(group1))
pattern.line.size1=c(10, 1, 6, 1, 10, 1, 6, 1)
g<-patternring1(group1,
pct1,
label1,
label.size1=4,
label.color1='black',
label.distance1=1.36,
pattern.type1,
pattern.color1,
pattern.line.size1,
background.color1,
frame.color='black',
frame.size=1.2,
density1,
pixel=13,
pattern.type.inner="blank",
pattern.color.inner="white",
pattern.line.size.inner=1,
background.color.inner="white",
pixel.inner=10,
density.inner=1,
r1=3,
r2=6
)
g
g<-g+annotate(geom="text", x=0, y=0, label="2019 Number of Cases n N=1000",color="black", size=4)+scale_x_continuous(limits=c(-7, 7))+scale_y_continuous(limits=c(-7, 7))
g
![fa4def33361b6a2fdfda42d8b9da309c.png](https://i-blog.csdnimg.cn/blog_migrate/0126a303e75c9c65a62bb0abfd739332.png)
#Example 1
library(patternplot)
library(png)
library(ggplot2)
?pattern
location<-gsub('','/',tempdir(), fixed=T)
pattern(type="blank", density=1, color='white', pattern.line.size=1, background.color="darkgreen", pixel=8, res=8)
FarWest<-readPNG(paste(location,'/',"blank",".png", sep=''))
pattern(type="blank", density=1, color='white', pattern.line.size=1, background.color="darkcyan", pixel=8, res=8)
GreatLakes<-readPNG(paste(location,'/',"blank",".png", sep=''))
pattern(type="blank", density=1, color='white', pattern.line.size=1, background.color="chocolate", pixel=8, res=8)
Mideast<-readPNG(paste(location,'/',"blank",".png", sep=''))
pattern(type="blank", density=1, color='white', pattern.line.size=1, background.color="cadetblue1", pixel=8, res=8)
NewEngland<-readPNG(paste(location,'/',"blank",".png", sep=''))
pattern(type="blank", density=1, color='white', pattern.line.size=1, background.color="darkorchid", pixel=8, res=8)
Plains<-readPNG(paste(location,'/',"blank",".png", sep=''))
pattern(type="blank", density=1, color='white', pattern.line.size=1, background.color="yellowgreen", pixel=8, res=8)
RockyMountain<-readPNG(paste(location,'/',"blank",".png", sep=''))
pattern(type="blank", density=1, color='white', pattern.line.size=1, background.color="hotpink", pixel=8, res=8)
Southeast<-readPNG(paste(location,'/',"blank",".png", sep=''))
pattern(type="blank", density=1, color='white', pattern.line.size=1, background.color="lightslateblue", pixel=8, res=8)
Southwest <-readPNG(paste(location,'/',"blank",".png", sep=''))
group1<-c('New_England', 'Great_Lakes','Plains', 'Rocky_Mountain', 'Far_West','Southwest', 'Southeast', 'Mideast')
pct1<-c( 12, 11, 17, 15, 8, 11, 16, 10)
label1<-paste(group1, " n ", pct1, "%", sep="")
pattern.type1<-list(NewEngland, GreatLakes,Plains, RockyMountain, FarWest,Southwest, Southeast, Mideast)
pattern.type.inner<-readPNG(system.file("img", "USmap.png", package="patternplot"))
g<-imagering1(group1,
pct1,
pattern.type1,
pattern.type.inner,
frame.color='black',
frame.size=1.5,
r1=3,
r2=4,label1,
label.size1=4,
label.color1='black',
label.distance1=1.3)
g
![ef479a5ed7ccebb364efeaac70813df1.png](https://i-blog.csdnimg.cn/blog_migrate/86bd6164725ffb2139bc13707742e2ef.png)
g<-g+annotate(geom="text", x=0, y=-2, label="2019 Number of Cases n N=1000",color="black", size=4)+scale_x_continuous(limits=c(-6, 6))+scale_y_continuous(limits=c(-6, 6))
g
library(patternplot)
library(png)
library(ggplot2)
group1<-c("Wind", "Hydro", "Solar", "Coal", "Natural Gas", "Oil")
pct1<-c(12, 15, 8, 22, 18, 25)
label1<-paste(group1, " n ", pct1 , "%", sep="")
group2<-c("Renewable", "Non-Renewable")
pct2<-c(35, 65)
label2<-paste(group2, " n ", pct2 , "%", sep="")
pattern.type1<-rep(c( "blank"), times=6)
pattern.type2<-c('grid', 'blank')
pattern.type.inner<-"blank"
pattern.color1<-rep('white', length(group1))
pattern.color2<-rep('white', length(group2))
background.color1<-c("darkolivegreen1", "white", "indianred", "gray81", "white", "sandybrown" )
background.color2<-c("seagreen", "deepskyblue")
density1<-rep(10, length(group1))
density2<-rep(10, length(group2))
pattern.line.size1=rep(5, length(group1))
pattern.line.size2=rep(2, length(group2))
pattern.line.size.inner=1
#Example 1: Two rings
g<-patternrings2(group1, group2, pct1,pct2, label1, label2, label.size1=3, label.size2=3.5, label.color1='black', label.color2='black', label.distance1=0.75, label.distance2=1.4, pattern.type1, pattern.type2, pattern.color1,pattern.color2,
pattern.line.size1, pattern.line.size2, background.color1, background.color2,density1=rep(10, length(group1)), density2=rep(15, length(group2)),pixel=10, pattern.type.inner, pattern.color.inner="black",pattern.line.size.inner, background.color.inner="white", pixel.inner=6, density.inner=5, frame.color='black',frame.size=1.5,r1=2.45, r2=4.25, r3=5)
g1<-g+annotate(geom="text", x=0, y=0, label="Earth's Energy",color="black", size=5)+scale_x_continuous(limits=c(-6, 6))+scale_y_continuous(limits=c(-6, 6))+ggtitle("(A) Two Rings")
#Example 2: Pie in a ring
g<-patternrings2(group1, group2, pct1,pct2, label1, label2, label.size1=3, label.size2=3.5, label.color1='black', label.color2='black', label.distance1=0.7, label.distance2=1.4, pattern.type1, pattern.type2, pattern.color1,pattern.color2,
pattern.line.size1, pattern.line.size2, background.color1, background.color2,density1=rep(10, length(group1)), density2=rep(15, length(group2)),pixel=10, pattern.type.inner, pattern.color.inner="black",pattern.line.size.inner, background.color.inner="white", pixel.inner=2, density.inner=5, frame.color='black',frame.size=1.5, r1=0.005, r2=4, r3=4.75)
g2<-g+scale_x_continuous(limits=c(-6, 6))+scale_y_continuous(limits=c(-6, 6))+ggtitle("(B) Pie in a Ring")
library(gridExtra)
grid.arrange(g1,g2, nrow = 1)
![4ebebb56b98e0b745da2a82af72054ed.png](https://i-blog.csdnimg.cn/blog_migrate/3384f3a3434f7d4ada4cd83b32ffc715.jpeg)
#Example 1
library(patternplot)
library(png)
library(ggplot2)
group1<-c("Wind", "Hydro", "Solar", "Coal", "Natural Gas", "Oil")
pct1<-c(12, 15, 8, 22, 18, 25)
label1<-paste(group1, " n ", pct1 , "%", sep="")
location<-gsub('','/',tempdir(), fixed=T)
pattern(type="blank", density=1, color='white', pattern.line.size=1, background.color="darkolivegreen1", pixel=20, res=15)
Wind<-readPNG(paste(location,'/',"blank",".png", sep=''))
pattern(type="blank", density=1, color='white', pattern.line.size=1, background.color="white", pixel=20, res=15)
Hydro<-readPNG(paste(location,'/',"blank",".png", sep=''))
pattern(type="blank", density=1, color='white', pattern.line.size=1, background.color="indianred", pixel=20, res=15)
Solar<-readPNG(paste(location,'/',"blank",".png", sep=''))
pattern(type="blank", density=1, color='white', pattern.line.size=1, background.color="gray81", pixel=20, res=15)
Coal<-readPNG(paste(location,'/',"blank",".png", sep=''))
pattern(type="blank", density=1, color='white', pattern.line.size=1, background.color="white", pixel=20, res=15)
NaturalGas<-readPNG(paste(location,'/',"blank",".png", sep=''))
pattern(type="blank", density=1, color='white', pattern.line.size=1, background.color="sandybrown", pixel=20, res=15)
Oil<-readPNG(paste(location,'/',"blank",".png", sep=''))
pattern.type1<-list(Wind, Hydro, Solar, Coal, NaturalGas, Oil)
group2<-c("Renewable", "Non-Renewable")
pct2<-c(35, 65)
label2<-paste(group2, " n ", pct2 , "%", sep="")
pattern(type="grid", density=12, color='white', pattern.line.size=5, background.color="seagreen", pixel=20, res=15)
Renewable<-readPNG(paste(location,'/',"grid",".png", sep=''))
pattern(type="blank", density=1, color='white', pattern.line.size=1, background.color="deepskyblue", pixel=20, res=15)
NonRenewable<-readPNG(paste(location,'/',"blank",".png", sep=''))
pattern.type2<-list(Renewable, NonRenewable)
pattern.type.inner<-readPNG(system.file("img", "earth.png", package="patternplot"))
g<-imagerings2(group1, group2,pct1,pct2, label1, label2, label.size1=3, label.size2=3.5, label.color1='black', label.color2='black', label.distance1=0.7, label.distance2=1.3, pattern.type1, pattern.type2, pattern.type.inner, frame.color='skyblue',frame.size=1.5, r1=2.2, r2=4.2, r3=5)
g<-g+scale_x_continuous(limits=c(-7, 7))+scale_y_continuous(limits=c(-7, 7))
g
![40258e99c361664e2999926d85c74a7e.png](https://i-blog.csdnimg.cn/blog_migrate/7b302939e99d72fa201779a242558dad.png)
patternbar函数:模式填充柱状图
#Example 1
library(patternplot)
library(png)
library(ggplot2)
data <- read.csv(system.file("extdata", "monthlyexp.csv", package="patternplot"))
data<-data[which(data$Location=='City 1'),]
x<-factor(data$Type, c('Housing', 'Food', 'Childcare'))
y<-data$Amount
pattern.type<-c('hdashes', 'blank', 'crosshatch')
pattern.color=c('black','black', 'black')
background.color=c('white','white', 'white')
density<-c(20, 20, 10)
barp1<-patternbar(data,x, y,group=NULL,ylab='Monthly Expenses, Dollars', pattern.type=pattern.type, hjust=0.5,
pattern.color=pattern.color, background.color=background.color,pattern.line.size=c(5.5, 1, 4),
frame.color=c('black', 'black', 'black'), density=density)+scale_y_continuous(limits = c(0, 2800))+ggtitle('(A) Black and White with Patterns')
#Example 2
pattern.color=c('black','white', 'grey20')
background.color=c('lightgreen','lightgreen', 'lightgreen')
barp2<-patternbar(data,x, y,group=NULL,ylab='Monthly Expenses, Dollars', pattern.type=pattern.type,hjust=0.5,
pattern.color=pattern.color, background.color=background.color,pattern.line.size=c(5.5, 1, 4),
frame.color=c('black', 'black', 'black'), density=density)+scale_y_continuous(limits = c(0, 2800))+ggtitle('(B) Colors with Patterns')
library(gridExtra)
grid.arrange(barp1,barp2, nrow = 1)
![1ec5279596cfa939602e12e7a7b21fe8.png](https://i-blog.csdnimg.cn/blog_migrate/25e7b664b8f9e144276844c0776a2014.jpeg)
patternbar函数:模式柱状图
#Example 3
data <- read.csv(system.file("extdata", "monthlyexp.csv", package="patternplot"))
group<-factor(data$Type, c('Housing', 'Food', 'Childcare'))
y<-data$Amount
x<-factor(data$Location, c('City 1', ' City 1'))
pattern.type<-c( 'Rsymbol_16', 'blank','hdashes')
pattern.color=c('yellow', 'chartreuse4', 'pink')
background.color=c('grey', 'chartreuse3', 'bisque')
barp3<-patternbar(data,x, y,group,ylab='Monthly Expenses, Dollars', pattern.type=pattern.type,
pattern.color=pattern.color,background.color=background.color, pattern.line.size=c(6, 10,6),
frame.size=1,frame.color='black',pixel=16, density=c(18, 10, 14), legend.type='h',
legend.h=12, legend.y.pos=0.49, vjust=-1, hjust=0.5,legend.pixel=6, legend.w=0.275,legend.x.pos=1.1) +scale_y_continuous(limits = c(0, 3100))+ggtitle('(C) Bar Chart with Two Grouping Variables')
barp3
![be7bdaea6ba5bf7b9cac73fcf35070eb.png](https://i-blog.csdnimg.cn/blog_migrate/90431ffb32f01271ab5488cade7fb974.jpeg)
patternbar_s函数: 模式填充的对对柱状图
#Example 1
library(patternplot)
library(png)
library(ggplot2)
data <- read.csv(system.file("extdata", "monthlyexp.csv", package="patternplot"))
x<-data$Location
y<-data$Amount
group<-data$Type
patternbar_s(data,x, y, group,xlab='', ylab='Monthly Expenses, Dollar', label.size=3,pattern.type=c( 'Rsymbol_16', 'blank','hdashes'), pattern.line.size=c(5, 10, 10),frame.size=1,pattern.color=c('yellow', 'chartreuse4', 'pink'),background.color=c('grey', 'chartreuse3', 'bisque'), pixel=16, density=c(18, 10, 10),frame.color='black', legend.type='h', legend.h=12, legend.y.pos=0.49, legend.pixel=6, legend.w=0.275, legend.x.pos=1.05,legend.label=c("Childcare", "Food", "Housing" ), bar.width=0.8)+scale_y_continuous(limits = c(0, 6800))+ggtitle('Stacked Bar Chart')
![810e3ad16d59095facdb49136755e6b9.png](https://i-blog.csdnimg.cn/blog_migrate/c896563c25ea273042abcdc33d816ae4.png)
library(patternplot)
library(jpeg)
library(ggplot2)
childcare<-readJPEG(system.file("img", "childcare.jpg", package="patternplot"))
food<-readJPEG(system.file("img", "food.jpg", package="patternplot"))
housing <-readJPEG(system.file("img", "housing.jpg", package="patternplot"))
#Example 1
data <- read.csv(system.file("extdata", "monthlyexp.csv", package="patternplot"))
data<-data[which(data$Location=='City 1'),]
x<-factor(data$Type, c('Housing', 'Food', 'Childcare'))
y<-data$Amount
pattern.type<-list(housing, food, childcare)
imagebar(data,x, y,group=NULL,pattern.type=pattern.type,vjust=-1, hjust=0.5,
frame.color='black',
ylab='Monthly Expenses, Dollars')+ggtitle('(A) Bar Chart with Images')
imagebar函数:图像填充的柱状图
#Example 2
data <- read.csv(system.file("extdata", "monthlyexp.csv", package="patternplot"))
group<-factor(data$Type, c('Housing', 'Food', 'Childcare'))
y<-data$Amount
x<-factor(data$Location, c('City 1', ' City 1'))
pattern.type<-list(housing, food, childcare)
imagebar(data,x, y,group,pattern.type=pattern.type,vjust=-1, hjust=0.5,
frame.color='black',
ylab='Monthly Expenses, Dollars')+ggtitle('(B) Image Bar Chart with Two Grouping Variables')
![1e3552264776f084bb0d1001e68fa343.png](https://i-blog.csdnimg.cn/blog_migrate/7862211fdd499d4b5d5fc3f7fab4539a.jpeg)
imagebar_s函数:图像填充的堆叠柱状图
library(patternplot)
library(jpeg)
library(ggplot2)
childcare<-readJPEG(system.file("img", "childcare.jpg", package="patternplot"))
food<-readJPEG(system.file("img", "food.jpg", package="patternplot"))
housing <-readJPEG(system.file("img", "housing.jpg", package="patternplot"))
data <- read.csv(system.file("extdata", "monthlyexp.csv", package="patternplot"))
x<-data$Location
y<-data$Amount
group<-data$Type
pattern.type<-list(childcare, food, housing)
imagebar_s(data,x, y, group, xlab='', ylab='Monthly Expenses, Dollar', pattern.type=pattern.type, label.size=3.5, frame.size=1.25, frame.color='black',legend.type='h', legend.h=6, legend.y.pos=0.49, legend.pixel=20, legend.w=0.2,legend.x.pos=1.1, legend.label=c("Childcare", "Food", "Housing" ))+ scale_y_continuous(limits = c(0, 6800))+ggtitle('Stacked Bar Chart with Images')
![23339de8942b6f16a60f75ba45b2c57e.png](https://i-blog.csdnimg.cn/blog_migrate/bf1c9ddba2cf25162b81d0b35a064e77.jpeg)
patternboxplot函数:箱线图
#Example 1
data <- read.csv(system.file("extdata", "fruits.csv", package="patternplot"))
group<-data$Fruit
y<-data$Weight
x<-data$Store
pattern.type<-c('nwlines', 'blank', 'waves')
pattern.color=c('black','black', 'black')
background.color=c('white','gray80', 'white')
frame.color=c('black', 'black', 'black')
pattern.line.size<-c(6, 1,6)
density<-c(6, 1, 8)
box1<-patternboxplot(data,x, y,group,pattern.type=pattern.type,pattern.line.size=pattern.line.size, label.size=3, pattern.color=pattern.color, background.color=background.color,frame.color=frame.color,
density=density, legend.h=2, legend.x.pos=1.075, legend.y.pos=0.499, legend.pixel=10,legend.w=0.18, legend.label=c("Orange","Strawberry","Watermelon"))+ggtitle('(A) Boxplot with Black and White Patterns')
#Example 2
pattern.color=c('black','white', 'grey20')
background.color=c('gold','lightpink', 'lightgreen')
box2<-patternboxplot(data,x, y,group=group,pattern.type=pattern.type,pattern.line.size=pattern.line.size, label.size=3,pattern.color=pattern.color, background.color=background.color,frame.color=frame.color, density=density,legend.h=2, legend.x.pos=1.075, legend.y.pos=0.499, legend.pixel=10,legend.w=0.18, legend.label=c("Orange","Strawberry","Watermelon"))+ggtitle('(B) Boxplot with Colors and Patterns')
library(gridExtra)
grid.arrange(box1,box2, nrow = 1)
imageboxplot函数,图形填充箱线图
library(patternplot)
library(jpeg)
library(ggplot2)
Orange<-readJPEG(system.file("img", "oranges.jpg", package="patternplot"))
Strawberry <-readJPEG(system.file("img", "strawberries.jpg", package="patternplot"))
Watermelon<-readJPEG(system.file("img", "watermelons.jpg", package="patternplot"))
#Example 1
data <- read.csv(system.file("extdata", "fruits.csv", package="patternplot"))
x<-data$Fruit
y<-data$Weight
group<-data$Store
pattern.type<-list(Orange, Strawberry, Watermelon)
box1<-imageboxplot(data,x, y,group=NULL,pattern.type=pattern.type,frame.color=c('orange','darkred',
'darkgreen'),legend.label="", ylab='Weight, Pounds')+ggtitle('(A) Image Boxplot with One Grouping Variable')
![1138f90e29bc2b0cf6cc1887f032f460.png](https://i-blog.csdnimg.cn/blog_migrate/a8b2e39efcf0962a63fb1c9b0f2a9342.jpeg)
#Example 2
x<-data$Store
y<-data$Weight
group<-data$Fruit
pattern.type<-list(Orange, Strawberry, Watermelon)
box2<-imageboxplot(data,x, y,group=group, pattern.type=pattern.type, frame.color=c('orange', 'darkred', 'darkgreen'), linetype=c('solid', 'dashed', 'dotted'),frame.size=0.8, xlab='', ylab='Weights, pounds', legend.h=2, legend.x.pos=1.1, legend.y.pos=0.499, legend.w=0.2, legend.label=c("Orange", "Strawberry", "Watermelon"))+ggtitle('(B) Image Boxplot with Two Grouping Variables')
library(gridExtra)
grid.arrange(box1,box2, nrow = 1)
![46071e3f74ee4d20b8319fbff0799719.png](https://i-blog.csdnimg.cn/blog_migrate/03e06850315551144d6bbe053c758d13.jpeg)
修改图形方向
library(patternplot)
library(png)
library(ggplot2)
data <- read.csv(system.file("extdata", "monthlyexp.csv", package="patternplot"))
data<-data[which(data$Location=='City 1'),]
x<-factor(data$Type, c('Housing', 'Food', 'Childcare'))
y<-data$Amount
pattern.type<-c('hdashes', 'blank', 'crosshatch')
pattern.color=c('black','black', 'black')
background.color=c('white','white', 'white')
density<-c(20, 20, 10)
g1<-patternbar(data,x, y,group=NULL,ylab='Monthly Expenses, Dollars', pattern.type=pattern.type,pattern.color=pattern.color, background.color=background.color,pattern.line.size=c(5.5, 1, 4),frame.color=c('black', 'black', 'black'), density=density, vjust=-1, hjust=0.5, bar.width=0.75)+scale_y_continuous(limits = c(0, 2800))+ggtitle('(A) Vertical Bar Chart')
g2<-patternbar(data,x, y,group=NULL,ylab='Monthly Expenses, Dollars', pattern.type=pattern.type,pattern.color=pattern.color, background.color=background.color,pattern.line.size=c(5.5, 1, 4),frame.color=c('black', 'black', 'black'), density=density, vjust=0.5, hjust=-0.25, bar.width=0.5)+scale_y_continuous(limits = c(0,2800))+ggtitle('(B) Horizontal Bar Chart')+coord_flip()
g3<-patternbar(data,x, y,group=NULL,ylab='Monthly Expenses, Dollars', pattern.type=pattern.type,pattern.color=pattern.color, background.color=background.color,pattern.line.size=c(5.5, 1, 4),frame.color=c('black', 'black', 'black'), density=density, vjust=2, hjust=0.5, bar.width=0.75)+ggtitle('(C) Reverse Bar Chart')+ scale_y_reverse(limits = c(2800,0))
library(gridExtra)
grid.arrange(g1,g2,g3, nrow = 1)
![a115058f32ebec74d9208510d65c625b.png](https://i-blog.csdnimg.cn/blog_migrate/f18c847b68fbcebae4bce0cdccdaeb5e.jpeg)
修改图形外观
library(patternplot)
library(png)
library(ggplot2)
data <- read.csv(system.file("extdata", "monthlyexp.csv", package="patternplot"))
data<-data[which(data$Location=='City 1'),]
x<-factor(data$Type, c('Housing', 'Food', 'Childcare'))
y<-data$Amount
pattern.type<-c('hdashes', 'blank', 'crosshatch')
pattern.color=c('black','black', 'black')
background.color=c('white','white', 'white')
density<-c(20, 20, 10)
g1<-patternbar(data,x, y,group=NULL,ylab='Monthly Expenses, Dollars', pattern.type=pattern.type,pattern.color=pattern.color, background.color=background.color,pattern.line.size=c(5.5, 1, 4),frame.color=c('black', 'black', 'black'), density=density, vjust=-1, hjust=0.5)+scale_y_continuous(limits = c(0, 2800))+ggtitle('(A) Bar Chart with Default Theme')
g2<-patternbar(data,x, y,group=NULL,ylab='Monthly Expenses, Dollars', pattern.type=pattern.type,pattern.color=pattern.color, background.color=background.color,pattern.line.size=c(5.5, 1, 4),frame.color=c('black', 'black', 'black'), density=density, vjust=-1, hjust=0.5)+scale_y_continuous(limits = c(0, 2800))+ggtitle('(B) Bar Chart with Classic Theme')+ theme_classic()
library(gridExtra)
grid.arrange(g1,g2, nrow = 1)
![b0c1785baf071daf5b227f404c3a9bab.png](https://i-blog.csdnimg.cn/blog_migrate/5c1a88b473b5a6de034ea94c025d83b9.jpeg)
添加主编微信 加入群聊
![ce7c0aaa93e492420ad98b65b79e08bf.png](https://i-blog.csdnimg.cn/blog_migrate/fa4b1bee5c1326e5aae96adee00f43e2.jpeg)
关于微生信生物 你想要的都在这里
微生信生物
reference
- https://cran.r-project.org/web/packages/patternplot/vignettes/patternplot-intro.html
![1f00d403c472aafee0f4c82faf344df0.png](https://i-blog.csdnimg.cn/blog_migrate/a61c03103d3f9c967d35930db1228a7e.jpeg)