0

I tried following the answer at the following link without success. I should mention that I'm in an entry level Data Analysis Course, so my current R skills are limited. How to match vector values with colours from a colour ramp in R?.

Anyway, my issue is that I am working with a Rosling style bubble chart, and the colors are not ramping correctly. The graph depicts data on China's New Alliances and MIDS (militarized interstate disputes). Specifically, the circle size indicates the # of MIDS from 1-7. Because area can be difficult to discern I opted to also use color in order to better distinguish the # of MIDS, as the Legend Shows.

The legend correctly shows my color scheme, however, you may notice that circles of larger size are not deep red, as they should be, nor are all the smallest circles the lightest color. In fact, some small and some large circles are dark red, and the same is true for the light yellow. I have my code and other details below.

MY CODE:

library(RColorBrewer)
my.colors99 <-colorRampPalette(c("#ffffcc", "#fd8d3c", "#800026"))(8)
my.design <- my.colors99[ChnData$mids]   

radius1 <- sqrt( ChnData$mids/pi )


symbols ( ChnData$year, ChnData$alliances,
circles= radius1, inches=.35,
xlab = "Year", ylab = "New Alliances", 
main = "China's New Alliances (Circles = MIDS)",
ylim=nrow(ChnData$alliances),
xlim= nrow(ChnData$year), 
bg = my.colors99)

legend("topleft", 
legend=c("0", "1", 
"2", "3", 
"4", "5", 
"6", "7"), title= "# of Mids",
col=my.colors99, pch=19, cex=1, box.col="grey60", 
text.col="grey30")

Some Details on the colors

my.colors99 in R returns:

"#FFFFCC" "#FEDEA2" "#FDBD79" "#FD9D50"

"#EB7838" "#C75032" "#A3282C" "#800026"

ChnData$MIDS in R returns:

"#FFFFCC" "#FEDEA2" "#FFFFCC" "#FFFFCC" "#FFFFCC" "#FFFFCC" "#FFFFCC"
"#FFFFCC" "#FFFFCC" "#FEDEA2" "#FFFFCC" "#FFFFCC" "#FFFFCC" "#FFFFCC"
"#FFFFCC" "#FFFFCC" "#FDBD79" "#FFFFCC" "#FEDEA2" "#FFFFCC" "#FFFFCC"
"#FFFFCC" "#FFFFCC" "#FFFFCC" "#FFFFCC" "#FEDEA2" "#FEDEA2" "#FEDEA2"
"#FDBD79" "#FFFFCC" "#FD9D50" "#FFFFCC" "#FEDEA2" "#FFFFCC" "#FFFFCC"
"#FFFFCC" "#FD9D50" "#FFFFCC" "#FFFFCC" "#FFFFCC" "#FEDEA2" "#FFFFCC"
"#FDBD79" "#FFFFCC" "#FEDEA2" "#FFFFCC" "#FEDEA2" "#FFFFCC" "#FEDEA2"
"#FFFFCC" "#FDBD79" "#FD9D50" "#FEDEA2" "#FFFFCC" "#FD9D50" "#EB7838"
"#FDBD79" "#A3282C" "#FFFFCC" "#EB7838" "#EB7838" "#FDBD79" "#FDBD79"
"#C75032" "#FFFFCC" "#FD9D50" "#C75032" "#FD9D50" "#EB7838" "#FDBD79"
"#FD9D50" "#FFFFCC" "#FFFFCC" "#FEDEA2" "#FD9D50" "#FFFFCC" "#FFFFCC"
"#FFFFCC" "#FDBD79" "#FDBD79" "#FFFFCC" "#FFFFCC" "#FEDEA2" "#FEDEA2"
"#FFFFCC" "#FDBD79" "#FDBD79" "#FDBD79" "#FEDEA2" "#FFFFCC" "#FD9D50"
"#FD9D50" "#EB7838" "#FDBD79" "#FFFFCC" "#FEDEA2" "#FDBD79" "#EB7838"
"#FDBD79" "#EB7838" "#FEDEA2" "#FDBD79" "#FEDEA2" "#FDBD79" "#EB7838"

To me it seems that the color scheme is certainly off, as I of course want the smallest circles, which represent the lowest # of mids to correspond to the lightest color in the ramp palette, and to darken as the circle size increases. Hopefully I provided enough details, but will be glad to answer any questions if more details are needed. Thanks for the help!

myplotgraphic circles small, color scheme correct with 1-mids but legend isnt

Community
  • 1
  • 1
Robborino
  • 1
  • 3

1 Answers1

0

This is exactly the type of plot where ggplot2 is so much easier to use than base graphics

require(ggplot2)
ggplot(ChnData, aes(x = year, y = alliances, col = mids, size = mids)) + 
  geom_point() +
  scale_colour_distiller(palette = "OrRd", direction  = -1) +
  scale_size(range = c(2, 12)) +
  labs(y = "New Alliances", title = "China's New Alliances (Circles = MIDS)")

I suspect the problem with your code is that my.colors99[ChnData$mids] does not do what you expect. When mids is zero, it will be dropped. The remaining values will then be out of place.

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
  • Thank you SO much! I've been struggling with this for quite a bit and you simplified very nicely, so I appreciate that. If you don't mind, how can I change a few minor details. I want to scale all the circles larger ( doing size = mids*10 only affected the legend and not actual circle size) and how do I reverse the color scheme (currently more MIDS refers to lighter, instead of darker circles). I tried col = 1-mids, and this worked for flipping the colors, but also affected the legend by making all values I'll have new pics in the OP to demonstrate. – Robborino Apr 24 '16 at 00:07
  • edited answer to make the sizes larger and change the direction of the colour scale – Richard Telford Apr 24 '16 at 08:03