7
$\begingroup$

I have a data frame looking like the following: p1-p5 are different products

A-C: price bands 1-3: size bands e.g. A1: small store selling the expensive that product D3: big store selling cheap that product values are the numbers of stores for each category A1-C2 per product

type    p1  p2  p3  p4  p5
  A1    158 48  18  6   62
  A2    343 151 58  49  55
  B1    400 181 200 29  863
  B2    398 093 448 215 795
  C1    809 217 154 15  422
  C2    877 549 340 46  576

What would it be the best way to visualize that information and which packages are available for that. I have found gridplot but unfortunately I havent managed to try it yet as I have issues with the Rstudio version.

Examples are more than welcome!

I've got that so far from a function found online enter image description here

$\endgroup$
2
  • $\begingroup$ What are you columns? $\endgroup$
    – Stereo
    Mar 6, 2017 at 14:35
  • $\begingroup$ p1,p2,p3,p4,p5. Apologies if the image is a bit misleading, it was created using a different version of what I posted as dummy data. So far, I have only found out different variations of a heatmap... Is it the only/best way of visualising a matrix? $\endgroup$
    – lml
    Mar 6, 2017 at 19:01

3 Answers 3

4
+50
$\begingroup$

I'm expecting your data in this form (use melt to transform the data):

   type product value
1   A1      p1   158
2   A2      p1   343
3   A3      p1   247
4   B1      p1   400
5   B2      p1   398
6   B3      p1   408

You may easily (using lattice package) draw a barchart diagram.

Personally, I'd switch to heatmap only after this diagram is no more feasible due to large number of levels in the product or channel factors.

library(lattice)
barchart(product~ value | type,
data =  dfp,
scales=list(x=list(rot=90, cex= 1 ),y=list(cex=1)),par.strip.text=list(cex=1), origin = 0,  
ylab="Product", xlab="Volume", 
main= "Matrix Visualization")

enter image description here

Of course you may also swap the dimensions by changing the formula. Use the parameter layout to align the barchart based on the number of levels.

barchart(type~ value | product,
data =  dfp,
scales=list(x=list(rot=90, cex= 1 ),y=list(cex=1)),par.strip.text=list(cex=1), origin = 0,  layout= c(5,1),
ylab="Channel", xlab="Volume", 
main= "Matrix Visualization")

enter image description here

$\endgroup$
2
  • $\begingroup$ That looks good... I am having an issue with displaying the second barchart as I actually have more types.. Is there a way to configure the size of the barchart elements? $\endgroup$
    – lml
    Mar 9, 2017 at 20:01
  • $\begingroup$ Check the parameterlayout. I'm using it as well. I'll update the answer mentioning it. $\endgroup$ Mar 9, 2017 at 20:12
3
$\begingroup$

You can simply use the heatmap function in the basic {stats} package. If desired, this can also display a dendrogram to show clusters. For your sample data (with heatmap(df)) this would look something like this:

enter image description here

On r-bloggers you can find some additional comments about the functionalities of heatmap.

$\endgroup$
2
$\begingroup$

One way to look at this data is by drawing trend lines.

Line graph

library(tidyverse) # for data transformations
library(magrittr) # for piping
library(forcats) # for factor manipulations
data %>% 
separate("type", c("price", "size"), sep=-2, remove = F) %>% # optional, separate 'type' into 'price' and 'size' bands
gather("product", "value", -c(price, size, type)) %>% # 'melt' products
mutate(type = fct_reorder(as.factor(type), value),
product = fct_reorder(as.factor(product), value)) %>% # optional, re-order 'type'/'product' factors by median 'value' numbers
ggplot() + geom_line(aes(x=type, y=value, color=product, group=product))

enter image description here

In case you would like a heatmap, there is another way to use that too.

Heat map

data %>% 
separate("type", c("price", "size"), sep=-2, remove = F) %>% 
gather("product", "value", -c(price, size, type)) %>% 
mutate(type = fct_reorder(as.factor(type), value),
product = fct_reorder(as.factor(product), value)) %>% 
ggplot() + geom_tile(aes(x=price, y=size, fill=value)) + facet_grid(product~.)

enter image description here

Explanation

Notice that in the line graph the 'type' factor is re-ordered by the median 'value' numbers. The plot shows that A1 tends to have lower value, whereas C2 tends to have higher value.
Similarly, in the heat map the 'product' factor is re-ordered by the median 'value' numbers. Looking at the plot you can tell that p4 has the highest median values, whereas p5 has the lowest median values.
You can play with re-ordering and faceting to identify different trends.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.