2015-02-01 2 views
2

ggplot의 다른 위치에 틱과 레이블을 만드는 방법을 찾고 있습니다.ggplot 축 틱과 레이블을 개별적으로 표시합니다.

샘플 코드이 그림에서

#load libraries 
library(ggplot2) 
library(reshape2) 

#create data 
df <-data.frame(A=1:6,B=c(0.6,0.5,0.4,0.2,0.3,0.8),C=c(0.4,0.5,0.6,0.8,0.7,0.2),D=c("cat1","cat1","cat1","cat2","cat2","cat2")) 
df 
df1 <- melt(df,measure.vars=c("B","C")) 

#plot 
p <- ggplot()+ 
    geom_bar(data=df1,aes(x=A,y=value,fill=variable),stat="identity")+ 
    theme(axis.title=element_blank(),legend.position="none") 
print(p) 

enter image description here

는 기본 (휴식에 의해 정의) 같은 위치에있는 진드기와 라벨이 있습니다. 그리고 테마 때문에 X 축 선이 모두 누락되었습니다.

대신에, 나는이 위치이 위치

lpoint <- data.frame(pos=c(2,5),lab=c("cat1","cat2")) 

그리고 부분의 x 축 라인이나 함께 아래와 같은 결국 그림 뭔가

tpoint <- c(1,3,4,6) 

및 라벨에 틱을하고 싶습니다 전체 x 축 라인 :

enter image description here

이 자리

p1 <- p + scale_x_discrete(breaks=lpoint$pos,labels=lpoint$lab) 

내 레이블을두고 그러나 진드기가 잘못된 장소에 여러 스케일은 불가능?

+1

: P2 <- P + facet_wrap를 (~ D = "free_x을"확장) – Mahrtynas

답변

3

내가 원하는 출력에 올 수있는 가장 가까운이 있습니다 :

dfannotate <- data.frame(x = c(2, 5), xmin = c(1, 4), xmax = c(3, 6), y = -.01, height=.02) 
dfbreaks = data.frame(lim = 1:6, lab = c('', 'cat1', '', '', 'cat2', '')) 

p + geom_errorbarh(data = dfannotate, aes(x, y, xmin=xmin, xmax=xmax, height=height)) + 
    scale_x_discrete(limits=dfbreaks$lim, labels=dfbreaks$lab) + 
    scale_y_continuous(expand = c(0, 0), limits=c(-0.02, 1.02)) + 
    theme(axis.ticks.x = element_line(linetype=0)) 
당신은 아주 유사한 출력을 얻을 수 facet_wrap을 사용할 수 있습니다
관련 문제