2016-04-14 5 views
4

データに2つの次元、(1)異なるファセットのレポートエンティティ、(2)x軸のデータポイントに関連付けられた国を表示します。問題は、国の次元にはすべての個々の値よりもはるかに高い「合計」が含まれていることです。そのためには軸の制限が必要です。ggplot2:ファセット:異なる軸の制限とフリースペース

私の解決策は、別のファセット面を試してみることでしたが、同時にそれをうまく機能させることができませんでした。以下のダミーデータを考えてみましょう:

id <- c(1,1,1,1,1,1,2,2,2,2,2,2) 
country <- c("US","US","UK","World","World","World","US","US","UK","World","World","World") 
value <- c(150,40,100,1000,1100,1500,5,10,20,150,200,120) 
# + some other dimensions 
mydat <- data.frame(id,country,value) 

    id country value 
1 1  US 150 
2 1  US 40 
3 1  UK 100 
4 1 World 1000 
5 1 World 1100 
6 1 World 1500 
7 2  US  5 
8 2  US 10 
9 2  UK 20 
10 2 World 150 
11 2 World 200 
12 2 World 120 

私は世界全体を表示するには、ファセットグリッドを使用する場合は、軸の上限は、他の国のために強制されています

mydat$breakdown <- mydat$country == "World" 

ggplot(mydat) + aes(x=country,y=value) + geom_point() + 
    facet_grid(id ~ breakdown,scales = "free",space = "free_x") + 
    theme(strip.text.x = element_blank() , strip.background = element_blank(), 
     plot.margin = unit(c(0,0,0,0) , units = "lines")) 

(の最後の部分をプロットは単に追加のストリップを削除することです)。

facet grid plot

私はファセットラップを使用している場合、それは私に、各プロットの異なる軸の範囲を与えていたが、その後、私は、合計のための単一の列が同じスペースを消費することを意味し、space = "free_x"引数を渡すことはできません多くの国でのデータ・セットの醜い国全体の概要、:

ggplot(mydat) + aes(x=country,y=value) + geom_point() + 
    facet_wrap(id ~ breakdown,scales = "free") 

facet wrap plot

同様の質問をして、ここでいくつかのスレッド、bがあります。私はまだこれを達成するのに役に立たなかった。

Different axis limits per facet in ggplot2

Is it yet possible to have different axis breaks/limits for individual facets in ggplot with free scale?

Setting individual axis limits with facet_wrap and scales = "free" in ggplot2

答えて

4

たぶんgridExtra::grid.arrangecowplot::plot_gridを試してみてください。

lst <- split(mydat, list(mydat$breakdown, mydat$id)) 
plots <- lapply(seq(lst), function(x) {ggplot(lst[[x]]) + 
    aes(x=country,y=value) + 
    geom_point() + 
    ggtitle(names(lst)[x]) + labs(x=NULL, y=NULL) 
}) 
do.call(gridExtra::grid.arrange, 
     c(plots, list(ncol=2, widths=c(2/3, 1/3)), 
      left="Value", bottom="country")) 

enter image description here

+1

ありがとう!ただ、いくつかの追加はそれが本当にいいます (1) は接頭辞を削除します。 '名(LST)< - GSUB( "TRUE。"、 ""、名前(LST))'と '名(LSTを)< - gsub( "FALSE。"、 ""、names(lst)) 'となります。 (2) 列がダイナミック幅ください: 'T < - (長さ(ユニーク(mydatの$国[ - 長さ(ユニーク(mydatの$国[mydat $内訳]))' 'F <! mydat $ breakdown)])) ' そして、' f /(​​t + f) 'と' t /(t + f) 'で列幅を置き換えます。おそらくそれを行うより効率的な方法がありますが、私はまだR(ggplotはMatlabからの切り替え理由の1つです:)から始めています))。 – Chris7b

+0

'cowplot :: plot_grid'もチェックして、軸を整列させることはできません:https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html – lukeA

関連する問題