如何在R中使用ggplot2建立具有深色點的散點圖?
要使用ggplot2建立點強度低的散點圖,我們可以按照以下步驟操作:
- 首先,建立一個數據框。
- 然後,建立散點圖。
- 使用scale_color_hue函式建立具有深色點的散點圖。
建立資料框
讓我們建立一個如下所示的資料框:
x<-sample(1:100,25) y<-sample(1:100,25) Factor<-sample(c("Real","Imaginary"),25,replace=TRUE) df<-data.frame(x,y,Factor) df
執行上述指令碼後,將生成以下輸出(由於隨機化,此輸出將在您的系統上有所不同):
x y Factor 1 43 50 Imaginary 2 6 52 Imaginary 3 19 60 Real 4 42 24 Real 5 99 66 Imaginary 6 32 69 Real 7 11 91 Imaginary 8 97 93 Imaginary 9 16 99 Real 10 76 7 Real 11 95 89 Imaginary 12 13 4 Imaginary 13 74 49 Imaginary 14 40 94 Real 15 54 47 Imaginary 16 38 15 Real 17 96 74 Real 18 20 72 Imaginary 19 34 23 Real 20 57 87 Imaginary 21 82 2 Real 22 36 59 Imaginary 23 98 67 Real 24 1 98 Real 25 65 62 Real
建立散點圖
使用ggplot2包的geom_point函式建立散點圖:
x<-sample(1:100,25) y<-sample(1:100,25) Factor<-sample(c("Real","Imaginary"),25,replace=TRUE) df<-data.frame(x,y,Factor) library(ggplot2) ggplot(df,aes(x,y,color=Factor))+geom_point()
輸出
建立具有深色點的散點圖
使用ggplot2包的scale_color_hue函式和l引數建立具有深色點的散點圖(l的預設值為65):
x<-sample(1:100,25) y<-sample(1:100,25) Factor<-sample(c("Real","Imaginary"),25,replace=TRUE) df<-data.frame(x,y,Factor) library(ggplot2) ggplot(df,aes(x,y,color=Factor))+geom_point()+scale_color_hue(l=50)
輸出
廣告