R color scatter plot points based on values

后端 未结 4 1584
悲哀的现实
悲哀的现实 2020-11-28 08:03

I am able to plot a scatter plot and color the points based on one criteria, i.e. I can color all points >=3 as red and the remainder as black. I would love to be able to co

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 08:24

    Best thing to do here is to add a column to the data object to represent the point colour. Then update sections of it by filtering.

    data<- read.table('sample_data.txtt', header=TRUE, row.name=1)
    # Create new column filled with default colour
    data$Colour="black"
    # Set new column values to appropriate colours
    data$Colour[data$col_name2>=3]="red"
    data$Colour[data$col_name2<=1]="blue"
    # Plot all points at once, using newly generated colours
    plot(data$col_name1,data$col_name2, ylim=c(0,5), col=data$Colour, ylim=c(0,10))
    

    It should be clear how to adapt this for plots with more colours & conditions.

提交回复
热议问题