2013-05-15 43 views
0

以前用於完美工作的舊代碼不再適用於0.9.3。這個問題與使用小平面,自由尺度和座標翻轉有關。ggplot 0.9.3 issue with facet_wrap,free scales and coord_flip - 第二次嘗試

這裏是重現的方式:

數據集:d.csv:

"Priority","Owner","Project" 
"Medium","owner7","Team4" 
"Medium","owner1","Team1" 
"Low","","Team3" 
"High","owner6","Team3" 
"Medium","","Team4" 
"Medium","owner3","Team1" 
"Medium","owner2","Team1" 
"Medium","owner5","Team2" 
"Low","owner4","Team2" 
"Critical","","Team2" 
"Medium","owner2","Team1" 
"High","","Team4" 

代碼:

data <- read.csv(file="d.csv",head=TRUE) 
attach(data) 

p3 <- ggplot(data,aes(x=Owner,fill=Priority))+ 
geom_bar(aes(y=..count..)) + 
facet_wrap(~ Project, nrow=2, scales="free") + 
opts(legend.position="none") 

這將創建一個層面的情節,但我需要翻轉軸。以前,添加一個coord_flip()來做到這一點,但現在新的ggplot不允許一起使用自由縮放和coord_flip。有沒有其他的方式來轉動方面的軸?自由秤對我很重要。感謝任何指針。

回答

0

這似乎是你要求的(如果我正確地理解了這個問題)之前已經提交給開發者,他們不會實現它。在這裏看到:

https://github.com/hadley/ggplot2/issues/95

所以我猜你需要找到一個解決辦法。這是一個應該工作的快速構思: 使用facet_grid而不是「facet_wrap」,那麼coord_flip()應該可以工作。然後將圖片保存爲PDF(或SVG)和重新排列某種矢量圖形軟件的情節 - 我建議Inkscape中...

+0

謝謝你的鏈接,但聽起來像壞消息。 我需要翻轉的單個圖上的座標軸。 – Prasad

+0

僅供參考,我在我的答案中增加了一點解決方法。 – thunk

1

這是我碰到這個問題我第二次或第三次。我發現我可以通過定義自定義幾何來破解我自己的解決方案。

geom_bar_horz <- function (mapping = NULL, data = NULL, stat = "bin", position = "stack", ...) { 
    GeomBar_horz$new(mapping = mapping, data = data, stat = stat, position = position, ...) 
} 

GeomBar_horz <- proto(ggplot2:::Geom, { 
    objname <- "bar_horz" 

    default_stat <- function(.) StatBin 
    default_pos <- function(.) PositionStack 
    default_aes <- function(.) aes(colour=NA, fill="grey20", size=0.5, linetype=1, weight = 1, alpha = NA) 

    required_aes <- c("y") 

    reparameterise <- function(., df, params) { 
    df$width <- df$width %||% 
     params$width %||% (resolution(df$x, FALSE) * 0.9) 
    OUT <- transform(df, 
       xmin = pmin(x, 0), xmax = pmax(x, 0), 
       ymin = y - .45, ymax = y + .45, width = NULL 
    ) 
    return(OUT) 
    } 

    draw_groups <- function(., data, scales, coordinates, ...) { 
    GeomRect$draw_groups(data, scales, coordinates, ...) 
    } 
    guide_geom <- function(.) "polygon" 
}) 

這是僅僅複製從GGPLOT2 github上的geom_bar代碼,然後切換Xÿ引用作水平barplot在標準笛卡爾協調員。

請注意,您需要必須使用position='identity',也可能stat='identity'爲此工作。如果您需要使用身份以外的職位,那麼您必須編輯collide function才能正常工作。

1

更新每晚2016年:此錯誤與coord_flip,facet_gridscales="free"已在發展版ggplot2中修復。你可以用

install.packages("devtools") 
devtools::install_github("hadley/ggplot2") 

注意安裝時,請嘗試free_xfree_y根據自己的需要,因爲它並不總是清楚什麼xy意思是,當你翻轉座標。

相關問題