in R, how do I have the scatterplot choose a color for a point based on the value of another variable?

AviG

I have a data set;

newData <- cbind(c(1,2,3,4,5),c(6,7,8,9,10),c(A,B,A,B,B))

I want to make a scatter plot on a two dimensional plane, but I want the points colored by if they have A or B. Using plot(params), how would I do that?

G5W

If you create the variable newData as you describe in the question, then it will be a matrix of text. I think that you want the first two columns to be numbers and the last column to be text. In order to mix numbers and text like that, you need a different data structure. A good one to use is a data.frame

newData <- data.frame(V1 = c(1,2,3,4,5),
    V2 = c(6,7,8,9,10), V3 = c('A','B','A','B','B'))
newData
  V1 V2 V3
1  1  6  A
2  2  7  B
3  3  8  A
4  4  9  B
5  5 10  B

Once you have that, the plot is easy.

plot(newData[,1:2], pch=20, col=c("red", "blue")[newData$V3])

Basic plot

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

R: How do I choose which row dplyr::distinct() keeps based on a value in another variable?

How do I change a value within a variable based on the value of another variable in R?

How do I set the value of a variable based on another variable?

How do I add to a variable based on another variables value? (Python)

R: How do I make a one-variable scatterplot?

How do I conditionally change the value of a variable based on the value of another variable?

How do I create a variable that increments by 1 based on the value of another variable?

How do I get the value of a variable whose name is based on another variable?

How do I set a color for each point in a graph depending on a value

How do i set a temp variable to a value based from another table

How do I replace the value of an observation based on another variable's values, within a group?

How do I reorder a points and corresponding labels in ggplot2 scatterplot by the numeric variable in R?

How do I decode the values of the variable in one dataframe based on another dataframe in R?

How do I create a condition from a variable value that I have?

How do I get a table value based on the value of another string?

How do I assign values from one data frame to another based on row value using R?

How do I change the color of an infobox in shinydashboard based on a string, not a value?

How can I set a runtime environment variable based on the value of another

How to create a variable with value based on probabilities in another data frame in R?

Using seaborn, how can I add a data point of a different color to my scatterplot or change to color of last data point?

How to add legend based on color in scatterplot [matplotlib]

In Google Sheets, how do I filter based on the value of another cell?

How do I select column based on value in another column with dplyr?

How do I replace missing values based on median of another value?

How do I restrict a cell value to a maximum based on another cell?

How do I change the color of the pixels I choose?

I have 2 sets of data mapped to each other. Is there a way to based on the value of another variable check that corresponding variable for its value?

How do I replace string with the value of another variable in PowerShell

How do I get a value from a variable in another View Controller