如何添加纹理来填充 ggplot2中的颜色

我目前使用 scale_brewer()的填充和这些看起来美丽的颜色(在屏幕上和通过彩色打印机) ,但打印相对均匀的灰色时,使用黑色和白色打印机。我搜索了在线 ggplot2文档,但是没有看到任何关于添加纹理来填充颜色的内容。有没有官方的 ggplot2方式来做这件事,或者有没有人有一个黑客,他们使用?我所说的纹理是指像对角线条、反向对角线条、网点图案等等这样的东西,当用黑白打印时,它们可以区分填充颜色。

114457 次浏览

目前不可能,因为网格(ggplot2用来绘制实际图形的图形系统)不支持纹理。对不起!

Ggplot 可以使用调色板。其中一些是“复印”友好的。那么这样的东西可能会为你工作吗?

ggplot(diamonds, aes(x=cut, y=price, group=cut))+
geom_boxplot(aes(fill=cut))+scale_fill_brewer(palette="OrRd")

在这种情况下,OrRd 是一个调色板,可以在 color brewer 网页上找到: http://colorbrewer2.org/

Photocopy Friendly: This indicates 一个给定的配色方案 经得起黑白的考验 影印 未能成功复印。 亮度的差异应该是 用序列格式保存。

嘿,伙计们,这里有一个小黑客,解决纹理问题在一个非常基本的时尚:

使一个条上的边框比其他条暗

EDIT: I've finally found time to give a brief example of this hack that allows at least 3 types of basic pattern in ggplot2. The code:

Example.Data<- data.frame(matrix(vector(), 0, 3, dimnames=list(c(), c("Value", "Variable", "Fill"))), stringsAsFactors=F)


Example.Data[1, ] <- c(45, 'Horizontal Pattern','Horizontal Pattern' )
Example.Data[2, ] <- c(65, 'Vertical Pattern','Vertical Pattern' )
Example.Data[3, ] <- c(89, 'Mesh Pattern','Mesh Pattern' )




HighlightDataVert<-Example.Data[2, ]
HighlightHorizontal<-Example.Data[1, ]
HighlightMesh<-Example.Data[3, ]
HighlightHorizontal$Value<-as.numeric(HighlightHorizontal$Value)
Example.Data$Value<-as.numeric(Example.Data$Value)


HighlightDataVert$Value<-as.numeric(HighlightDataVert$Value)
HighlightMesh$Value<-as.numeric(HighlightMesh$Value)
HighlightHorizontal$Value<-HighlightHorizontal$Value-5
HighlightHorizontal2<-HighlightHorizontal
HighlightHorizontal2$Value<-HighlightHorizontal$Value-5
HighlightHorizontal3<-HighlightHorizontal2
HighlightHorizontal3$Value<-HighlightHorizontal2$Value-5
HighlightHorizontal4<-HighlightHorizontal3
HighlightHorizontal4$Value<-HighlightHorizontal3$Value-5
HighlightHorizontal5<-HighlightHorizontal4
HighlightHorizontal5$Value<-HighlightHorizontal4$Value-5
HighlightHorizontal6<-HighlightHorizontal5
HighlightHorizontal6$Value<-HighlightHorizontal5$Value-5
HighlightHorizontal7<-HighlightHorizontal6
HighlightHorizontal7$Value<-HighlightHorizontal6$Value-5
HighlightHorizontal8<-HighlightHorizontal7
HighlightHorizontal8$Value<-HighlightHorizontal7$Value-5


