2012-08-27 35 views
4

當用ggplot2繪製連續數據時,有沒有辦法在軸上包含字符值?我有刪減的數據,如:R中連續軸上的字符值ggplot2

x y Freq 
1 -3 16 3 
2 -2 12 4 
3 0 10 6 
4 2 7 7 
5 2 4 3 

最後一行數據是右刪失。我的代碼繪製此之下產生以下情節:

a1 = data.frame(x=c(-3,-2,0,2,2), y=c(16,12,10,7,4), Freq=c(3,4,6,7,3)) 
fit = ggplot(a1, aes(x,y)) + geom_text(aes(label=Freq), size=5)+ 
    theme_bw() + 
    scale_x_continuous(breaks = seq(min(a1$x)-1,max(a1$x)+1,by=1), 
        labels = seq(min(a1$x)-1,max(a1$x)+1,by=1), 
        limits = c(min(a1$x)-1,max(a1$x)+1))+ 
    scale_y_continuous(breaks = seq(min(a1$y),max(a1$y),by=2)) 

enter image description here

3點(2,4)是對的審查。我希望他們被繪製成右邊一個單位,相應的x軸刻度標記'> = 2'而不是3。任何想法如果這是可能的?

+2

你試過就只有X軸抖動加入? 'geom_text(aes(label = Freq),size = 5,position = position_jitter(w = 0.1,h = 0))' –

回答

4

這很有可能。我砍死了數據,所以2,4它是3,4。然後,我修改了您的標籤,只要它們的長度與休息時間相同即可。

ggplot(a1, aes(x,y)) + geom_text(aes(label=Freq), size=5)+ 
theme_bw() + 
scale_x_continuous(breaks = seq(min(a1$x)-1,max(a1$x),by=1), 
        labels = c(seq(min(a1$x)-1,max(a1$x)-1,by=1), ">=2"), 
        limits = c(min(a1$x)-1,max(a1$x)))+ 
scale_y_continuous(breaks = seq(min(a1$y),max(a1$y),by=2)) 

enter image description here

+0

太棒了!一旦你看到解決方案,問題總是很簡單。 – Glen