Invalid layers for facet_grid in ggplot2

2019-06-24 03:35发布

问题:

I've been working with a variation on this code:

library(ggplot2)

Compare = matrix(c(
0,           0,           "Soil 1", "tF",
0.379268025, 5.555806214, "Soil 1", "tF",
0.961561989, 13.05580621, "Soil 1", "tF",
1.55418685,  20.55580621, "Soil 1", "tF",
0,           0,           "Soil 2", "tF",
0.104297312, 1.581249799, "Soil 2", "tF",
0.705818262, 6.081249799, "Soil 2", "tF",
1.447379092, 10.5812498,  "Soil 2", "tF",
0,           20,          "Soil 1", "tf",
0.379268025, 13.1603086,  "Soil 1", "tf",
0.961561989, 12.72354396, "Soil 1", "tf",
1.55418685,  12.60549558, "Soil 1", "tf",
0,           20,          "Soil 2", "tf",
0.104297312, 10.51383279, "Soil 2", "tf",
0.705818262, 6.433709727, "Soil 2", "tf",
1.447379092, 5.82398083,  "Soil 2", "tf",
0,           0,           "Soil 1", "zf",
0.379268025, 205.7706005, "Soil 1", "zf",
0.961561989, 483.5483783, "Soil 1", "zf",
1.55418685,  761.3261561, "Soil 1", "zf",
0,           0,           "Soil 2", "zf",
0.104297312, 23.25367352, "Soil 2", "zf",
0.705818262, 89.43014411, "Soil 2", "zf",
1.447379092, 155.6066147, "Soil 2", "zf"), nrow = 24, ncol = 4, byrow = TRUE)

plot = ggplot(as.data.frame(Compare),
aes(as.double(Compare[,1]), as.double(Compare[,2]), color = Compare[,3])) +
    geom_point() + facet_grid(Compare[,4] ~ .)
plot

My issue is with the facet_grid() aspect of the code. If I comment it out or delete it, it runs great, so I know I can isolate my issue to something that has to do with it. What I'm trying to accomplish is a set of three panels stacked vertically, with Compare[,1] on the x-axis, Compare[,2] on the y-axis, coloration based on Compare[,3] (producing a set of points for both soil types on each facet), and the three facets produced according to Compare[,4].

The error I get when I run with facet_grid() included in the code is:

Error in layout_base(data, rows, drop = drop) : 
    At least one layer must contain all variables used for facetting

I feel like my error may be related to the forcing of doubles and dataframes to deal with the initial form of a matrix, but am not sure what needs to be changed.

回答1:

Your issues are more far-reaching than just the facet_grid component.

  1. Matrices can only hold a single type of data. If you have multiple types, use a data.frame.
  2. You as.double code is not correct for coercing from factor to numeric (as required because you had a character matrix coerced --> data.frame.
  3. ggplot and aes should refer to the column names, not the direct subsets of the data object using [.
  4. facet_grid is even more particular required names, not even functions thereof.

So, to rescue your data.

CP <- as.data.frame(Compare)

CP[[1]] <- as.numeric(as.character(CP[[1]]))
CP[[2]] <- as.numeric(as.character(CP[[2]]))
# your data.frame names are..
names(CP)
# [1] "V1" "V2" "V3" "V4"

ggplot(CP, aes(x = V1, y = V2, colour = V3)) +
 geom_point() + facet_grid(V4 ~ . )



标签: r ggplot2