HighlightMeshHoriz<-HighlightMesh
HighlightMeshHoriz$Value<-HighlightMeshHoriz$Value-5
HighlightMeshHoriz2<-HighlightMeshHoriz
HighlightMeshHoriz2$Value<-HighlightMeshHoriz2$Value-5
HighlightMeshHoriz3<-HighlightMeshHoriz2
HighlightMeshHoriz3$Value<-HighlightMeshHoriz3$Value-5
HighlightMeshHoriz4<-HighlightMeshHoriz3
HighlightMeshHoriz4$Value<-HighlightMeshHoriz4$Value-5
HighlightMeshHoriz5<-HighlightMeshHoriz4
HighlightMeshHoriz5$Value<-HighlightMeshHoriz5$Value-5
HighlightMeshHoriz6<-HighlightMeshHoriz5
HighlightMeshHoriz6$Value<-HighlightMeshHoriz6$Value-5
HighlightMeshHoriz7<-HighlightMeshHoriz6
HighlightMeshHoriz7$Value<-HighlightMeshHoriz7$Value-5
HighlightMeshHoriz8<-HighlightMeshHoriz7
HighlightMeshHoriz8$Value<-HighlightMeshHoriz8$Value-5
HighlightMeshHoriz9<-HighlightMeshHoriz8
HighlightMeshHoriz9$Value<-HighlightMeshHoriz9$Value-5
HighlightMeshHoriz10<-HighlightMeshHoriz9
HighlightMeshHoriz10$Value<-HighlightMeshHoriz10$Value-5
HighlightMeshHoriz11<-HighlightMeshHoriz10
HighlightMeshHoriz11$Value<-HighlightMeshHoriz11$Value-5
HighlightMeshHoriz12<-HighlightMeshHoriz11
HighlightMeshHoriz12$Value<-HighlightMeshHoriz12$Value-5
HighlightMeshHoriz13<-HighlightMeshHoriz12
HighlightMeshHoriz13$Value<-HighlightMeshHoriz13$Value-5
HighlightMeshHoriz14<-HighlightMeshHoriz13
HighlightMeshHoriz14$Value<-HighlightMeshHoriz14$Value-5
HighlightMeshHoriz15<-HighlightMeshHoriz14
HighlightMeshHoriz15$Value<-HighlightMeshHoriz15$Value-5
HighlightMeshHoriz16<-HighlightMeshHoriz15
HighlightMeshHoriz16$Value<-HighlightMeshHoriz16$Value-5
HighlightMeshHoriz17<-HighlightMeshHoriz16
HighlightMeshHoriz17$Value<-HighlightMeshHoriz17$Value-5


