2014-02-27 23 views
0

我正在製作一個堆積面積圖表,但並不完全在那裏。我已經跟隨以前的帖子,並嘗試了許多相同代碼的迭代,結果相同或更差。這是最成功的: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

我沒有任何問題,使它成爲一個堆疊的barplot,但我想多年來持續展示它。我不認爲這會很難。

而且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") 
+0

請提供'dput(trtmeans)輸出'在它轉換爲數值型變量編輯你的問題。 – Thomas

+0

@joran:情況並非如此:'geom_area'確實需要'y'。 –

+0

@DavidRobinson Hrm。你是對的。該文件似乎在這一點上具有誤導性。 – joran

回答

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 
0

你需要每X /填充/小組合,至少有兩點。試想一下:

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點各種填充/小值。

+0

我的完整數據集確實有額外的年限,但是我沒有用相同的代碼得到這種輸出。 – Nazer