2013-07-30 42 views
-3

我有一個表以下數據重新排序的傳奇物品GGPLOT2

marks cut but xut 
1 49 51 67 
2 53 47 76 
3 54 46 67 
4 54 46 56 
5 55 45 65 
6 55 45 75 
7 55 45 45 
8 55 45 33 
9 55 45 43 
10 56 45 53 
11 56 45 23 
12 56 44 78 
13 56 44 45 

當我繪製的圖表,我得到得到傳說的那樣切斷,但XUT,我想傳說是爲XUT但並即切我想重新排列傳說和在whichI需要

下面

的方式呈現它們是我已經實現

install.packages("plyr") 
install.packages("ggplot2") 
install.packages("reshape2") 
library("plyr") 
library("reshape2") 
library("ggplot2") 

data=read.csv("data.csv") 
attach(data) 
data$marks <- factor(data$marks, levels 
= data$marks[order(data$cut)]) 

c.data=melt(data, id.var="marks") 
n.data = ddply(c.data,.(marks), transform, pos = cumsum(value) - 0.5*value) 

n.data <- transform(n.data,variable = factor(levels = c("xut", "but", "cut"))) 

plot = ggplot(d.data, aes(x = marks, y = value)) + geom_bar(stat = "identity",mapping = aes(x = value, fill = variable)) + scale_y_continuous(breaks=seq(0,100, by = 10))+geom_text(aes(label = value, y = pos), size = 3, face="bold", colour="white") + scale_fill_manual(values=c("455555","333333","335566")) + theme(axis.line = element_line(),axis.text.x=element_text(angle=60,hjust=1,colour="white"),axis.text.y=element_text(colour="white"),axis.title.x = element_blank(),axis.title.y = element_blank(),panel.background = element_blank(),axis.ticks=element_blank()) + labs(fill="")+coord_cartesian(ylim=c(0,100)) + theme(legend.position = "bottom", legend.direction = "horizontal") 
+0

我不明白你的問題描述。當我運行你的代碼時,我會得到一個列出'a'和'b'的圖例。爲什麼圖例中會出現「標記」級別?我不關注... – joran

+0

對不起,請現在檢查代碼 – Zack

+0

這只是一個演示代碼,我有他們想要的多個列表 – Zack

回答

4

重新排序的代碼與所述組中的變量的在長的水平或梅爾特德版本的數據。例如,使用數據

foo <- read.table(text="marks cut but xut 
1 49 51 67 
2 53 47 76 
3 54 46 67 
4 54 46 56 
5 55 45 65 
6 55 45 75 
7 55 45 45 
8 55 45 33 
9 55 45 43 
10 56 45 53 
11 56 45 23 
12 56 44 78 
13 56 44 45", header = TRUE) 

熔體它爲合適的格式

require(reshape2) 
require(ggplot2) 
bar <- melt(foo, id = "marks") 

> head(bar) 
    marks variable value 
1  1  cut 49 
2  2  cut 53 
3  3  cut 54 
4  4  cut 54 
5  5  cut 55 
6  6  cut 55 

然後設置在包含該組的variable因子水平標籤

bar <- transform(bar, 
       variable = factor(variable, levels = c("xut", "but", "cut"))) 

然後繪製

ggplot(bar) + geom_bar(mapping = aes(x = value, fill = variable)) 

因爲你沒有顯示任何密謀代碼我猜你的實際情節代碼的樣子,但如上所示,至少是你想要的順序...

+0

eval(expr,envir,enclos)中的錯誤:找不到對象'pos' 這是我正面臨的錯誤 – Zack

+1

@Zack *顯示您的代碼* !你已經編輯了這方面的任何有用的東西,修正你的問題,我會仔細看看,不可能說出你在沒有它的情況下搞砸了什麼, –

+0

請檢查我需要刪除的代碼,只要你看到代碼 – Zack