ggplot(Example.Data, aes(x=Variable, y=Value, fill=Fill)) + theme_bw() + #facet_wrap(~Product, nrow=1)+ #Ensure theme_bw are there to create borders
theme(legend.position = "none")+
scale_fill_grey(start=.4)+
#scale_y_continuous(limits = c(0, 100), breaks = (seq(0,100,by = 10)))+
geom_bar(position=position_dodge(.9), stat="identity", colour="black", legend = FALSE)+
geom_bar(data=HighlightDataVert, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.80)+
geom_bar(data=HighlightDataVert, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.60)+
geom_bar(data=HighlightDataVert, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.40)+
geom_bar(data=HighlightDataVert, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.20)+
geom_bar(data=HighlightDataVert, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.0) +
geom_bar(data=HighlightHorizontal, position=position_dodge(.9), stat="identity", colour="black", size=.5)+
geom_bar(data=HighlightHorizontal2, position=position_dodge(.9), stat="identity", colour="black", size=.5)+
geom_bar(data=HighlightHorizontal3, position=position_dodge(.9), stat="identity", colour="black", size=.5)+
geom_bar(data=HighlightHorizontal4, position=position_dodge(.9), stat="identity", colour="black", size=.5)+
geom_bar(data=HighlightHorizontal5, position=position_dodge(.9), stat="identity", colour="black", size=.5)+
geom_bar(data=HighlightHorizontal6, position=position_dodge(.9), stat="identity", colour="black", size=.5)+
geom_bar(data=HighlightHorizontal7, position=position_dodge(.9), stat="identity", colour="black", size=.5)+
geom_bar(data=HighlightHorizontal8, position=position_dodge(.9), stat="identity", colour="black", size=.5)+
geom_bar(data=HighlightMesh, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.80)+
geom_bar(data=HighlightMesh, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.60)+
geom_bar(data=HighlightMesh, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.40)+
geom_bar(data=HighlightMesh, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.20)+
geom_bar(data=HighlightMesh, position=position_dodge(.9), stat="identity", colour="black", size=.5, width=0.0)+
geom_bar(data=HighlightMeshHoriz, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz2, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz3, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz4, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz5, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz6, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz7, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz8, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz9, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz10, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz11, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz12, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz13, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz14, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz15, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz16, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
geom_bar(data=HighlightMeshHoriz17, position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")

生产这个:

enter image description here

这不是超级漂亮,但它是唯一的解决方案,我可以考虑。

可以看到,我产生了一些非常基本的数据。为了得到垂直线,我只需要创建一个数据框架来包含我想要添加垂直线的变量,并且每次都多次重绘图形边框以减小宽度。

A similar thing is done for the horizontal lines but a new data frame is needed for each redraw where I have subtracted a value (in my example '5') from the value associated with the variable of interest. Effectively lowering the height of the bar. This is clunky to achieve and there may be more streamlined approaches but this illustrates how it can be achieved.

网格模式是两者的结合。首先绘制垂直线,然后添加水平线设置 fillfill='transparent',以确保垂直线不被绘制过来。

直到有一个模式更新,我希望你们中的一些人发现这个有用。

编辑2:

此外,还可以添加对角线模式。我在数据框中添加了一个额外的变量:

Example.Data[4,] <- c(20, 'Diagonal Pattern','Diagonal Pattern' )

Then I created a new data frame to hold coordinates for the diagonal lines:

Diag <- data.frame(
x = c(1,1,1.45,1.45), # 1st 2 values dictate starting point of line. 2nd 2 dictate width. Each whole = one background grid
y = c(0,0,20,20),
x2 = c(1.2,1.2,1.45,1.45), # 1st 2 values dictate starting point of line. 2nd 2 dictate width. Each whole = one background grid
y2 = c(0,0,11.5,11.5),# inner 2 values dictate height of horizontal line. Outer: vertical edge lines.
x3 = c(1.38,1.38,1.45,1.45), # 1st 2 values dictate starting point of line. 2nd 2 dictate width. Each whole = one background grid
y3 = c(0,0,3.5,3.5),# inner 2 values dictate height of horizontal line. Outer: vertical edge lines.
x4 = c(.8,.8,1.26,1.26), # 1st 2 values dictate starting point of line. 2nd 2 dictate width. Each whole = one background grid
y4 = c(0,0,20,20),# inner 2 values dictate height of horizontal line. Outer: vertical edge lines.
x5 = c(.6,.6,1.07,1.07), # 1st 2 values dictate starting point of line. 2nd 2 dictate width. Each whole = one background grid
y5 = c(0,0,20,20),# inner 2 values dictate height of horizontal line. Outer: vertical edge lines.
x6 = c(.555,.555,.88,.88), # 1st 2 values dictate starting point of line. 2nd 2 dictate width. Each whole = one background grid
y6 = c(6,6,20,20),# inner 2 values dictate height of horizontal line. Outer: vertical edge lines.
x7 = c(.555,.555,.72,.72), # 1st 2 values dictate starting point of line. 2nd 2 dictate width. Each whole = one background grid
y7 = c(13,13,20,20),# inner 2 values dictate height of horizontal line. Outer: vertical edge lines.
x8 = c(.8,.8,1.26,1.26), # 1st 2 values dictate starting point of line. 2nd 2 dictate width. Each whole = one background grid
y8 = c(0,0,20,20),# inner 2 values dictate height of horizontal line. Outer: vertical edge lines.
#Variable = "Diagonal Pattern",
Fill = "Diagonal Pattern"
)

然后,我将 geom _ path 添加到上面的 ggplot 中,每个 geom _ path 调用不同的坐标,并在所需的工具条上绘制线条:

+geom_path(data=Diag, aes(x=x, y=y),colour = "black")+  # calls co-or for sig. line & draws
geom_path(data=Diag, aes(x=x2, y=y2),colour = "black")+  # calls co-or for sig. line & draws
geom_path(data=Diag, aes(x=x3, y=y3),colour = "black")+
geom_path(data=Diag, aes(x=x4, y=y4),colour = "black")+
geom_path(data=Diag, aes(x=x5, y=y5),colour = "black")+
geom_path(data=Diag, aes(x=x6, y=y6),colour = "black")+
geom_path(data=Diag, aes(x=x7, y=y7),colour = "black")

其结果如下:

enter image description here

这是一个有点草率,因为我没有投入太多的时间在获得线完美的角度和间隔分开,但这应该作为一个概念的证明。

显然,线条可以向相反的方向倾斜,而且还有对角线啮合的空间,就像水平和垂直的啮合一样。

我想这就是我所能提供的所有图案前面。希望有人可以找到它的用途。

编辑3: 著名的遗言。我想出了另一个模式选项。这次使用 geom_jitter

我再次在数据框中添加了另一个变量:

Example.Data[5,] <- c(100, 'Bubble Pattern','Bubble Pattern' )

我按照我想要的样式排列:

Example.Data$Variable = Relevel(Example.Data$Variable, ref = c("Diagonal Pattern", "Bubble Pattern","Horizontal Pattern","Mesh Pattern","Vertical Pattern"))

接下来,我创建了一个列,其中包含与 x 轴上的目标栏相关联的数字:

Example.Data$Bubbles <- 2

后面跟着的列包含“气泡”在 y 轴上的位置:

Example.Data$Points <- c(5, 10, 15, 20, 25)
Example.Data$Points2 <- c(30, 35, 40, 45, 50)
Example.Data$Points3 <- c(55, 60, 65, 70, 75)
Example.Data$Points4 <- c(80, 85, 90, 95, 7)
Example.Data$Points5 <- c(14, 21, 28, 35, 42)
Example.Data$Points6 <- c(49, 56, 63, 71, 78)
Example.Data$Points7 <- c(84, 91, 98, 6, 12)

最后,我在上面的 ggplot 中添加了 geom_jitter,使用新的列来定位和重用“ Point”来改变“ Bubbles”的大小:

+geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points2, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points2, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points3, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points4, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points2, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points2, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points3, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points4, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points2, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points5, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points5, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points6, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points6, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points7, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points7, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points2, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points2, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points3, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points4, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points2, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points2, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points3, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points4, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points2, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points5, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points5, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points6, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points6, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points7, size=Points), alpha=.5)+
geom_jitter(data=Example.Data,aes(x=Bubbles, y=Points7, size=Points), alpha=.5)

