如何在R中使用ggplot2建立的圖表中去除刻度線?


在使用ggplot2建立的圖表中,座標軸值會生成刻度線,例如表示X軸標籤從1到10,Y軸標籤從10到1,但我們可以使用theme函式去除這些刻度線。如果想要建立一個沒有刻度線的圖表,只需要在繪圖程式碼中新增以下程式碼。

theme(axis.ticks.x=element_blank(),axis.ticks.y=element_blank())

示例

考慮以下資料框。

線上演示

> set.seed(321)
> x<-rnorm(20,1,0.025)
> y<-rnorm(20,5,1.35)
> df<-data.frame(x,y)
> df

輸出

x y
1 1.0426226 6.238295
2 0.9821990 4.855467
3 0.9930504 6.334253
4 0.9970088 3.552478
5 0.9969010 3.976679
6 1.0067046 5.128251
7 1.0181710 1.853243
8 1.0058284 5.563647
9 1.0084778 3.487558
10 0.9862021 4.359176
11 1.0086925 2.933944
12 1.0371148 5.561215
13 1.0047081 5.856167
14 1.0610815 6.661644
15 0.9711640 4.791339
16 0.9798832 5.154628
17 1.0114017 1.994545
18 1.0105083 7.208317
19 1.0144396 4.784917
20 1.0111589 5.038161

載入ggplot2包並在x和y之間建立一個點狀圖。

> library(ggplot2)
> ggplot(df,aes(x,y))+geom_point()

輸出

使用ggplot2建立沒有座標軸刻度線的點狀圖。

示例

> ggplot(df,aes(x,y))+geom_point()+theme(axis.ticks.x=element_blank(),axis.ticks.y=element_blank())

輸出

更新於:2020年11月7日

142 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.