强烈安利!!绘制可自定义填充图案的统计图-patternplot

2022-03-29 14:47:01 浏览数 (1)

导语

GUIDE ╲

饼图、环形图、柱状图和箱式图是我们论文写作和数据统计经常要绘制的统计图,常常是使用单一的颜色填充(ggplot2大家可能都用烦了吧),小编呕心沥血终于找到一个非常实用又高级的绘图R包。patternplot包不仅可以绘制美观和信息丰富的统计图,它可以用颜色或纹理或png或jpeg格式的任何外部图像填充统计图,让我们一起来看看吧!!

代码语言:javascript复制
install.packages("patternplot") #安装包
library(patternplot) #加载包
library(png) #加载png包
library(ggplot2) #加载ggplot2包

R包介绍

01

patternpie绘制饼图

代码语言:javascript复制
data <- read.csv(system.file("extdata", "vegetables.csv", package="patternplot"))
fix(data)

输入的数据格式和内容非常的简单,第一列是分组类别,第二列是数据占比,第三列是标签

1. 绘制黑白简约风饼图

代码语言:javascript复制
pattern.type<-c('hdashes', 'vdashes', 'bricks')
#用于填充图形的线条类型
#有以下类型 'blank', 'bricks', 'vdashes', 'hdashes',
#'crosshatch','dots', 'grid','hlines','nelines',
#''nwlines',’vlines’,’waves’,’Rsymbol_0’ to ’Rsymbol_25’
pie1<-patternpie(group=data$group,
                 pct=data$pct,
                 label=data$label,
                 ####group,pct,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')
#ggtitle给饼图加标题

2. 绘制彩色可爱风饼图

代码语言:javascript复制
pattern.type<-c('hdashes', 'vdashes', 'bricks')
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')

3. imagepie绘制指定填充图案的饼图

代码语言:javascript复制
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"))
#导入指定填充图片,这里你就可以用自己喜欢的图啦
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')

02

环形图

1. patternring1绘制彩色环形图

代码语言:javascript复制
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)
#环内线/点的填充颜色,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,
                #label.size1标签大小,label.distance1标签到图形距离
                pattern.color1, pattern.line.size1,
                background.color1,
                frame.color='black',
                frame.size=1.2,
                density1,  pixel=13,
                #pixel,环的像素分辨率
                pattern.type.inner="blank",
                #内圆填充线/点的形式
                pattern.color.inner="white",
                #内圆填充线/点颜色
                pattern.line.size.inner=1,  
                #内圆填充线/点的粗细/大小
                background.color.inner="white",
                #内圆填充背景颜色
                pixel.inner=10,
                #内圆像素分辨率
                density.inner=1,
                #内圆填充线/点d额分布密度
                r1=2.7, r2=4
                #r1是内圆半径,r2是外环半径
                )
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

2. imagering1含有内嵌图形的环形图

代码语言:javascript复制
location<-gsub('\','/',tempdir(), fixed=T)
###定义一个计算机的文件夹位置
pattern(type="blank", density=1, color='white',
        pattern.line.size=1, background.color="darkgreen", pixel=8, res=8)
#pattern是指生成png格式的模式,运行结束后会在location下生成定义好的blank.png图片
#以上是对圆环中的一部分进行定义
#type是填充线条/点的类型,density是填充密度,color是线条/点d的颜色
#pattern.line.size是填充线/点的粗细/大小,background.color背景颜色
#pixel是分辨率, res=是分辨率
FarWest<-readPNG(paste(location,'/',"blank",".png", sep=''))
#读取location下的blank.png图片以填充图形,该图的颜色为上面语句定义好的背景色
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=''))
####以上语句是在location创建所需颜色的图片,调用该图片以填充环内各成分(8个)

 
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<-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

3. patternrings2绘制多环图和多环饼图

代码语言:javascript复制
#设定所需数值:
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
#内圆填充的线/点的粗细/大小

(1)绘制多环图

代码语言:javascript复制
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")
g1

(2)绘制外环饼图

代码语言:javascript复制
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")
g2

(3)自定义填充图形

代码语言:javascript复制
#下面是定义第一环的数据、填充图形以及参数
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

03

patternbar条形图

代码语言:javascript复制
data <- read.csv(system.file("extdata", "monthlyexp.csv", package="patternplot"))
#读入绘图数据
fix(data)

1. 黑白简约

(1)竖直的条形图

