【问题标题】:Add colour based distinction to R bubble charts为 R 气泡图添加基于颜色的区别
【发布时间】:2015-07-23 07:49:22
【问题描述】:

我遇到了一个气泡图教程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 变量,我该怎么做?

【问题讨论】:

    标签: r ggplot2 bubble-chart


    【解决方案1】:

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

    【讨论】:

    • 除了这个出色的答案,我想知道您从popIndex 中获得了哪些额外信息?基本上它只是基于最大人口的百分比,所以气泡越小它们越暗。因此,在我看来,这两个美学编码相同的信息,那么附加值是什么?
    • @thothal 这当然是一个相关的统计数据,但从不同的角度来看。如果尺寸足以传达信息,我个人不会在意颜色。
    • @thothal 这在我正在研究的另一个问题中是有意义的,但我想用上面给出的一个更简单的例子来问同样的问题。就是这样。
    猜你喜欢
    • 2018-06-10
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多