2017-08-08 48 views
-1

我正在繪製一些數據,並希望在我的陰謀的x軸刻度線。我的數據是這樣的:滴答不出現在ggplot

publication labels percentage 
1 foo   0  .4572 
2 foo   1  .0341 
3 foo   2  .09478 
4 foo   3  .0135 
5 bar   0  .7442 
6 bar   1  .2847 

,其中每個名字有從0標籤9.

我的代碼如下所示:

ggplot(aes(y = percentage, x = labels, color = publication), data = labelsdf)+ 
    geom_point(size = 3)+ 
    scale_x_discrete(breaks = c(0,1,2,3,4,5,6,7,8,9), 
        labels = c('1','2','3','4','5','6','7','8','9','10')) 

但我圖的樣子:

enter image description here

沒有breaks沒有任何刻度標記o r labels指定。爲什麼不出現我的蜱蟲?

+2

這是不可能回答這個問題沒有表現出我們是什麼數據看上去像個即一種可能性是所有的實際數據都落在兩個指定的刻度線之間。 –

+0

@snapcrack您使用的是哪個版本的'ggplot2'? – Prradep

+0

@Praderade 2.2.1.9000。對於Remko,我添加了一些數據;我沒想過要添加它,因爲它有直接的離散值,但希望這有助於。 – snapcrack

回答

1

在使用scale_x_discrete明確提及labels作爲離散值之後,需要使用factor()作爲離散值,否則值仍然是數值。

ggplot(aes(y = percentage, x = factor(labels), color = publication), data = df)+ 
    geom_point(size = 3)+ 
    scale_x_discrete(breaks = c(0,1,2,3,4,5,6,7,8,9), 
        labels = c('1','2','3','4','5','6','7','8','9','10')) 

enter image description here

使用後,您可能希望更改軸標籤按照您的要求。


由於labels已經是離散的,不要求使用scale_x_discretebreaks

ggplot(aes(y = percentage, x = labels, color = publication), data = df)+ 
    geom_point(size = 3) 

enter image description here

如果你想在X軸標籤不同於什麼存在於數據框,你可以得到使用小調整爲(即先從1代替0):

ggplot(aes(y = percentage, x = labels+1, color = publication), data = df)+ 
    geom_point(size = 3) 

enter image description here

+1

因素起作用。我還應該補充一點,我想出了使用「scale_x_continuous」的作品,同時沒有考慮變量。不應用「scale_x_discrete」的問題是,我最終得到了無用的刻度,如2.5,5.5等。 – snapcrack