How to add texture to fill flowers in ggplot2

I am currently using scale_brewer() to fill, and they look beautiful in color (on screen and through a color printer), but they print relatively evenly, like gray, when using a black and white printer. I was looking for ggplot2 online documentation but saw nothing about adding textures to fill colors. Is there an official ggplot2 way to do this, or does anyone have a hack that they use? By textures, I mean things like diagonal stripes, reverse diagonal stripes, dot patterns, etc., which will distinguish the fill colors when printing in black and white.

+81
r ggplot2
May 24 '10 at 7:07
source share
7 answers

ggplot can use colorbrewer palettes. Some of them are “photocopied”. So something like this will work for you?

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

in this case, OrRd is the palette found on the colorbrewer web page: http://colorbrewer2.org/

Photocopy Friendly: This means that the color scheme will withstand a black and white photocopy. Cannot photocopy discrepancies. Differences in lightness should be maintained with consistent circuits.

+34
Jun 10 2018-10-10T00:
source share

Hey guys, here is a tiny hack that solves the texture problem very simply:

ggplot2: make the border on one bar darker than the others using R

EDIT: I finally took the time to give a brief example of this hack that allows at least 3 types of basic template 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") 

Produces the following:

enter image description here

It is not super beautiful, but it is the only solution I can think of.

Apparently, I create very simple data. To get the vertical lines, I simply create a data frame to contain the variable to which I wanted to add vertical lines, and reduced the graph borders several times, decreasing the width each time.

A similar task is performed for horizontal lines, but each redraw requires a new data frame, where I subtracted the value (in my example "5") from the value associated with the variable of interest. Effective barbell height reduction. This is inconvenient to achieve, and there may be more optimized approaches, but it illustrates how this can be achieved.

A grid cell is a combination of both. First draw the vertical lines and then add the horizontal lines, fill as fill='transparent' to ensure that the vertical lines are not drawn.

Until a template update appears, I hope some of you find this useful.

EDIT 2:

Additionally diagonal patterns can be added. I added an additional variable to the data frame:

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

Then I created a new data frame for storing coordinates for 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" ) 

From there, I added geom_paths to ggplot above, each of which calls a different coordinate and draws lines above the required strip:

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

This leads to the following:

enter image description here

This is a bit messy, because I did not invest too much time so that the lines were perfectly angular and spaced from each other, but this should serve as a proof of concept.

Obviously, the lines can lean in the opposite direction, and there is also room for a diagonal grid, like a horizontal and vertical grid.

I think about everything that I can offer at the front of the picture. Hope someone finds a use for this.

EDIT 3: Famous last words. I came up with another version of the template. This time using geom_jitter .

Again, I added another variable to the data frame:

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

And I ordered how I wanted each sample to be submitted:

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

Then I created a column that should contain the number associated with the target target row on the x axis:

 Example.Data$Bubbles <- 2 

The columns then contain the y-axis positions of the bubbles:

 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) 

Finally, I added geom_jitter to ggplot above, using the new columns to position and reuse “Points” to resize the “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) 

Each time the chart starts, the jitter positions the bubbles differently, but here is one of the best results that I had:

enter image description here

Sometimes "bubbles" tremble beyond borders. If it is repeated or simply exported in large sizes. On each increment along the y axis, more bubbles can be displayed that will fill more space if you wish.

This amounts to up to 7 patterns (if you include the opposite oblique diagonal lines and the diagonal grid of both), which can be cracked into ggplot.

Please feel free to offer more if anyone can think of some.

EDIT 4: I was working on a wrapper function to automate hatching / patterns in ggplot2. I will send the link as soon as I have expanded this function to allow templates in facet_grid plots, etc. Here's the output with an input function for a simple bar chart as an example:

enter image description here

I will add one last edit when I have a function that you are willing to share.

EDIT 5: Here's a link to the EggHatch function that I wrote to make the process of adding templates to geom_bar charts a little easier.

+78
Dec 06 '13 at 14:35
source share

This is currently not possible because the grid (the graphics system that ggplot2 uses for actual drawing) does not support textures. Sorry!

+65
May 25 '10 at
source share

You can use the ggtextures package from @claus wilke to draw textured rectangles and stripes with ggplot2 .

 # 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

You can also combine it with other gems:

 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

+5
Jun 11 '19 at 13:38 on
source share

I think Docconcoct is working fine, but now I suddenly switched to a special package --- Patternplot . I have not seen the internal code, but the vignette seems useful.

+2
Jul 17 '18 at 8:34
source share

It may be useful to create a dummy data frame whose outlines 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)) 

And here is the result: a shaded plot with lines

(lines should be smoothed)

+1
Jan 24 '19 at 12:50
source share

ggrough might be interesting: https://xvrdm.imtqy.com/ggrough/

0
Jun 24. '19 at 18:36
source share



All Articles