【问题标题】:Facet in ggplot giving differing results with same data formatggplot 中的 Facet 使用相同的数据格式给出不同的结果
【发布时间】:2014-08-03 02:08:04
【问题描述】:
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 个与这个格式相同的其他数据集,当我在它们上运行分面的 ggplot 部分代码时,所有的箱线图都在变量名称上带有居中的框,每个图两个。我查看了所有文件的 str() 以及 dput() 并没有发现数据格式有任何差异。我还重新输入了所有原始数据并将其保存到新的 .csv 文件中,但问题仍然存在。

【问题讨论】:

    标签: r ggplot2 facet


    【解决方案1】:

    这是因为您在 aes() 内部通过 day1$shrub 引用列,您永远不应该这样做。在现有的任何文档或任何(可信的)ggplot2 指南中都没有出现类似这种语法风格的东西。

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2020-01-13
      • 2017-05-21
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      相关资源
      最近更新 更多