0

Consider these 3 variables : A [100*1], B [100*1], and M [100*1]. Variable M has 7 classes. For each class, I would like to plot a raster like :

ggplot(data = test, aes(x = factor(A), y = factor(B), fill = M)) +
       xlab("A") + ylab("B") +
       geom_raster()

So, for each class, I can do it properly :

enter image description here

in the second step, I would like to plot all 7 classes as 7 subplots in one column using facet_wrap function. So, I generated a data.frame where I rbind A, B, and M of each class. I added a column to the data.frame as the ID of each class. the data frame df looks like this (I removed intermediate rows of each class to keep the data frame short) :

row A   B   M   ID
1   50  5   0.272727272727273   1
2   50  10  0.352941176470588   1
97  2000    200 0.75    1
98  2000    300 0.692307692307692   1
99  2000    500 0.692307692307692   1
100 2000    1000    0.444444444444444   1
1   50  5   0.199117609627896   2
2   50  10  0.176712023248457   2
98  2000    300 0.059602649006623   2
99  2000    500 0.101954497349775   2
100 2000    1000    0.12773219045144    2
1   50  5   0.864866760389603   3
2   50  10  0.88297043479882    3
3   50  20  0.904572710950147   3
98  2000    300 0.96047443246472    3
99  2000    500 0.96047443246472    3
100 2000    1000    0.925079933067989   3
1   50  5   1   4
2   50  10  1   4
96  2000    150 1   4
97  2000    200 1   4
98  2000    300 1   4
99  2000    500 1   4
100 2000    1000    1   4
1   50  5   0.907284768211923   5
2   50  10  0.921192052980132   5
3   50  20  0.942384105960265   5
98  2000    300 0.987417218543044   5
99  2000    500 0.974172185430469   5
100 2000    1000    0.951655629139076   5
1   50  5   6.875   6
2   50  10  6.75    6
3   50  20  6.5 6
97  2000    200 0   6
98  2000    300 -0.625  6
99  2000    500 -6.375  6
100 2000    1000    -7  6
1   50  5   -489.141    7
2   50  10  -446.8695   7
3   50  20  -335.402    7
4   50  50  -208.469    7
96  2000    150 3.492   7
97  2000    200 7.056   7
98  2000    300 11.177  7
99  2000    500 12.827  7
100 2000    1000    14.487  7

To plot the 7 rasters I used this :

ggplot(data = df, aes(x = factor(A), y = factor(B), fill = M)) +
          xlab("A") + ylab("B") +
          geom_raster() + 
          facet_wrap(~ID, ncol = 1)

What it plots, looks like this :

enter image description here

that is not correct. It seems it considers all 7 facets as one ratser. I could not find the problem. I would appreciate your help.

Basilique
  • 150
  • 1
  • 11
  • 1
    I'm not sure what you mean that it considers the facets as one raster. If you print or save the image out larger, you'll be able to see each facet more clearly – camille Apr 20 '19 at 20:25
  • No, the first three are all black. It is not the matter of size of the image. since in fact, M varies between different ranges for different ID. – Basilique Apr 20 '19 at 22:01
  • 1
    @Reyhaneh You have already noted in your own comment that M varies between different ranges. In your first plot, the range of fill values is somewhere in the region of `0.4, 1`. In your second plot, the range of fill values is around (-400, 0)--with that scale, a range of values in `0.4, 1` *would* be indistinguishably black. Do you want a separate scale for each facet? – Z.Lin Apr 21 '19 at 02:58
  • Yes, exactly that’s what I want. How can i do so ? – Basilique Apr 21 '19 at 06:24
  • 1
    If you want a different M scale for each facet, it might be best to plot each ID raster seperately, convert them to grobs with `ggplotGrob()`, after which you `rbind(..., size = "last)` them. You can suppress the x-axis labels, text and titles in `theme()` for the first 6 plots. The alternative is to rescale the M values for each ID to a common range. – teunbrand Apr 21 '19 at 07:39
  • thanks, would you please post your solution as an answer? – Basilique Apr 21 '19 at 07:48

0 Answers0