ggplot中的facet以相同的数据格式给出不同的结果

时间:2014-08-03 02:08:05

标签: r ggplot2 facet

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)

带有三个不同箱形图的刻面图在“14”部分出现奇怪的对齐和三个框,而不是2.当我仅将时间“14”分组然后制作一个箱形图时,该图显示为集中在一起,只有两个治疗水平的盒子。

我有6个其他数据集,其格式与此相同,当我在其上运行代码的facetted ggplot部分时,所有的箱形图都在变量名称上面有居中框,每个绘图有两个。我已经查看了所有文件的str()以及dput(),并且没有看到数据格式的任何差异。我还重新输入了所有原始数据并将其保存到新的.csv文件中,但问题仍然存在。

1 个答案:

答案 0 :(得分:2)

这是因为您指的是day1$shrubaes()内的列,您永远不应该这样做。在任何文档或任何(可靠的)ggplot2指南中都没有类似于这种语法风格的内容。

试试这个:

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