每次运行图表的抖动位置的“泡沫”不同,但这里是一个更好的输出我有:

enter image description here

有时候,“泡沫”会在国界之外抖动。如果发生这种情况,则重新运行或仅导出较大的维度。可以在 y 轴的每个增量上绘制更多的气泡,如果您愿意,这将填充更多的空白空间。

这使得7种模式(如果你包括相反倾斜的对角线和两者的对角网格)可以在 ggplot 中被破解。

如果有人能想到一些,请随时提出更多的建议。

编辑4: 我一直在研究一个在 ggplot2中自动化孵化/模式的包装函式。一旦我扩展了这个函数,允许在 facet _ grid 图中使用模式,我就会发布一个链接。下面是一个简单的条形图输入函数的输出示例:

enter image description here

我将添加最后一个编辑后,我已经准备好共享的功能。

EDIT 5: 这里有个链接 to the function EggHatch that I wrote to make the process of adding patterns to geom_bar plots a little easier.

编辑6: 我想我会分享这个解决方案的一个简单的变体,以增加一些色彩的阴谋。

使用与上面相同的 df 运行这段代码:

bar_width = 0.8
xaxislabs <- c("Purple", "Blue", "Green")


ggplot(Example.Data, aes(x=Variable, y=Value, fill=Fill)) +
theme(legend.position = "none")+
geom_bar(position=position_dodge(.9), stat="identity", colour="black", legend = FALSE,  width=bar_width, fill="#15a742")+
geom_bar(data=Example.Data[2, ], position=position_dodge(.9), stat="identity", colour="#FFFFFF", width=(bar_width/7)*5, fill="#FFFFFF")+
geom_bar(data=Example.Data[2, ], position=position_dodge(.9), stat="identity", colour="#15a742", width=(bar_width/7)*3, fill="#15a742")+
geom_bar(data=Example.Data[2, ], position=position_dodge(.9), stat="identity", colour="#FFFFFF", width=(bar_width/7), fill="#FFFFFF")+
geom_bar(data=Example.Data[2, ], position=position_dodge(.9), stat="identity", colour="black", width=bar_width, fill="transparent")+
  

