Search code examples
rggplot2rasterrgraph

Forming a Stack Bar chart/ Grid with Color Intensity varying on Value


I am Trying to make a grid look like stacked bar chart. The various sections of the Bar represent the App category and x-axis plots the bar on their price class. I want the color intensity of the section of the bar to change with the number of Apps in the category. I tried using ggplot2 geom_bar but am not able to figure out as to how can I plot categories on Y-axis. My data looks like this:

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

I want my graph to look somewhat like this:

https://i.sstatic.net/XEn2g.png

with categories on y-axis and price-class on x-axis.


Solution

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