2016-04-09 44 views
-1

我知道,這真的是一個很簡單的問題,我很抱歉,我知道使用GGPLOT2複雜的矩陣,但是當我這種類型的信息,我沒有辦法來顯示它很好,就像我爲別人做的那樣。難點顯示簡單的價值ggplot

如何ggplot K +

編輯:沒有得到錯誤,但只是一個空洞的情節

k=structure(list(`11` = 76.2025316455696, `16` = 0.907172995780591, 
     `17` = 84.3670886075949, `12` = 19.957805907173, `15` = 5.44303797468354, 
     `13` = 5.44303797468354, `14` = 19.0506329113924, `18` = 3.62869198312236), .Names = c("11", 
    "16", "17", "12", "15", "13", "14", "18"), row.names = c(NA, 
    -1L), class = "data.frame") 

編輯:

Ineed使用ggplot以指定顏色爲每個類別如下

cat.colors <- brewer.pal(8, "Set1") # assign a color for each level of your factor variable Sous_Categorie 
names(cat.colors) <- c("11","12","13","14","15","16","17","18") 
label=c("cat1","cat2","cat3","cat4","cat5","cat6","cat7","cat8") 
mycolourscale <- scale_fill_manual(name = "Catégories",values = cat.colors, breaks=1:8, labels=as.character(label)) 
+0

什麼代碼產生的錯誤?你剛纔給我們的數據... – Spacedman

+0

ggplot(k),我需要精確的X和Y?如何將它轉換爲ggplot?謝謝。 – ranell

+0

使用該'k','ggplot(k)'不會生成關於「類矩陣」的錯誤消息。你做了什麼? – Spacedman

回答

1

我想你只需要重塑你的數據位,以使這項工作(k是從dput):

library(ggplot2) 
library(RColorBrewer) 
k = data.frame(X = names(k), Y = t(k)) #reshape your data 
#then define the colour scale as you provided 
cat.colors <- brewer.pal(8, "Set1") # assign a color for each level of your factor variable 
names(cat.colors) <- c("11","12","13","14","15","16","17","18") 
label=c("cat1","cat2","cat3","cat4","cat5","cat6","cat7","cat8") 
mycolourscale <- scale_fill_manual(name = "Catégories", 
values = cat.colors, breaks=1:8, labels=as.character(label)) 
#make the plot 
ggplot(k, aes(X, Y))+geom_bar(stat = 'identity', aes(fill = X))+ 
    mycolourscale 

導致: enter image description here

如果你想有一個傳奇的標籤(但相同的調色板):

ggplot(k, aes(X, Y))+geom_bar(stat = 'identity', aes(fill = X))+ 
    scale_fill_brewer(palette = 'Set1')+theme_bw() 

enter image description here

+0

非常感謝! – ranell