geom_bar(data=Example.Data[1, ], position=position_dodge(.9), stat="identity", colour="black", width=bar_width, fill="#8b2fbb")+
geom_bar(data=Example.Data[1, ], position=position_dodge(.9), stat="identity", colour="#FFFFFF", width=(bar_width/7)*5, fill="#FFFFFF")+
geom_bar(data=Example.Data[1, ], position=position_dodge(.9), stat="identity", colour="#8b2fbb", width=(bar_width/7)*3, fill="#8b2fbb")+
geom_bar(data=Example.Data[1, ], position=position_dodge(.9), stat="identity", colour="#FFFFFF", width=(bar_width/7), fill="#FFFFFF")+
geom_bar(data=Example.Data[1, ], position=position_dodge(.9), stat="identity", colour="black", width=bar_width, fill="transparent")+
  

  

geom_bar(data=Example.Data[3, ], position=position_dodge(.9), stat="identity", colour="#59a5db", width=bar_width, fill="#59a5db")+
geom_bar(data=Example.Data[3, ], position=position_dodge(.9), stat="identity", colour="#FFFFFF", width=(bar_width/7)*5, fill="#FFFFFF")+
geom_bar(data=Example.Data[3, ], position=position_dodge(.9), stat="identity", colour="#59a5db", width=(bar_width/7)*3, fill="#59a5db")+
geom_bar(data=Example.Data[3, ], position=position_dodge(.9), stat="identity", colour="#FFFFFF", width=(bar_width/7), fill="#FFFFFF")+
geom_bar(data=Example.Data[3, ], position=position_dodge(.9), stat="identity", colour="black", width=bar_width, fill="transparent")+
  

scale_x_discrete(labels= xaxislabs)

结果如下:

enter image description here

这段代码同样使用了上面的 dfs:

bar_width = 0.8
xaxislabs <- c("Purple", "Blue", "Green")




ggplot(Example.Data, aes(x=Variable, y=Value, fill=Fill)) +
theme(legend.position = "none")+
geom_bar(position=position_dodge(.9), stat="identity", colour="black", legend = FALSE,  width=bar_width, fill="#15a742")+
geom_bar(data=Example.Data[2, ], position=position_dodge(.9), stat="identity", colour="#FFFFFF", width=(bar_width/7)*5, fill="#FFFFFF")+
geom_bar(data=Example.Data[2, ], position=position_dodge(.9), stat="identity", colour="#15a742", width=(bar_width/7)*3, fill="#15a742")+
geom_bar(data=Example.Data[2, ], position=position_dodge(.9), stat="identity", colour="#FFFFFF", width=(bar_width/7), fill="#FFFFFF")+
geom_bar(data=Example.Data[2, ], position=position_dodge(.9), stat="identity", colour="black", width=bar_width, fill="transparent")+
  

