Monday, 15 August 2011

r - How to control colors and breaks in heatmap using ggplot? -


I am trying to create a heatmap using the ggplot2 package. I have trouble controlling the color and brake on summer and I have 18 questions, 22 firms and the average value of the firms reacts to the scale of 1 to 5.

Say I value (0-1) (1-2) (2-3) (3-4) (4-5) color coded. Either different colors (blue, green, red, yellow, purple) or a gradient scale. And also NA values ​​= black brief: How do I choose colors and breaks?

I would also like to fix the order on the axis for "Question 1, Question 2 ... Q18" Similarly, for firms, at this time I believe that this class is the "factor" which causes this problem.

  & gt; Head (Mind, 20) Firm Question value 1 1 question 1 3.6675482217047 2 1 question 2 3.74327628361858 3 1 question 3 & gt; NA & gt; 4 1 question 4 & gt; NA & gt; 5 1 question 5> NA & gt; 6 1 question 6> NA & gt; 7 1 Question 7 0.352078239608802 8 1 Questions 8 3.04180471049169 9 1 Questions 9 3. 9 5590 9 99 99 10 10 1 Question 10 & gt; NA & gt; 11 1 Questions 1 1 12 1 Question 12 4.265 9 12 9 6778731 13 1 Questions 13 3.95256943635 996 14 1 Questions 14 0.46568627450 9 804 15 1 Questions 15 2.61764705882353 16 1 Questions 16 1.83333333333333 17 1 Question 17 & gt; NA & gt; 18 1 Question 18 0.225490196078431192 Question 1 3.85714285714286 20 2 Questions2 4 & gt; Ggplot (mydf, aes (question, firm, fill = value)) + geom_tile () + theme (axis.text.x = element_text (angle = 330, hjust = 0))  

Link to the picture of my current plot.

The root cause of your problem seems to be that value is not a numerical vector The factor I guess is based on the fact that head () output NA values ​​ & lt; NA & gt; , which I think was written in your original spreadsheet, but there is no default behavior for R. The image you link to is the default behavior of ggplot for coloring on a factor; Default coloring for numeric is very close to what you want.

You can check that the case is actually using the class $ mydf $ Value . If this is actually a factor, then convert it to numeric with the following:

  mydf $ value & lt; -a.numeric (as.character (mydf $ value))  

Your conspiracy in writing will now return a graph that looks like this:

You can use, or add, with precise visualization using.

For your second question, it is quite easy to redirect that order. Optimized from:

  mydf $ question & lt; - factor (mydf $ question, level (mydf $ question) [c (1,10: 18,2: 9)])  

No comments:

Post a Comment