2012-01-19 10 views
3

我正在尋找一些幫助創建與angeled x軸刻度標籤的多面的情節,這可能是由下面的例子最好的解釋:正確的垂直對準方位ggplot蜱數字

require(ggplot2) 
df <- data.frame(group=factor(c('sex','sex','race','race')), variable=c('Female','Male','White','African American'), value=1:4) 
p <- ggplot(aes(x=variable, y=value), data=df) 
p <- p + geom_line() 
p <- p + facet_grid(. ~ group, scale="free") 
p <- p + opts(axis.text.x=theme_text(angle=45,hjust=1,vjust=1)) 
ggsave(p, file='faceted.pdf', width=6, height=4) 

產生該圖,其中右邊的x蜱錯位:

misaligned x-axis ticks

似乎問題在facet_grid,我使用scale="free"時引入的與變化的刻度標籤長度有關。

任何建議,非常感謝。

回答

3

看起來像一個bug:https://github.com/hadley/ggplot2/issues/221顯然已在ggplot2 0.9及更高版本中修復。

> help(package='ggplot2') 
       Information on package ‘ggplot2’  
Description: 
Package:   ggplot2 
Type:    Package 
Title:    An implementation of the Grammar of Graphics 
Version:   0.8.9 

Bug的重現性(太糟糕了,對我來說!):

qplot(reorder(model, hwy), hwy, data=mpg) + 
    facet_grid(. ~ manufacturer, scales="free") + 
    opts(axis.text.x = theme_text(angle=90)) 
2

如果你真的得短短的X標籤 - 誰需要蜱伴侶嗎?使用axis.ticks來抑制它們並消除水平/垂直對齊。

require(ggplot2) 
df <- data.frame(group=factor(c('sex','sex','race','race')), 
       variable=c('Female','Male','White','African American'), 
       value=1:4) 
p <- ggplot(df,aes(x=variable, y=value)) + geom_line() 
p <- p + facet_grid(. ~ group, scale="free") 
p <- p + opts(axis.text.x=theme_text(angle=45), 
       axis.ticks = theme_blank(),axis.title.y=theme_blank()) 
ggsave(p, file='no_ticks.png', width=6, height=4) 

enter image description here

+0

蜱是指導時,有更多的因素眼睛有益的,但是這是一個很好的解決方法。謝謝。 – jhofman