2015-10-14 81 views
0

我想知道是否有從一張表生成較小條形圖的有效方法。我的目標是生成乾淨的可用圖形,而不是一張難以閱讀的稠密圖形。有沒有一種方法可以做到這一點,而無需編碼。源表格位於數據框對象類型中。從一個精簡版創建多個條形圖

+0

它會幫助很多,如果你可以發佈[示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example),但看看[在此頁](http://docs.ggplot2.org/0.9.3.1/geom_bar.html)在'ggplot'中的一些選項。方面特別可能是你正在尋找的。 – Axeman

回答

1

這裏有四個不同的情節,也許其中一個是你喜歡的。

library(ggplot2) # plotting and the diamonds data set 
library(dplyr) # needed for the filter function 


# Unwanted 'dense' graph 
g1 <- 
    ggplot(diamonds) + 
    aes(x = cut, fill = color) + 
    geom_bar() + 
    ggtitle("g1: stacked bar plot") 

enter image description here

# or 
g2 <- 
    ggplot(diamonds) + 
    aes(x = cut, fill = color) + 
    geom_bar(position = position_dodge()) + 
    ggtitle("g2: dodged bar plot") 

enter image description here

# different option, layered bars 
g3 <- 
    ggplot() + 
    aes(x = cut, fill = color) + 
    geom_bar(data = filter(diamonds, color == "D"), width = 0.90) + 
    geom_bar(data = filter(diamonds, color == "E"), width = 0.77) + 
    geom_bar(data = filter(diamonds, color == "F"), width = 0.63) + 
    geom_bar(data = filter(diamonds, color == "G"), width = 0.50) + 
    geom_bar(data = filter(diamonds, color == "H"), width = 0.37) + 
    geom_bar(data = filter(diamonds, color == "I"), width = 0.23) + 
    geom_bar(data = filter(diamonds, color == "J"), width = 0.10) + 
    ggtitle("g3: overlaid bar plot") 

enter image description here

# facet plot 
g4 <- 
    ggplot(diamonds) + 
    aes(x = cut) + 
    geom_bar() + 
    facet_wrap(~ color) 

enter image description here