geom_bar(data=Example.Data[1, ], position=position_dodge(.9), stat="identity", colour="#8b2fbb", size=.5, fill = "#8b2fbb")+
geom_bar(data=HighlightHorizontal, position=position_dodge(.9), stat="identity", colour="#FFFFFF", size=.5, fill = "#FFFFFF")+
geom_bar(data=HighlightHorizontal2, position=position_dodge(.9), stat="identity", colour="#8b2fbb", size=.5, fill="#8b2fbb")+
geom_bar(data=HighlightHorizontal3, position=position_dodge(.9), stat="identity", colour="#FFFFFF", size=.5, fill = "#FFFFFF")+
geom_bar(data=HighlightHorizontal4, position=position_dodge(.9), stat="identity", colour="#8b2fbb", size=.5, fill="#8b2fbb")+
geom_bar(data=HighlightHorizontal5, position=position_dodge(.9), stat="identity", colour="#FFFFFF", size=.5, fill = "#FFFFFF")+
geom_bar(data=HighlightHorizontal6, position=position_dodge(.9), stat="identity", colour="#8b2fbb", size=.5, fill="#8b2fbb")+
geom_bar(data=HighlightHorizontal7, position=position_dodge(.9), stat="identity", colour="#FFFFFF", size=.5, fill = "#FFFFFF")+
geom_bar(data=HighlightHorizontal8, position=position_dodge(.9), stat="identity", colour="#8b2fbb", size=.5, fill="#8b2fbb")+
geom_bar(data=Example.Data[1, ], position=position_dodge(.9), stat="identity", colour="black", size=.5, fill = "transparent")+
  

geom_bar(data=Example.Data[3, ], position=position_dodge(.9), stat="identity", colour="black", width=bar_width, fill="#59a5db")+
  

scale_x_discrete(labels= xaxislabs)

结果是:

enter image description here

我认为 医学博士的工作是伟大的,但现在我突然谷歌一个特殊的包——-模式图。没有看到内部代码,但 Vignette 似乎有用。

It might be useful to create a dummy data frame whose contours correspond to "textures" and then use geom_contour. Here is my example:

library(ggplot2)


eg = expand.grid(R1 = seq(0,1,by=0.01), R2 = seq(0,1,by=0.01))
eg$importance = (eg$R1+eg$R2)/2


ggplot(eg , aes(x = R1, y = R2)) +
geom_raster(aes(fill = importance), interpolate=TRUE) +
scale_fill_gradient2(low="white", high="gray20", limits=c(0,1)) +
theme_classic()+
geom_contour(bins=5,aes(z=importance), color="black", size=0.6)+
coord_fixed(ratio = 1, xlim=c(0,1),ylim=c(0,1))

这就是结果: 有线条的阴影图

(线条应该平滑)

你可以通过 @claus wilke使用 翻译: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校对: 奇芳校包来绘制带有纹理的矩形和条形。

# Image/pattern randomly selected from README
path_image <- "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/rocks2-256.jpg"


library(ggplot2)
# devtools::install_github("clauswilke/ggtextures")
ggplot(mtcars, aes(cyl, mpg)) +
ggtextures::geom_textured_bar(stat = "identity", image = path_image)

enter image description here

你也可以把它和其他宝石结合起来:

data_raw <- data.frame(x = round(rbinom(1000, 50, 0.1)))
ggplot(data_raw, aes(x)) +
geom_textured_bar(
aes(y = ..prop..), image = path_image
) +
geom_density()

enter image description here

可能感兴趣的是 ggrough: https://xvrdm.github.io/ggrough/

我刚刚发现了一个名为 ggpattern(https://github.com/coolbutuseless/ggpattern)的包,它似乎是解决这个问题的很好的解决方案,并且与 ggplot2工作流很好地集成在一起。虽然使用纹理的解决方案可能对对角线条很有效,但它们不会产生矢量图形,因此不是最佳的。

下面是一个直接来自 ggpattern 的 github 存储库的示例:

install.packages("remotes")
remotes::install_github("coolbutuseless/ggpattern")


library(ggplot2)
library(ggpattern)


df <- data.frame(level = c("a", "b", "c", 'd'), outcome = c(2.3, 1.9, 3.2, 1))


ggplot(df) +
geom_col_pattern(
aes(level, outcome, pattern_fill = level),
pattern = 'stripe',
fill    = 'white',
colour  = 'black'
) +
theme_bw(18) +
theme(legend.position = 'none') +
labs(
title    = "ggpattern::geom_pattern_col()",
subtitle = "pattern = 'stripe'"
) +
coord_fixed(ratio = 1/2)


结果导致了这个阴谋:

ggpattern example plot

如果只有一些条纹,geom_col_pattern()有一个 pattern_alpha参数,可以用来使某些不需要的条纹完全透明。