代码语言:javascript复制
data<-data[which(data$Location=='City 1'),]
#提取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',
                  #ylab是y轴标题
                  pattern.type=pattern.type, hjust=0.5,
                  #hjust从每个条的顶部边框到标签的水平距离
                  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')
barp1

(2)水平条形图

代码语言:javascript复制
p2<-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),
                  vjust=0.5, hjust=-0.25, bar.width=0.5,
                  frame.color=c('black', 'black', 'black'),
                  density=density) 
  scale_y_continuous(limits = c(0, 2800)) 
  ggtitle('(A) Black and White with Patterns') coord_flip()
#coord_flip()翻转坐标轴
p2

(3)逆转条形图

代码语言:javascript复制
p3<-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),
               vjust=2, hjust=0.5, bar.width=0.75,
               frame.color=c('black', 'black', 'black'),
               density=density) 
  ggtitle('(C) Reverse Bar Chart') 
  scale_y_reverse(limits = c(2800,0))
p3

2. 彩色填充

代码语言:javascript复制
pattern.color=c('black','white', 'grey20')
background.color=c('darkolivegreen1','lightgreen', 'chocolate')
#填充颜色
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')
barp2

3. 多组展示

代码语言:javascript复制
data <- read.csv(system.file("extdata", "monthlyexp.csv", package="patternplot"))
x<-factor(data$Location, c('City 1', ' City 2'))
group<-factor(data$Type, c('Housing', 'Food',  'Childcare'))
#在用x因子分组之后,再用group分组
y<-data$Amount
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.type='h',图例的布局是水平的,如果legend.type=’v’图例的布局是垂直的
                  legend.h=12, legend.y.pos=0.49,
                  #legend.h图例boxes的高度
                  #legend.y.pos 改变y轴上图例位置
                  vjust=-1, hjust=0.5,legend.pixel=6,
                  #legend.pixel图例的分辨率
                  legend.w=0.275,legend.x.pos=1.1
                  #legend.w图例boxes的宽度
                  #legend.y.pos 改变x轴上图例位置
                  )  
  scale_y_continuous(limits = c(0, 3100)) 
  ggtitle(' Bar Chart with Two Grouping Variables')
barp3

4. patternbar_s堆积条形图

代码语言:javascript复制
x<-data$Location
y<-data$Amount
group<-data$Type
#patternbar_s绘制堆叠条形图
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,
             bar.width=0.8
             #bar.width条形宽度
             ) 
  scale_y_continuous(limits = c(0, 6800)) 
  ggtitle('Stacked Bar Chart')

5. imagebar自定义填充图片

(1)条形图

代码语言:javascript复制
library(jpeg)
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"))
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,
         #vjust从每个条的顶部边框到标签的垂直距离
         #hjust从每个条的顶部边框到标签的水平距离
         frame.color='black',
         ylab='Monthly Expenses, Dollars') 
  ggtitle('Bar Chart with Images')

(2)多组条形图

代码语言:javascript复制
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 2'))
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('Image Bar Chart with Two Grouping Variables')

(3)堆叠条形图

代码语言:javascript复制
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) 
  scale_y_continuous(limits = c(0, 6800)) 
  ggtitle('Stacked Bar Chart with Images')

04

patternboxplot箱式图

代码语言:javascript复制
data <- read.csv(system.file("extdata", "fruits.csv", package="patternplot"))
fix(data)

1. 黑白简洁箱式图

代码语言:javascript复制
y<-data$Weight
x<-data$Store #先根据Store分组
group<-data$Fruit  #再根据Fruit分组
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) 
  ggtitle('(A) Boxplot with Black and White Patterns')
box1

2. 彩色箱式图

代码语言:javascript复制
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) ggtitle('(B) Boxplot with Colors and Patterns')

3. imageboxplot自定义图片填充

(1)

代码语言:javascript复制
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"))
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'),
                   ylab='Weight, Pounds') 
  ggtitle('(A) Image Boxplot with One Grouping Variable')
box1

(2)

代码语言:javascript复制
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) 
  ggtitle('(B) Image Boxplot with Two Grouping Variables')
box2

小编总结

虽然代码好像很多,但其实都是简单易懂的,在使用的时候修改参数即可,小编就非常喜欢里面的各种线和波点图案,看起来蛮可爱的,如果你想绘制与众不同 的统计图,就来试试这个R包吧!

END

0 人点赞