0
votes

How do i explicitly assign a specific color to a particular factor/group using the plot command? I have plot two datasets - each having two groups (A3 & A4). The only difference between them is that one of them have two rows (outlier) removed. However, the colors were completely different between the two datasets. And in each group, the colors are not distinct between the two groups. Please refer:

All data 2 data removed

And this is my code:

d<-read.csv("data-outlier.csv",header=T)

par(mar=c(5,5.5,4,2)+0.1)
plot(
  formula = Wealth ~ Age,
  data = d,
  pch = 20,
  col = c('blue','red2'),
  xlab = expression(Age),
  ylab = expression(Income),
  xlim=c(0,140),
  las = 1,
  cex = 1.5,
  cex.axis = 1.6, cex.lab = 1.8,
  bty = 'l'
)
A3 <- subset(d, Team=="A3")
A4 <- subset(d, Team=="A4")
rA3 <- lm(Wealth~Age, data=A3);
rA4 <- lm(Wealth~Age, data=A4);
abline(rA3, lty=3, col='blue')
abline(rA4, lty=3, col='red2')

 legend(x = 'bottomright', 
        legend = c('A3','A4'),
        col = c('blue','red2'), 
        pch = c(20,20),
        bty = 'n')

I am sorry i do not know how to format this data into a vector to be put here, but here are the external links: 1. All data 2. 2 data removed

Is there a way to specify one color to one group besides plotting using "points" separately (and besides using ggplot)?

Thank you in advance!

1

1 Answers

1
votes

step 1: filter the data and create a plot for group A3 using plot()

d<-read.csv("data-outlier.csv",header=T)
A3 <- subset(d, Team=="A3")


par(mar=c(5,5.5,4,2)+0.1)
plot(A3$Age,A3$Wealth,
  pch = 20,
  col = "blue",
  xlab = expression(Age),
  ylab = expression(Income),
  xlim=c(0,140),
  las = 1,
  cex = 1.5,
  cex.axis = 1.6, cex.lab = 1.8,
  bty = 'l'
)
rA3 <- lm(Wealth~Age, data=A3)
abline(rA3, lty=3, col='blue')

step 2: add group A4 data to the plot using points()

A4 <- subset(d, Team=="A4")
points(A4$Age,A4$Wealth,
        pch = 20,
        col = "red",
        las = 1,
        cex = 1.5,
        bty = 'l')

rA4 <- lm(Wealth~Age, data=A4)
abline(rA4, lty=3, col='red2')

legend(x = 'bottomright', 
       legend = c('A3','A4'),
       col = c('blue','red2'), 
       pch = c(20,20),
       bty = 'n')