1
我有一些數據,我正在嘗試使用ggplot2進行圖形化,其中x軸是數值/整數值。繪製圖形時,我希望圖形只顯示數據集中存在的x的值,而不會將值添加到x軸(離散值)。下面的完全可重現的示例演示了這個問題:即使提供的x軸值爲1,3,25,生成的圖形在x軸上呈現0,5,15,20,25。我嘗試過鑄造這些價值觀,以及嘗試一種獨立的規模,但沒有一個似乎有效。ggplot2 - 來自數值/整數數據的離散X軸值
編輯儘管x軸上的值是數字/整數,但它們代表因素(即試驗中的人數,發動機中的汽缸數等)並且不是連續值。
#Example
library(ggplot2)
row1 <- c(1, 1)
row2 <- c(3, 2)
row3 <- c(25, 10)
data <- data.frame()
data <- rbind(data, row1)
data <- rbind(data, row2)
data <- rbind(data, row3)
names(data) <- c("A", "B")
qplot(A, B, data = data, geom="line")
#Things Tried
qplot(factor(A), B, data = data, geom="line") #geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
qplot(as.factor(A), B, data = data, geom="line") #geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
qplot(character(A), B, data = data, geom="line") #Error in character(A) : invalid 'length' argument
qplot(as.character(A), B, data = data, geom="line") #geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
qplot(A, B, data = data, geom="line") + scale_x_discrete(breaks = data$A) #Works, but values are too far apart
感謝您的回覆。這個解決方案效果很好,但它可以使用qplot函數來完成,而不是使用ggplot2函數嗎? qplot創建更清晰的腳本,所以我儘量堅持下去。此外,重新:誤導性表述:請參閱我的原始問題中的編輯。 – lolcodez 2014-12-02 23:47:04
@lolcodez瞭解,但數字暗示數字關係。我會列出他們作爲審判1,審判2,審判25等...或類似的東西。我已將qplot代碼添加到我的解決方案中。 – 2014-12-02 23:52:03