2014-06-07 4 views
1

사이 눈금 I은 X에서 그리드 선을 설정하는 방법의 값ggplot2 이산 값

library(reshape2) 

ggplot(data=tips, aes(x=time, y=total_bill, fill=sex)) + 
    geom_bar(stat="identity", position=position_dodge()) 

enter image description here

의 중심 눈금 값에 눈금 선을 제공 ggplot2 이산 값을 사용하면 (즉, '저녁'과 '점심'사이에)

panel.grid.minor.x 나는이 분리되어 작동하지 않는다는 점을 생각했는데 ... 생각했습니다. 이것은 작은 값이 아닙니다 거트 라인을 그릴 때.

ggplot(data=tips, aes(x=time, y=total_bill, fill=sex)) + 
    geom_bar(stat="identity", position=position_dodge()) + 
    theme(panel.grid.minor.x = element_line()) 

답변

4

다음과 같이 그리드 선 역할을하는 수직선을 추가 할 수 있습니다 : 당신은 물론, 등 라인 폭, 색상, 스타일을 변경할 필요 수

geom_vline(xintercept=1.5, colour='white') 

을하고, 그리드 선으로 구분해야하는 여러 그룹의 막대가있는 경우 적절한 위치에 여러 줄을 추가하십시오. 답변에 대한

set.seed(1) 
dat = data.frame(total_bill=rnorm(100,80,10), 
       sex=rep(c("Male","Female"),50), 
       time=sample(c("Breakfast","Brunch","Lunch","Afternoon Tea","Dinner"), 
          100, replace=TRUE)) 
dat$time = factor(dat$time, 
        levels=c("Breakfast","Brunch","Lunch","Afternoon Tea","Dinner"), 
        ordered=TRUE) 

ggplot(data=dat, aes(x=time, y=total_bill, fill=sex)) + 
    geom_bar(stat="identity", position=position_dodge()) + 
    geom_vline(xintercept=seq(1.5, length(unique(dat$time))-0.5, 1), 
      lwd=1, colour="black") 

enter image description here

+0

감사합니다 : 예를 들어, 일부 가짜 데이터를 사용하여. 실제로'scales = 'free_x'와 함께 'facet_grid'를 사용하고 있습니다. 그래서 'length'를 사용하면 문제가되는 것처럼 보입니다. 나는 모양을했지만 캔트는 각면에서 진드기의 수를 얻는 방법을 찾는다. –

+0

무료 척도가 숫자이거나 정렬 된 요소 인 경우,'length (unique (dat $ var_name)) '대신'max (dat $ var_name)'을 사용할 수 있습니다. 그게 도움이 되나요, 아니면 그보다 더 복잡한 상황입니까? – eipi10