4
$\begingroup$

I have a dataset which looks like-

Variable value year Quarter Location
A 48.235 2011 Q1 North
B 65.444 2011 Q2 North
C 77.453 2011 Q3 North
D 44.678 2011 Q4 North
A 88.542 2012 Q1 South
B 66.566 2012 Q2 South
C 55.443 2012 Q3 South
D 78.990 2012 Q4 South

Can anybody help me with a code to generate heatmap with more than 2 categorical variables? I need 'variable' and 'Location' to be on y axis and 'Quarter'&' year' to be on x axis. The shades of the data inside heat map should correspond to 'value'.

$\endgroup$
2
$\begingroup$

Here is one way of visualizing the data you have presented. However, I have used some liberties and assumptions to create this plot.

First, create a year_quarter variable that simply concatenates the year and quarter to present the time on the X axis. The following piece of code can do this in R

year_quarter = paste(dat$year, dat$Quarter, sep="-")

Now, the dataset you have will look like:

> dat

Variable value year Quarter Location year_quarter 1 A 48.235 2011 Q1 North 2011-Q1 2 B 65.444 2011 Q2 North 2011-Q2 3 C 77.453 2011 Q3 North 2011-Q3 4 D 44.678 2011 Q4 North 2011-Q4 5 A 88.542 2012 Q1 South 2012-Q1 6 B 66.566 2012 Q2 South 2012-Q2 7 C 55.443 2012 Q3 South 2012-Q3 8 D 78.990 2012 Q4 South 2012-Q4

Finally, using ggplto2, you can create the plot such that the colour represents the value, the shape represents the Location and the size represents the Variable.

This simple one liner can help you produce such a plot:

p = ggplot(dat, aes(x = year_quarter, y = value, colour = value)) + geom_point(aes(shape = Location,size = Variable))

This is how the output plot looks like:

enter image description here

Note that you can also add geom_line with interaction if you would like the lines to be connected based on Location and Variable.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Please let me know if you need clarification. $\endgroup$ – Nitesh Mar 23 '16 at 14:38
  • $\begingroup$ I tried this with ggtile too. I included facets in it. Do you have any idea if we can REDUCE the gap between facets to ZERO? $\endgroup$ – pinky Mar 23 '16 at 16:58
  • $\begingroup$ May be stackoverflow.com/questions/3681647/… can provide you with how to control for spaces between different facets. Though I am not sure I understand exactly what you're trying to do. $\endgroup$ – Nitesh Mar 24 '16 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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