Ggplot2,我的数据在哪里?

时间:2014-02-27 17:07:20

标签: r ggplot2

我正在制作一个堆积区域图表,但我不是那里。我已经关注了之前的帖子并尝试了相同代码的多次迭代,结果相同或更差。这是最成功的:

ggplot(trtmeans, aes(x = year, y = dist, fill = factor(point))) + geom_area(position = 'stack')+ facet_wrap(~ trt)

以下是我的数据框的负责人:

   year trt  point     dist
  2009  C2   2.5  0.07819708
  2009  C2    10  0.24723689
  2009  C2  22.5  0.17690575
  2009  C2    45  0.28355538
  2009  C2    80  0.21410490
  2009  CC   2.5  0.09657976

以及指向文本文件的链接:https://www.dropbox.com/s/2m1bj970jj8rj7f/stackedplot.txt

以下是结果:

enter image description here

我没有问题把它变成堆积的条形图,但我想多年来连续展示它。我认为这不会那么难。

dput(head(trtmeans))

structure(list(year = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2008", 
"2009", "2010", "2011", "2012", "2013", "(all)"), class = "factor"), 
    trt = structure(c(1L, 1L, 1L, 1L, 1L, 2L), .Label = c("C2", 
    "CC", "CCW", "P", "PF", "S2", "(all)"), class = "factor"), 
    depth = structure(c(1L, 2L, 3L, 4L, 5L, 1L), .Label = c("5", 
    "15", "30", "60", "100", "(all)"), class = "factor"), point = structure(c(5L, 
    4L, 3L, 2L, 1L, 5L), .Label = c("80", "45", "22.5", "10", 
    "2.5"), class = c("ordered", "factor")), mass = c(0.039049865125, 
    0.1215071878125, 0.084692273125, 0.147087857875, 0.110562685125, 
    0.0798292900625), coresum = c(0.5028998690625, 0.5028998690625, 
    0.5028998690625, 0.5028998690625, 0.5028998690625, 0.774738473875
    ), dist = c(0.0781970752917714, 0.247236893004015, 0.176905750464174, 
    0.283555383459544, 0.214104897780495, 0.0965797557709145)), .Names = c("year", 
"trt", "depth", "point", "mass", "coresum", "dist"), row.names = c(68L, 
70L, 72L, 74L, 76L, 79L), class = "data.frame")

2 个答案:

答案 0 :(得分:2)

您的year是一个因素,而非数字变量,这意味着geom_area地块未连接相邻年份。使用以下命令将其转换为数字变量:

trtmeans$year = as.numeric(as.character(trtmeans$year))

year是一个因素,原因在于它在原始文件中的引号:

"year" "trt" "depth" "point" "mass" "coresum" "dist"
"2009" "C2" "5" "2.5" 0.039049865125 0.5028998690625 0.0781970752917714
"2009" "C2" "15" "10" 0.1215071878125 0.5028998690625 0.247236893004015
"2009" "C2" "30" "22.5" 0.084692273125 0.5028998690625 0.176905750464174

答案 1 :(得分:0)

每x / fill / facet组合至少需要两个点。考虑:

trtmeans <- read.table(h=T, text="   year trt  point     dist
  2009  C2   2.5  0.07819708
  2010  C2   2.5  0.07819708
  2009  C2    10  0.24723689
  2010  C2    10  0.24723689
  2010  C2  22.5  0.17690575
  2009  C2  22.5  0.17690575
  2009  C2    45  0.28355538
  2010  C2    45  0.21410490
  2009  CC   2.5  0.09657976
  2010  CC   2.5  0.09657976")

ggplot(trtmeans, aes(x = year, y = dist, fill=factor(point))) + 
  geom_area(position = 'stack') + 
  facet_wrap(~ trt)

enter image description here

我所做的只是为各种填充/方面值添加2010点。