r - 向 R 气泡图添加基于颜色的区分

标签 r ggplot2 bubble-chart

我遇到了气泡图教程 here ,我正在重复使用其代码以进行说明。请在下面找到代码:

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

在此图中,气泡大小由人口决定。我的问题是,如果我必须使用气泡颜色在绘图中容纳 popIndex 变量,我该怎么做?

最佳答案

我会这样画。请注意,大小现在没有与之关联的颜色,因为此“域”已被 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()

enter image description here

要反转 popIndex 比例添加到堆栈

scale_fill_gradient(trans = "reverse") +

关于r - 向 R 气泡图添加基于颜色的区分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31581213/

相关文章:

r - ggplot2:并排的条形图,一个条堆叠,另一个没有

R:带有 ddply 的 for 循环

r - ggplot2 绘制横向填充页面 (pdf)

r - 填充和空心形状,其中填充颜色 = 线条颜色

r - 如何使用回归模型删除ggplot中的图例?

highcharts - 显示 Highcharts 气泡图的附加文本

r - 在 R 中使用 apply 函数进行字符串连接

r - ggplot2 情节在情节中的完美拟合

javascript - 如何根据列值给DC js气泡图着色?