使用R中的colorramp(Rosling Style气泡图)

时间:2016-04-23 21:21:02

标签: r colors bubble-chart

我尝试按照以下链接回答,但没有成功。我应该提到我在入门级数据分析课程中,所以我目前的R技能是有限的。 How to match vector values with colours from a colour ramp in R?

无论如何,我的问题是我正在使用Rosling风格的气泡图,并且颜色没有正确渐变。该图描绘了中国新联盟和MIDS(军事化的州际争端)的数据。具体而言,圆圈大小表示1-7的MIDS的数量。因为区域可能难以识别我选择使用颜色以便更好地区分MIDS,如图例所示。

图例正确显示了我的配色方案,但是,您可能会注意到较大尺寸的圆圈不是深红色,因为它们应该是,也不是所有最小的圆圈都是最亮的颜色。事实上,一些小圆圈和一些大圆圈都是深红色,浅黄色也是如此。我的代码和其他详细信息如下。

我的代码:

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")

有关颜色的一些细节

my.colors99 in R returns:
  

&#34;#FFFFCC&#34; &#34;#FEDEA2&#34; &#34;#FDBD79&#34; &#34;#FD9D50&#34;

     

&#34;#EB7838&#34; &#34;#C75032&#34; &#34;#A3282C&#34; &#34;#800026&#34;

ChnData$MIDS in R returns:
  

&#34;#FFFFCC&#34; &#34;#FEDEA2&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34;
  &#34;#FFFFCC&#34; &#34;#FFFFCC&#34; &#34;#FEDEA2&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34;
  &#34;#FFFFCC&#34; &#34;#FFFFCC&#34; &#34;#FDBD79&#34; &#34;#FFFFCC&#34; &#34;#FEDEA2&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34;
  &#34;#FFFFCC&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34; &#34;#FEDEA2&#34; &#34;#FEDEA2&#34; &#34;#FEDEA2&#34;
  &#34;#FDBD79&#34; &#34;#FFFFCC&#34; &#34;#FD9D50&#34; &#34;#FFFFCC&#34; &#34;#FEDEA2&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34;
  &#34;#FFFFCC&#34; &#34;#FD9D50&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34; &#34;#FEDEA2&#34; &#34;#FFFFCC&#34;
  &#34;#FDBD79&#34; &#34;#FFFFCC&#34; &#34;#FEDEA2&#34; &#34;#FFFFCC&#34; &#34;#FEDEA2&#34; &#34;#FFFFCC&#34; &#34;#FEDEA2&#34;
  &#34;#FFFFCC&#34; &#34;#FDBD79&#34; &#34;#FD9D50&#34; &#34;#FEDEA2&#34; &#34;#FFFFCC&#34; &#34;#FD9D50&#34; &#34;#EB7838&#34;
  &#34;#FDBD79&#34; &#34;#A3282C&#34; &#34;#FFFFCC&#34; &#34;#EB7838&#34; &#34;#EB7838&#34; &#34;#FDBD79&#34; &#34;#FDBD79&#34;
  &#34;#C75032&#34; &#34;#FFFFCC&#34; &#34;#FD9D50&#34; &#34;#C75032&#34; &#34;#FD9D50&#34; &#34;#EB7838&#34; &#34;#FDBD79&#34;
  &#34;#FD9D50&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34; &#34;#FEDEA2&#34; &#34;#FD9D50&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34;
  &#34;#FFFFCC&#34; &#34;#FDBD79&#34; &#34;#FDBD79&#34; &#34;#FFFFCC&#34; &#34;#FFFFCC&#34; &#34;#FEDEA2&#34; &#34;#FEDEA2&#34;
  &#34;#FFFFCC&#34; &#34;#FDBD79&#34; &#34;#FDBD79&#34; &#34;#FDBD79&#34; &#34;#FEDEA2&#34; &#34;#FFFFCC&#34; &#34;#FD9D50&#34;
  &#34;#FD9D50&#34; &#34;#EB7838&#34; &#34;#FDBD79&#34; &#34;#FFFFCC&#34; &#34;#FEDEA2&#34; &#34;#FDBD79&#34; &#34;#EB7838&#34;
  &#34;#FDBD79&#34; &#34;#EB7838&#34; &#34;#FEDEA2&#34; &#34;#FDBD79&#34; &#34;#FEDEA2&#34; &#34;#FDBD79&#34; &#34;#EB7838&#34;

对我而言,似乎颜色方案肯定是关闭的,因为我当然想要最小的圆圈,它们代表最低的中间数#对应于渐变调色板中最亮的颜色,并且随着圆圈尺寸的增加而变暗。希望我提供了足够的细节,但如果需要更多细节,我将很乐意回答任何问题。谢谢你的帮助!

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

1 个答案:

答案 0 :(得分:0)

这正是ggplot2比基本图形更容易使用的绘图类型

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)")

我怀疑您的代码存在的问题是my.colors99[ChnData$mids]没有达到预期效果。当mids为零时,它将被丢弃。其余的值将不合适。