如何在 R 中使用 ggplot2 設定 Y 軸刻度標記?
使用 ggplot2 的 Y 軸刻度標記的預設值由 R 使用所提供的資料獲取,但我們可以透過使用 ggplot2 包的 scale_y_continuous 函式進行設定。例如,如果我們希望以 1 的間隔從 1 到 10 擁有值,那麼我們可以使用 scale_y_continuous(breaks=seq(1,10,by=1))。
示例
Consider the below data frame: x<-rpois(20,5) y<-rpois(20,2) df<-data.frame(x,y) df
輸出
x y 1 8 1 2 4 3 3 1 1 4 4 1 5 3 2 6 3 6 7 6 4 8 3 3 9 7 1 10 4 1 11 5 1 12 6 3 13 3 1 14 6 3 15 8 2 16 0 2 17 8 1 18 5 0 19 4 1 20 4 3
載入 ggplot2 包並建立 x 和 y 之間的點圖 −
示例
library(ggplot2) ggplot(df,aes(x,y))+geom_point()
輸出
建立 Y 軸刻度標記差異為 1 的點圖 −
示例
ggplot(df,aes(x,y))+geom_point()+scale_y_continuous(breaks=seq(1,10,by=1))
輸出
廣告