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

我目前使用scale_brewer进行填充,这些颜色看起来非常漂亮(在屏幕上和通过彩色打印机),但是在使用黑白打印机时会相当均匀地以灰色打印。 我search了在线ggplot2文档,但没有看到任何添加纹理来填充颜色。 有没有官方的ggplot2方式来做到这一点,或有没有人有他们使用的黑客? 纹理我的意思是像斜杠,反斜杠,圆点图案等,这些将在黑白打印时区分填充颜色。

ggplot可以用户colorbrewery调色板。 有些是“复印”友好的。 所以玛贝这样的事情会为你工作?

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

在这种情况下OrRd是在colorbrewer网页上find的调色板: http ://colorbrewer2.org/

复印友好:这表明一个给定的配色scheme将承受黑白复印。 发行计划不能成功复印。 顺序scheme应该保持亮度差异。

嘿伙计这里是一个非常基本的方式解决纹理问题的一个小窍门:

ggplot2:使一个栏上的边框比使用R的其他栏更暗

编辑:我终于find时间给这个简短的例子,允许在ggplot2中至less3种基本模式。 代码:

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

产生这个:

在这里输入图像描述

这不是超级漂亮,但它是我能想到的唯一解决scheme。

可以看出我产生了一些非常基本的数据。 为了得到垂直线,我简单地创build一个数据框来包含我想要添加垂直线的variables,并多次重绘graphics边框,以减小每次的宽度。

对于水平线也做了类似的事情,但是每次重新绘制都需要一个新的dataframe,我从与感兴趣的variables相关的值中减去一个值(在我的例子中是'5')。 有效地降低酒吧的高度。 这是很难实现的,可能会有更简化的方法,但是这说明了如何实现。

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

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

编辑2:

另外还可以添加对angular线图案。 我在数据框中添加了一个额外的variables:

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

然后,我创build了一个新的数据框来保存对angular线的坐标:

 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_paths到上面的ggplot,每个调用不同的坐标,并在所需的栏上画线:

 +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") 

这导致以下结果:

在这里输入图像描述

这是有点马虎,因为我没有花太多的时间来获得完美的angular度和间距的线条,但这应该作为一个概念的certificate。

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

我认为这就是我所能提供的模式方面的一切。 希望有人可以find它的用途。

编辑3:着名的遗言。 我已经提出了另一种模式选项。 这一次使用geom_jitter

我再次添加了一个variables到数据框中:

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

接下来,我创build了一个列,以包含与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) 

最后,我将geom_jitter添加到上面的ggplot中,使用新的列来定位和重新使用“Points”来改变“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) 

每次剧情运行的抖动定位不同的气泡,但这是我有更好的输出之一:

在这里输入图像描述

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

这可以构成7种模式(如果你包括两个相反的倾斜对angular线和对angular线网格),可以在ggplot中进行攻击。

如果有人能想到一些,请随时提出build议。

编辑4:我一直在使用包装函数来自动化ggplot2中的阴影/图案。 我将发布一个链接,当我已经扩大了function,允许在facet_grid图等模式。这里是一个输出与functioninput为一个简单的酒吧作为一个例子:

在这里输入图像描述

我将添加一个最后的编辑,我有function准备分享。

目前不可能,因为网格(ggplot2用于执行实际绘图的graphics系统)不支持纹理。 抱歉!