Boogi
Boogi

Reputation: 153

Facet in ggplot giving differing results with same data format

day1 <- structure(list(shrub = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("c","s", ""), class = "factor"), Plot_time = c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,19L, 19L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L), Lp = c(3, 0.5, 0.5, 2, 4, 4, 0.5, 0.5, 1, 1, 1, 0.5, 2, 0.5, 0.5, 0.75, 3.5, 1.5, 1.5, 0.5, 0.5, 1, 1, 2, 1, 0.5, 1, 1, 1.5, 0.5, 1, 0.5, 1, 1.5, 0.5, 4.5, 0.5, 0.5, 1, 1, 3, 1, 2, 1, 1.5, 1.5, 0.5, 0.5, 0.5, 0.75, 2, 1.5, 2, 1, 0.5, 0.5, 0.5, 0.5, 2.5, 1.75, 1, 1.75, 0.5, 1, 1.5, 1.5, 1.5, 1.5, 3), day = c(1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("shrub", "Plot_time", "Lp", "day"), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "37","38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48","49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71"), class = "data.frame")
#Make faceted ggplot boxplot comparing Lp by shrub treatment level
ggplot(data = day1,aes(x=(day1$shrub),y=(day1$Lp), colour = shrub))+geom_boxplot(size = 1)+scale_shape(solid = FALSE)+facet_grid(day~Plot_time)


#subset data that is not aligned in above plot and plot it separately
p14 = subset(day1, Plot_time == "19")
ggplot(data = p14,aes(x=(p14$shrub),y=(p14$Lp), colour = shrub))+geom_boxplot(size = 1)+scale_shape(solid = FALSE)

The facetted plot with three different boxplots in it comes out with strange alignment and three boxes in the "14" section, not 2. When I subset out only the time "14" and then make a boxplot, the plot appears to be centered, and has only the two boxes for the treatment levels.

I have 6 other data sets with the same format as this one, and when I run the facetted ggplot part of the code on them, all of the boxplots come out with centered boxes over the variable names, two to each plot. I have looked at str() for all the files as well as dput() and have not seen any difference in the data formats. I also re-entered all of the raw data and saved it to a new .csv file but the problem persists.

Upvotes: 1

Views: 59

Answers (1)

joran
joran

Reputation: 173517

It is because you are referring to columns via day1$shrub inside of aes() which you should never, ever do. Nothing resembling that style of syntax ever occurs in any of the documentation or any (credible) ggplot2 guide in existance.

Try this:

ggplot(data = day1,aes(x=shrub,y=Lp, colour = shrub)) + 
  geom_boxplot(size = 1) + 
  scale_shape(solid = FALSE) + 
  facet_grid(day~Plot_time)

Upvotes: 2

Related Questions