【问题标题】:Forming a Stack Bar chart/ Grid with Color Intensity varying on Value形成颜色强度随值变化的堆叠条形图/网格
【发布时间】:2014-07-21 18:26:29
【问题描述】:

我正在尝试使网格看起来像堆积条形图。条形图的各个部分代表 App 类别,x 轴在其价格等级上绘制条形图。我希望栏部分的颜色强度随着类别中应用程序的数量而变化。我尝试使用 ggplot2 geom_bar 但无法弄清楚如何在 Y 轴上绘制类别。我的数据如下所示:

Category    No.Apps Price
Utilities   400     0
Utilities   300     1-10
Utilities   500     11-20
Utilities   200     21-30
Games       1000    0
Games       900     1-10
Games       400     11-20
Games       100     21-30
Productivity 300    0
Productivity 100    1-10
Productivity 50     11-20
Productivity 80     21-30

我希望我的图表看起来像这样:

http://i.stack.imgur.com/XEn2g.png

在 y 轴上显示类别,在 x 轴上显示价格等级。

【问题讨论】:

    标签: r ggplot2 raster rgraph


    【解决方案1】:
    library(ggplot2)
    
    #making some fake data and putting it into a data.frame
    price <- rnorm(n = 10000,mean = 5,sd = 2)
    app <- sample(x = LETTERS[1:10],size = 10000,replace = T)
    df <- data.frame(price,app)
    head(df)
    
    #now the plot
    ggplot(df, aes(x=price,y=app))+
      geom_bin2d()
    

    【讨论】:

      【解决方案2】:

      这似乎接近您的图形,虽然颜色不同。

      library(ggplot2)
      ggplot(df, aes(x=Price,y=Category, fill=No.Apps)) + 
        geom_tile()+
        scale_fill_gradientn(colours=rev(heat.colors(10)))+
        scale_x_discrete(expand=c(0,0))+
        scale_y_discrete(expand=c(0,0))+
        coord_fixed()
      

      注意事项:

      1. 使用geom_tile(...) 制作积木。
      2. 使用rev(heat.colors(10)) 创建调色板(红色表示高值,淡黄色表示低值)。
      3. 使用了coord_fixed(...),所以方块是方形的(将纵横比设置为 1:1)。
      4. 使用了expand=c(0,0),所以方块填满了整个绘图区域。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-31
        • 2023-04-09
        • 1970-01-01
        • 2018-04-01
        • 2023-03-28
        • 1970-01-01
        相关资源
        最近更新 更多