2010-05-24 84 views
57

我目前使用scale_brewer()進行填充,這些顏色看起來非常漂亮(在屏幕上和通過彩色打印機),但在使用黑白打印機時可以相當均勻地以灰色打印。我搜索了在線ggplot2文檔,但沒有看到任何有關添加紋理來填充顏色的信息。有沒有官方ggplot2的方式來做到這一點,或有沒有人有他們使用的黑客?通過紋理我的意思是像黑色和白色打印時區分填充顏色的對角線條,反向對角線條,點圖案等。如何添加紋理來填充ggplot2中的顏色?

+0

[底層網格問題的gridSVG解決方案](http://stackoverflow.com/questions/26110160/how-to-apply-cross-hatching-to-a-polygon-using-the-grid-graphical-系統/ 26110400#26110400) – baptiste 2014-10-06 10:59:19

回答

26

ggplot可以使用colorbrewer調色板。其中一些是「影印」友好的。所以瑪貝這樣的事情會對你有用嗎?

ggplot(diamonds, aes(x=cut, y=price, group=cut))+ 
geom_boxplot(aes(fill=cut))+scale_fill_brewer(palette="OrRd") 
在這種情況下OrRd

是在網頁在ColorBrewer發現的調色板:http://colorbrewer2.org/

影印友好:這表明 給定的顏色方案將 承受黑白影印 。發散計劃 不能成功複印。 亮度差異應該是 保留序列計劃。

51

目前不可能,因爲網格(ggplot2用於執行實際繪圖的圖形系統)不支持紋理。抱歉!

+38

是否有計劃將gridextra作爲依賴添加以獲得此功能? – russellpierce 2013-02-03 02:44:11

62

嘿人這裏有一個凌晨黑客工具,在一個非常基本的方式解決了質地問題:

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

編輯:我終於找到時間給這個技巧,可以讓一個簡單的例子ggplot2中至少有3種基本模式。代碼:

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

它不是超級靚,但它是我能想到的唯一解決方案。

可以看出我產生了一些非常基本的數據。爲了獲得垂直線,我只需創建一個數據框來包含我想要添加垂直線的變量,並多次重繪圖形邊框,從而每次減小寬度。

對於水平線也做了類似的事情,但是每次重繪都需要一個新的數據幀,我從與感興趣的變量相關的值中減去了一個值(在我的示例中爲'5')。有效降低酒吧的高度。這很難實現,可能會有更簡化的方法,但這說明了如何實現。

網格模式是兩者的組合。首先繪製垂直線,然後將fill的水平線設置爲fill='transparent',以確保垂直線不會被拉伸。

直到有一個模式更新,我希望你們有些人覺得這很有用。

編輯2:也可以添加

此外對角線圖案。我添加一個額外的變量,以該數據幀:

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

然後創建了一個新的數據幀,以保持座標爲對角線:

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

這導致以下:

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) 

最後我添加geom_jitter S到ggplot以上使用新的列用於定位和重新使用「點」以改變的大小的'泡泡:

+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

我會添加一個最後一次編輯,我有功能可以共享。

編輯5:Here's a link到函數EggHatch,我寫了使向geom_bar圖添加模式的過程更容易一些。