2014-06-19 30 views
0

我繪製了連續變量(窗口)上的某些值(y),但我想將x軸上的值更改爲值類變量(chr)它作爲一個組變量(對於許多'窗口'值是'chr'值)。與ggplot中使用的X變量不同的X座標

這是我的代碼數據的例子:

chr<-c(1,1,1,1,1,2,2,2,2,2) 
window<-c(1,2,3,4,5,6,7,8,9,10) 
y<-c(2,0.5,3,2.5,2,0.2,0.3,0.7,0.3,6) 

data <-data.frame(as.character(chr),window,y) 

ggplot(data,aes(window,y),coulour=chr)+ geom_point(color=data$chr) 

我要繪製的「Y」值agains的「窗口」的價值觀,但我不希望看到的「窗口」值在我的x軸上 - 我想看到一個(01)'chr'值代表所有給定'chr'的'窗口'值。例如,如果你運行上面的代碼,你會看到值「2.5」,「5.0」,「7.5」和「10.0」,但是,我希望看到一個「1」從「5.0」到「10.0」的「y」值和從「5.0」到「5.0」的那些「y」值的一個「2」。

任何想法?謝謝!

+0

現在我想起來了,這可能是一個[複製](http://stackoverflow.com/questions/5096538/customizing-the-axis-標籤功能於ggplot) –

回答

0

使用scale_x_discrete

chr<-c(1,1,1,1,1,2,2,2,2,2) 
window<-c(1,2,3,4,5,6,7,8,9,10) 
y<-c(2,0.5,3,2.5,2,0.2,0.3,0.7,0.3,6) 

data <-data.frame(chr,window,y) 
library(ggplot2) 
ggplot(data,aes(window,y),coulour=chr)+ geom_point(color=data$chr) + 
scale_x_discrete(breaks = 1:10, labels=chr) 

enter image description here