2016-05-15 81 views
1

我在R中使用ggplot包來繪製一個簡單的點圖,xlab上的「Lecture」數字和ylab上的「音樂周」。R ggplot xlab編號

這裏是簡單的數據集,我想要繪製:

Dataset - Music2

這裏是我使用的代碼:

ggplot(music2, aes(Lecture, Music)) + geom_point() + xlab("Lecture") +  ylab("Music Week") 

這裏的情節,我得到:

point plot of dataset-music2

如以上在繪圖,繪圖的xlab不會像數據集中那樣進入數字序列。 11號和12號在1和2之間跳躍,而不是保持在10以下。

如何將序列更改爲自然順序?

+0

不要將您的數據作爲圖像發佈,請學習如何給出[可重現的示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610 )。也就是說,你的'Lecture'列很可能被存儲爲一個字符變量(檢查與'Music'列對齊的區別)。檢查類:'class(dataset $ Lecture)'。使用'dataset $ Lecture < - as.numeric(數據集$講座)',您可以將其轉換爲數字變量。如果它作爲因子變量存儲,則使用'as.numeric(as.character(dataset $講座))'。之後,你的繪圖代碼應該可以正常工作。 – Jaap

+0

感謝您的建議!下次我會改善。 –

回答

0

你要轉換變量在x軸的一個因素,或者至少不應該,因爲在這種情況下,R期望猜測是字符類型的字符的自然順序應該是什麼

library(ggplot2) 

# Example data. Replace with yours 
data <- data.frame(a = 1:12, b = sample(c(0, 1))) 

ggplot(data, aes(x = as.factor(a), y = b)) + 
geom_point() + 
labs(x = "Label x", y = "Label y") 
+0

非常感謝!有用! –