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 :
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 :
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.