2010-05-24 7 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-graph- system/26110400 # 26110400) – baptiste

답변

26

ggplot은 컬러 제작자 팔레트를 사용할 수 있습니다. 이 중 일부는 친절한 "사진 복사"기능을 제공합니다. 그래서 이런 게 뭔가 당신을 위해 일할 것입니까? http://colorbrewer2.org/

복사 친절한 :이 경우 OrRd에

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

는 ColorBrewer를 웹 페이지에서 볼 수있는 팔레트입니다 이것은 주어진 색상이 흑백 복사를 견딜 수 을 나타냅니다. 전환 계획은 을 복사 할 수 없습니다. 밝기의 차이는 순차 구성으로 보존 된 이어야합니다.

51

그리드 (ggplot2가 실제 그림을 그리기 위해 사용하는 그래픽 시스템)가 텍스처를 지원하지 않기 때문에 현재로서는 불가능합니다. 죄송합니다!

+38

이 기능을 사용하려면 gridextra를 종속성으로 추가 할 계획이 있습니까? – russellpierce

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')을 뺀 각 다시 그리기에 새 데이터 프레임이 필요합니다. 막대의 높이를 효과적으로 낮 춥니 다. 달성하기가 어렵고 더 효율적인 접근법이있을 수 있지만 이것이 어떻게 달성 될 수 있는지를 보여줍니다.

메쉬 패턴은이 둘의 조합입니다. 먼저 수직선을 그리고 나서 수평선이 그려지지 않도록 fillfill='transparent'으로 설정하는 수평선을 추가하십시오.

패턴 업데이트가있을 때까지는 여러분 중 일부가 유용하다고 생각하기를 바랍니다.

편집 2 :

또한 대각선 패턴도 추가 될 수 있습니다. I는 각각 위 ggplot에 geom_paths 추가 거기서

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

을 :

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

그럼 I는 대각선 좌표를 유지하기 위해 새로운 데이터 프레임을 생성 : I는 상기 데이터 프레임에 추가적인 변수를 부가 다른 하나의 좌표를 호출하여 원하는 바 위에 선을 그림이 결과는 다음

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

:

,536,913 나는 선이 완벽하게 기울어지고 너무 많은 시간을 투자하고 이격 그러나이 개념의 증거로 봉사해야하지 않았다으로 63,210

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 

conta "버블"의 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) 

마지막 I는 크기를 변화시키는 "포인트"- 사용하고 위치와위한 새로운 컬럼을 사용하여 위 ggplot에 geom_jitter들 추가 ' 거품 '

+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

,691,363 여기 다른 '거품을하지만이 내가 가진 더 좋은 출력 중 하나입니다210

때로는 '거품'이 테두리 외부에서 지터가납니다. 이 문제가 다시 발생하거나 더 큰 차원으로 단순히 내보내는 경우 원하는 경우 빈 공간을 더 채울 y 축의 각 증분에 더 많은 거품을 그릴 수 있습니다.

ggplot에서 해킹 당할 수있는 최대 7 개의 패턴 (대각선과 대각선이 반대쪽에있는 대각선을 포함하는 경우)을 만듭니다.

누군가가 생각하면 더 많이 제안 해주십시오.

EDIT 4 : ggplot2에서 해칭/패턴을 자동화하는 래퍼 함수로 작업 해 왔습니다.

enter image description here

나는 거 : 나는 facet_grid 플롯 등이 여기에 예로 바의 간단한 줄거리에 대한 함수 입력과 출력의 패턴을 수 있도록 기능을 확장 일단 내가 링크를 게시합니다 마지막으로 한 번 편집을 추가하면 공유 할 수있는 기능이 생깁니다.

EDIT 5 : Here's a link에 geom_bar 플롯에 패턴을 추가하는 과정을 좀 더 쉽게하기 위해 작성한 EggHatch 함수를 사용하면됩니다.

관련 문제