简体   繁体   中英

Scatter plot is empty after assigning different colour argument in R

I have a dataset (df) with 111 data points that are assigned to three categorical classes labelled "Acoustic", "Visual" and "Both".

bft swell depth      dist boats     type
1   1   1.0    10 0.5249928     0 Acoustic
2   1   2.0    13 1.3942633     0 Acoustic
3   1   0.5    10 0.5064976     0 Acoustic
4   2   0.5    16 2.9369194     2 Acoustic
5   2   0.5    14 2.8161770     0 Acoustic
6   1   0.5    15 2.5131418     0 Acoustic

And I have five feature parameters that I want to use to cluster these classes. The data doesn't cluster well, and I can see this via a scatter plot (and that's okay) but when I try to colour code these plots, it returns blank plots. Even if they don't cluster well, I feel like it should still plot the colours. Im new to this so Im probably missing something. Could someone help me out please? Below is the code Im using and Im also attaching a picture of my scatter plot w/o colour.

df <- porpoise[,c("bft", "swell", "depth", "dist", "boats", "type")]
pairs(df[,1:5], pch = 15, lower.panel = NULL)
my_cols <- c("#00AFBB", "#E7B800", "#FC4E07") 

pairs(df[,1:5], pch = 19,  cex = 0.5,
      col = my_cols["type"],
      lower.panel=NULL)

When I tried to do a trial cluster analysis on this dataset, the same thing happens but I get this warning after I run the det_col line "NAs introduced by coercion"

df2 <- df[,-6]
detection_type <- df[,6]
library(colorspace)
det_col <- rev(rainbow_hcl(3))[as.numeric(detection_type)]

I feel this has something to do with the plots not being able to get colour coded (if this helps to solve the mystery).

Thanks在此处输入图像描述

Try adding a column which gives each type a color of your my_cols and assign that column to col in pairs like this:

porpoise <- read.table(text = "bft swell depth      dist boats     type
1   1   1.0    10 0.5249928     0 Acoustic
2   1   2.0    13 1.3942633     0 Acoustic
3   1   0.5    10 0.5064976     0 Acoustic
4   2   0.5    16 2.9369194     2 Acoustic
5   2   0.5    14 2.8161770     0 Acoustic
6   1   0.5    15 2.5131418     0 Acoustic", header = TRUE)

df <- porpoise[,c("bft", "swell", "depth", "dist", "boats", "type")]
pairs(df[,1:5], pch = 15, lower.panel = NULL)

my_cols <- c("#00AFBB", "#E7B800", "#FC4E07") 

library(dplyr)
df <- df %>%
  mutate(color = case_when(type == "Acoustic" ~ my_cols[1],
                           type == "Visual" ~ my_cols[2],
                           type == "Both" ~ my_cols[3]))

pairs(df[,1:5], pch = 19,  cex = 0.5,
      col = df$color,
      lower.panel=NULL)

Created on 2022-08-13 by the reprex package (v2.0.1)

Please note: It only shows one color, because you only shared the type "Acoustic".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM