专栏首页优雅Rpatternplot包:用ggplot解决你对线性填充,不!所有填充的全部幻想。

patternplot包:用ggplot解决你对线性填充,不!所有填充的全部幻想。

写在前面

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

可选参数

主要有三种:

  • 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

全部黑白 中文期刊格式

?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

这里学习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

使用grid进行拼图

library(gridExtra)
grid.arrange(pie1,pie2,  nrow = 1)

使用自定义图形进行填充

只需要将各自的图形赋值给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')

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

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)

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

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

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

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')
#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)

修改图形方向

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)

修改图形外观

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)

reference

  • https://cran.r-project.org/web/packages/patternplot/vignettes/patternplot-intro.html

本文分享自微信公众号 - 优雅R(elegant-r)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2020-06-10

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 「R」数据可视化3 : 热图

    本文作者蒋刘一琦,自嘲是一个有艺术追求的生信狗,毕业于浙江大学生物信息学专业,目前在复旦大学就读研究生,研究方向为宏基因组。

    王诗翔呀
  • 「Workshop」第十七期 奇异值分解

    奇异值分解(Singular Value Decomposition,以下简称SVD)是在机器学习领域广泛应用的算法,它不光可以用于降维算法中的特征分解,还可以...

    王诗翔呀
  • 使用 py12306 购票

    首先需要 Python 环境,下面安装了一个 Miniconda,它会带 Python,如果已经有的话可以跳过。

    王诗翔呀
  • 一图读懂腾讯Q2财报:助力经济复苏!总收入1148.83亿元

    点击上方“腾讯云TStack”,关注我们,获取最in云端资讯和海量技术干货~ 今天,腾讯发布了2020年第二季度业绩报告。 ? ? ? ?

    腾讯云TStack
  • C++ 多进程并发框架FFLIB之Tutorial

          FFLIB框架是为简化分布式/多进程并发而生的。它起始于本人尝试解决工作中经常遇到的问题如消息定义、异步、多线程、单元测试、性能优化等。基本介绍可以...

    知然
  • 重新定义时间轴

    最近读了Reid Havens在PowerPivotPro上发表的一篇《产品上线时间后比较表现》的文章,不同产品上线的时间不同,通过自定义时间轴来把所有产品的上...

    公众号PowerBI大师
  • 物联网|2018世界杯,资深球迷必知的6点!

    足球作为全世界最多人关注的运动,赛场上每个运动员的汗水挥洒都牵动着每个球迷的心,每次的进球都刺激着无数观众的荷尔蒙。现在正值世界杯火热举办期间,这项四年一届举世...

    用户2356481
  • 逆向分析智能窗帘频射协议

    近来我热衷于对家庭自动化设备的破解,然后将它们添加到我的Homekit集成包之中。这事情要从几个月前说起,当时我爸订购了大批量的RAEX 433MHz射频电动窗...

    FB客服
  • 如何衡量一个国家是不是真的富裕了

    嫂子是个很有才华的姑娘,早年也有赴日留学的经历,周天去串门,听嫂子讲了讲她以前的故事,通过她朴实略带励志的话语里,我突然发现自己以前的一个想法是错误的,之前一直...

    用户2936994
  • 使用monit搭建一个监控系统

    马哥linux运维 | 最专业的linux培训机构 ---- 上周用monit搭建或者说定制了一个监控系统,来监控服务器发生事情。当然了主要是监控异常,因为我...

    小小科

扫码关注云+社区

领取腾讯云代金券