0

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.stack.imgur.com/XEn2g.png

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

jlhoward
  • 58,004
  • 7
  • 97
  • 140

2 Answers2

0
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()
Sardimus
  • 156
  • 8
0

This seems to come close to your graphic, although the colors are different.

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

Notes:

  1. Used geom_tile(...) to make the blocks.
  2. Used rev(heat.colors(10)) to create the color palette (reds for high values, pale yellow for low values).
  3. Used coord_fixed(...) so the blocks are square (sets aspect ration to 1:1).
  4. Used expand=c(0,0) so the blocks fill the whole plotting area.
jlhoward
  • 58,004
  • 7
  • 97
  • 140