为R气泡图添加基于颜色的区别

Sai*_*esh 2 bubble-chart r ggplot2

我会遇到一个气泡图教程在这里,他的代码,我重用了澄清.请找到以下代码:

crime <-read.csv("http://datasets.flowingdata.com/crimeRatesByState2005.tsv", header=TRUE, sep="\t")

popIndex <- crime$population/max(crime$population)
crime.new <- cbind(crime,popIndex)

ggplot(crime, aes(x=murder, y=burglary, size=population, label=state),guide=FALSE)+
geom_point(colour="white", fill="#E69F00", shape=21)+ scale_size_area(max_size = 22)+
scale_x_continuous(name="Murders per 1,000 population", limits=c(0,12))+
scale_y_continuous(name="Burglaries per 1,000 population", limits=c(0,1250))+
geom_text(size=4)+
theme_bw()
Run Code Online (Sandbox Code Playgroud)

在该图中,气泡大小由总体确定.我的问题是,如果我要popIndex使用气泡颜色在图中容纳变量,我该怎么做呢?

Rom*_*rik 6

我会这样画.请注意,大小现在没有与之关联的颜色,因为此"域"已被接管fill = popIndex.

crime$popIndex <- crime$population/max(crime$population)

ggplot(crime, aes(x=murder, y=burglary, label=state))+
  geom_point(aes(size=population, fill = popIndex), shape=21)+ 
  scale_size_area(max_size = 22)+
  scale_x_continuous(name="Murders per 1,000 population", limits=c(0,12))+
  scale_y_continuous(name="Burglaries per 1,000 population", limits=c(0,1250))+
  geom_text(size=4)+
  theme_bw()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

要反转popIndex比例添加到堆栈

scale_fill_gradient(trans = "reverse") +
Run Code Online (Sandbox Code Playgroud)

  • @thothal这当然是一个相关的统计数据,但从不同的角度来看.如果尺寸足以传达信息,我个人不会打扰颜色. (2认同)