0

I am new to R, so my problem is simple. I am trying to create a scatterplot that shows data about all countries, and then size the bubbles by population and color them by region. I have accomplished everything except the coloring. Here is my code:

attach(gapminder2)
colors(distinct = FALSE)
radius<-sqrt(gapminder2$Population/pi)
plot(log(`CO2 Emissions per capita`), log(Income), main="PerCapita GDP vs CO2 Emissions by Country", xlab="CO2 Emissions (tons/person)", ylab="Per Capita GDP (US$)")
grid()
symbols(log(`CO2 Emissions per capita`), log(Income), main="PerCapita GDP vs CO2 Emissions by Country", xlab="CO2 Emissions (tons/person)", ylab="Per Capita GDP (US$)", circles=radius, inches=0.25)

All of that runs fine and produces the following graph:

GDP vs CO2 graph

In the dataset (called "gapminder2"), I have a variable "Region". I have tried converting it to a factor variable (Region<- as.factor) but i still don't know how to assign each individual region its own color on the graph (I have 8 different regions).

  • See [this answer](https://stackoverflow.com/questions/7721262/colouring-plot-by-factor-in-r). Try adding adding `col = gapminder2$Region` to `plot` – Hallie Swan Oct 10 '17 at 00:43
  • Ok, I added that, but got the error message: "invalid color name 'Middle East'". Do you know how I could assign the colors to each region within the variable? @blondeclover – Chloe Davis Oct 10 '17 at 00:47
  • The fact that this didn't work suggests that gapminder2$Region is not stored as a factor. Try doing `gapminder2$Region <- as.factor(gapminder2$Region)` and then use @blondeclover 's solution. – Jim Quirk Oct 10 '17 at 01:02
  • @JimQuirk Ok thank you, that worked to get the colors. Now, the problem is that it removes the sizing of the bubbles, and I can't figure out a command that runs both color and sizing. Also, it only does an outline color rather than a fill color. Any thoughts? – Chloe Davis Oct 10 '17 at 01:08

1 Answers1

0

You can do this all in one plot command, using some of the graphical parameters:

plot(
  log(`CO2 Emissions per capita`),
  log(Income),
  pch=16, ## filled in circles
  cex=radius, ## varies the size,
  col=as.integer(as.factor(Region)+1) ## colour by region, skipping black
)
JDL
  • 1,496
  • 10
  • 18