簡體   English   中英

我如何使用 R 在 R 中隨機使用 3 個不同的 colors 為圖形點着色

[英]How can i color points of a graph randomly with 3 different colors in R using R basic only

我正在使用 R 中的數據集iris 我需要制作一個軸 X=Petal.Length 和軸 Y=Petal.Width 的圖表。
這很簡單。 但我需要隨機 colors 3 個不同 colors 中的圖形點。 我找到了每種顏色的十六進制值:

  • 橙色: #ff8000
  • 粉紅色: #ff80c0
  • 黃色: #ffff00

這是我需要的顏色:

需要的顏色

我嘗試使用rgb function 但沒有成功。
有人可以幫我找到如何用這 3 個 colors 隨機給圖表的點上色嗎?

這是我的 plot 的代碼,它只是沒有具體細節 colors 的圖表:

plot(iris$Petal.Length, iris$Petal.Width, xlab = "Petal Lenght", ylab = "Petal Width", pch=18)

謝謝您的幫助

只需將這些顏色sample到適當的長度並將它們提供給col參數:

my_colours <- sample(c("#ff8000", "#ff80c0", "#ffff00"), nrow(iris), replace = TRUE)

plot(iris$Petal.Length, iris$Petal.Width, 
     xlab = "Petal Length", ylab = "Petal Width", pch=18, col = my_colours)

在此處輸入圖像描述

您也可以嘗試將顏色連接到 col function

plot(iris$Petal.Length, iris$Petal.Width, xlab = "Petal Lenght", ylab = "Petal Width", pch=18, col = c("#ff8000","#ff80c0","#ffff00"))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM