2014-03-07 52 views
3

我有兩種類型,看起來像這樣的數據: 1型(http://dpaste.com/1697615/plain/爲什麼GGPLOT2餅圖方面混淆了小標籤

Cluster-6 abTcells 1456.74119 
Cluster-6 Macrophages 5656.38478 
Cluster-6 Monocytes 4415.69078 
Cluster-6 StemCells 1752.11026 
Cluster-6 Bcells 1869.37056 
Cluster-6 gdTCells 1511.35291 
Cluster-6 NKCells 1412.61504 
Cluster-6 DendriticCells 3326.87741 
Cluster-6 StromalCells 2008.20603 
Cluster-6 Neutrophils 12867.50224 
Cluster-3 abTcells 471.67118 
Cluster-3 Macrophages 1000.98164 
Cluster-3 Monocytes 712.92273 
Cluster-3 StemCells 557.88648 
Cluster-3 Bcells 599.94109 
Cluster-3 gdTCells 492.61994 
Cluster-3 NKCells 524.42522 
Cluster-3 DendriticCells 647.28811 
Cluster-3 StromalCells 876.27875 
Cluster-3 Neutrophils 1025.24105 

和類型二,(http://dpaste.com/1697602/plain/)。與類型1的數據處理該代碼時

abTcells 1456.74119 
Macrophages 5656.38478 
Monocytes 4415.69078 
StemCells 1752.11026 
Bcells 1869.37056 
gdTCells 1511.35291 
NKCells 1412.61504 
DendriticCells 3326.87741 
StromalCells 2008.20603 
Neutrophils 12867.50224 

但是爲什麼:

library(ggplot2); 
library(RColorBrewer); 
filcol <- brewer.pal(10, "Set3") 
dat <- read.table("http://dpaste.com/1697615/plain/") 
ggplot(dat,aes(x=factor(1),y=dat$V3,fill=dat$V2))+ 
    facet_wrap(~V1)+ 
    xlab("") + 
    ylab("") + 
    geom_bar(width=1,stat="identity",position = "fill") + 
    scale_fill_manual(values = filcol,guide = guide_legend(title = "")) + 
    coord_polar(theta="y")+ 
    theme(strip.text.x = element_text(size = 8, colour = "black", angle = 0)) 

就緒數據:

> dput(dat) 
structure(list(V1 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Cluster-3", 
"Cluster-6"), class = "factor"), V2 = structure(c(1L, 5L, 6L, 
9L, 2L, 4L, 8L, 3L, 10L, 7L, 1L, 5L, 6L, 9L, 2L, 4L, 8L, 3L, 
10L, 7L), .Label = c("abTcells", "Bcells", "DendriticCells", 
"gdTCells", "Macrophages", "Monocytes", "Neutrophils", "NKCells", 
"StemCells", "StromalCells"), class = "factor"), V3 = c(1456.74119, 
5656.38478, 4415.69078, 1752.11026, 1869.37056, 1511.35291, 1412.61504, 
3326.87741, 2008.20603, 12867.50224, 471.67118, 1000.98164, 712.92273, 
557.88648, 599.94109, 492.61994, 524.42522, 647.28811, 876.27875, 
1025.24105)), .Names = c("V1", "V2", "V3"), class = "data.frame", row.names = c(NA, 
-20L)) 

這些值在1型與集羣-6相同的上述生成以下數字: enter image description here

請注意,Facet標籤放錯位置,Cluster-3應爲Cluster-6, ,其中嗜中性粒細胞的比例較大。

我該如何解決問題?

處理類型2數據時完全沒有問題。

library(ggplot2)     
df <- read.table("http://dpaste.com/1697602/plain/"); 
library(RColorBrewer); 
filcol <- brewer.pal(10, "Set3") 

ggplot(df,aes(x=factor(1),y=V2,fill=V1))+ 
    geom_bar(width=1,stat="identity")+coord_polar(theta="y")+ 
    theme(axis.title = element_blank())+ 
    scale_fill_manual(values = filcol,guide = guide_legend(title = "")) + 
     theme(strip.text.x = element_text(size = 8, colour = "black", angle = 0)) 

就緒數據:

> dput(df) 
structure(list(V1 = structure(c(1L, 5L, 6L, 9L, 2L, 4L, 8L, 3L, 
10L, 7L), .Label = c("abTcells", "Bcells", "DendriticCells", 
"gdTCells", "Macrophages", "Monocytes", "Neutrophils", "NKCells", 
"StemCells", "StromalCells"), class = "factor"), V2 = c(1456.74119, 
5656.38478, 4415.69078, 1752.11026, 1869.37056, 1511.35291, 1412.61504, 
3326.87741, 2008.20603, 12867.50224)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, 
-10L)) 

enter image description here

+0

你能寫出你的數據,以便隨時複製/粘貼到R? –

+0

我所有的代碼都很容易複製粘貼以生成上面的圖形。 – pdubois

+0

您的繪圖命令很好,但數據不容易放入工作區。請參閱http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example關於如何改進的一些提示。 –

回答

4

這是因爲你在aes(...)使用數據幀的名字。這解決了這個問題。

ggplot(dat,aes(x=factor(1),y=V3,fill=V2))+ 
    facet_wrap(~V1)+ 
    xlab("") + 
    ylab("") + 
    geom_bar(width=1,stat="identity",position = "fill") + 
    scale_fill_manual(values = filcol,guide = guide_legend(title = "")) + 
    coord_polar(theta="y")+ 
    theme(strip.text.x = element_text(size = 8, colour = "black", angle = 0)) 

在定義方面,可以通過電平在默認數據集的上下文中引用V1,和ggplot排序字母(因此「羣集-3」至上)。在您撥打aes(...)時,您直接參考dat$V3,因此ggplot將從默認數據集的上下文中跳出到原始數據幀。在那裏,Cluster-6是第一個。

作爲一般評論,應該在aes(...)從未參考數據外側data=...定義的數據集的上下文中。所以:

ggplot(data=dat, aes(y=V3...))  # good 
ggplot(data=dat, aes(y=dat$V3...)) # bad 

你的問題是爲什麼第二個選項不好的完美例子。

+0

對不起,爲什麼我不能用您的代碼重現您的結果.. – pdubois

+0

因爲我粘貼了錯誤的代碼...抱歉 - 編輯應該修復它。 – jlhoward