0

everyone,

I have the following data set (called demo).

    Nation   GDP FHouse OECD   regime
1   Albania  3719    4.5    0 not free
2   Algeria  5327    5.5    0 not free
3    Angola  1462    6.0    0 not free
4 Argentina 12095    1.5    0     free
5   Armenia  2421    4.0    0 not free
6 Australia 27390    1.0    1     free

I made a plot make a plot of GDP and Fhouse.

plot(demo$GDP, demo$FHouse, xlab = "Country's GDP", ylab = "Freedom House Rating", pch=16, xlim = c(500,5000), family="serif")

Now, I am being asked to color each point based on regime (free,not free, partly free). I've been reading, and apparently I can use ifelse, however, I don't know how to give it three conditions so that each country is colored based on free, not free, and partly free.

Thank you all.

thelatemail
  • 91,185
  • 12
  • 128
  • 188
Rnub
  • 29
  • 2
  • 2
    an easier way is to use `factors`: `plot(mpg ~ wt, mtcars, col = factor(gear))` you can also set custom colors this way: `plot(mpg ~ wt, mtcars, col = c('red', 'blue', 'orange')[factor(gear)])` – rawr Feb 09 '21 at 02:02

1 Answers1

0

If you want to continue with base R plotting you can do :

demo$color <- ifelse(demo$regime == 'free', 'red', 
                ifelse(demo$regime == 'not free', 'yellow', 'green'))

plot(demo$GDP, demo$FHouse, xlab = "Country's GDP", 
      ylab = "Freedom House Rating", pch=16, family="serif", col = demo$color)

With ggplot2 :

library(ggplot2)
ggplot(demo) + 
  aes(GDP, FHouse, color = regime) + 
  geom_point() + 
  theme_bw()
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213