我遇到了气泡图教程 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()
要反转 popIndex
比例添加到堆栈
scale_fill_gradient(trans = "reverse") +
关于r - 向 R 气泡图添加基于颜色的区分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31581213/