如何在R中使用ggplot2建立沒有灰色背景的散點圖?
為了在R中使用ggplot2建立沒有灰色背景的散點圖,我們可以按照以下步驟操作:
- 首先,建立一個數據框。
- 然後,使用ggplot2包的geom_point函式建立散點圖。
- 之後,將theme_minimal()函式新增到ggplot函式中。
建立資料框
讓我們建立一個如下所示的資料框:
x<-rnorm(25) y<-rnorm(25) df<-data.frame(x,y) df
執行上述指令碼後,會生成以下輸出(由於隨機化,此輸出在您的系統上可能會有所不同):
x y 1 -0.12641811 1.95757659 2 -1.38633565 0.77338055 3 -1.22762385 -0.44237134 4 -0.64563725 0.89688293 5 0.26524186 -0.70475151 6 0.17533808 -0.94449945 7 -0.49610310 0.39499452 8 0.63024372 -2.10619250 9 -0.26097072 0.72478045 10 -0.25907688 0.06150708 11 0.43858635 0.98054086 12 -0.47139236 1.27132622 13 -1.30498718 -0.20353174 14 0.15307884 -2.02789784 15 0.30174693 -1.15737060 16 -0.79224642 -1.20908515 17 -0.69133131 -1.07662121 18 0.84688970 0.47372022 19 -0.71300947 -0.42851191 20 0.04015489 0.41890481 21 -0.19846102 0.86615792 22 -0.37124762 0.16788345 23 -0.70334925 1.74406602 24 0.66638451 0.55980663 25 0.78728771 0.49338160
建立散點圖
載入ggplot2包並在x和y之間建立散點圖:
x<-rnorm(25) y<-rnorm(25) df<-data.frame(x,y) library(ggplot2) ggplot(df,aes(x,y))+geom_point()
輸出
建立沒有灰色背景的散點圖
使用theme_minimal()建立沒有灰色背景的散點圖:
x<-rnorm(25) y<-rnorm(25) df<-data.frame(x,y) library(ggplot2) ggplot(df,aes(x,y))+geom_point()+theme_minimal()
輸出
廣告