2012-04-10 75 views
4

我使用下面的繪製如下圖:僅顯示正在使用的標籤?

data <- structure(list(Type1 = c("DB", "DB", "DB", "DB", "DB", "DB", 
"DB", "DB", "DB", "DB", "DB", "DB", "DB", "Manual", "Manual", 
"Manual", "Manual", "Manual", "Manual", "Manual", "Manual", "Manual", 
"Manual", "Manual", "Manual", "Manual", "Manual", "Manual"), 
    Type2 = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
    3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
    3L), Category = c("A", "B", "C", "D", "E", "A", "B", "F", 
    "G", "A", "B", "C", "H", "I", "J", "K", "L", "M", "O", "J", 
    "P", "K", "Q", "M", "K", "P", "J", "P"), Percent = c("83.5106383", 
    "9.574468085", "5.85106383", "0.531914894", "1/188*100", 
    "85.24590164", "11.47540984", "1/61*100", "1.639344262", 
    "90", "3.333333333", "3.333333333", "3.333333333", "20.10582011", 
    "10.05291005", "6.349206349", "5.82010582", "4.761904762", 
    "31.14754098", "16.39344262", "6.557377049", "6.557377049", 
    "4.918032787", "30", "23.33333333", "16.66666667", "10", 
    "6.666666667")), .Names = c("Type1", "Type2", "Category", 
"Percent"), row.names = c(NA, -28L), class = "data.frame") 

data$Percent <- as.numeric(data$Percent) 

g= ggplot(data, aes(x=Category, y=Percent)) + 
      geom_bar(width=0.8, stat="identity", position=position_dodge()) + 
      facet_grid(Type1 ~ Type2,) + 
      theme_bw() + 
      coord_flip() + 
      scale_y_continuous(limits=c(0,100)) 

print(g) 

enter image description here

有什麼辦法,我可以積等taht每行僅使用標籤是非零?例如,採取DB行。它只使用6個標籤,但顯示所有16個標籤,因爲其他10個正被Manual部分數據使用。對於Manual行也是如此。我正在尋找的是這樣的:

 1   2    3 
H 
G 
D          DB 
C 
B 
A 

Q 
P 
O 
G          MANUAL 
M 
L 
K 
J 
I 

有關如何做到這一點的任何建議?

+1

我這個玩周圍的位(後我固定在你的榜樣數據的問題),我開始認爲facet_grid和scales參數可能存在一些問題。但是,您也可以使用facet_wrap,而且似乎可行。 – joran 2012-04-10 03:32:22

+0

@joran:+1謝謝你的時間。我也玩過,我可以確認有一個錯誤。我會盡快報告。 – Legend 2012-04-12 19:46:14

回答

2

似乎有結合facet_grid(scales="free")coord_flip()時是一個錯誤或意外行爲。

這裏有兩個可能的變通:

library(ggplot2) 

# Removed quotes from Percent values, so that Percent will be numeric. 
dat <- structure(list(Type1 = c("DB", "DB", "DB", "DB", "DB", "DB", 
"DB", "DB", "DB", "DB", "DB", "DB", "DB", "Manual", "Manual", 
"Manual", "Manual", "Manual", "Manual", "Manual", "Manual", "Manual", 
"Manual", "Manual", "Manual", "Manual", "Manual", "Manual"), 
    Type2 = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
    3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
    3L), Category = c("A", "B", "C", "D", "E", "A", "B", "F", 
    "G", "A", "B", "C", "H", "I", "J", "K", "L", "M", "O", "J", 
    "P", "K", "Q", "M", "K", "P", "J", "P"), Percent = c(83.5106383, 
    9.574468085, 5.85106383, 0.531914894, 1/188*100, 
    85.24590164, 11.47540984, 1/61*100, 1.639344262, 
    90, 3.333333333, 3.333333333, 3.333333333, 20.10582011, 
    10.05291005, 6.349206349, 5.82010582, 4.761904762, 
    31.14754098, 16.39344262, 6.557377049, 6.557377049, 
    4.918032787, 30, 23.33333333, 16.66666667, 10, 
    6.666666667)), .Names = c("Type1", "Type2", "Category", 
"Percent"), row.names = c(NA, -28L), class = "data.frame") 

figure_1 = ggplot(dat, aes(x=Category, y=Percent)) + 
      geom_bar(width=0.8, stat="identity") + 
      facet_grid(Type2 ~ Type1, scales="free_x") + 
      theme_bw() + 
      scale_y_continuous(limits=c(0, 100)) + 
      opts(title="Figure 1. Success!\n(But Rotated 90 Degrees)") 

figure_2 = ggplot(dat, aes(x=Percent, y=Category)) + 
      geom_point(size=3) + 
      facet_grid(Type1 ~ Type2, scales="free_y") + 
      theme_bw() + 
      scale_x_continuous(limits=c(0, 100)) + 
      opts(title="Figure 2. Success!\n(But Dotplot Instead Of Barplot)") 

# Unexpected interaction between scales="free" and coord_flip()? 
figure_3 = ggplot(dat, aes(x=Category, y=Percent)) + 
      geom_bar(width=0.8, stat="identity") + 
      facet_grid(Type1 ~ Type2, scales="free") + 
      theme_bw() + 
      scale_y_continuous(limits=c(0, 100)) + 
      coord_flip() + 
      opts(title="Figure 3. Strange Y-axis Behavior.") 

enter image description here enter image description here enter image description here

3

它只會刪除那些不在任何方面的元素。但是您可以在facet_grid()中使用scales =「free」參數。

g + facet_grid(Type1 ~ Type2, scales="free") 

但我認爲facet_wrap,可能會爲您提供更合適的可視化:

ggplot(data, aes(x=Category, y=Percent)) + 
    geom_bar(width=0.8, stat="identity", position=position_dodge()) + 
    facet_wrap(Type1 ~ Type2,scales="free") + 
    theme_bw() + 
    scale_y_continuous(c(0,100)) + 
    coord_flip() + 
    opts() 

enter image description here