2016-09-27 34 views
1

**編集、ここでは2つの素晴らしい解決策がありますが、答えとしてマークされていますが、@hrbrmstrは2つのggplotsを組み合わせた素晴らしいソリューションを提供します。 *2つのX軸ラベルを1つのX軸でggplotに追加します

ここでコード

breaks.major <- c(0,15,37.5,52.5,67.5,82.5,95,100) #defines the midpoints of the categories (label locations) 
breaks.minor <- c(30,45,60,75,90) #defines the edges of the categories (second label set I need) 
labels.minor <- c("","Extremely \nDissatisfied","Dissatisfied","Uncertain","Satisfied","Very \nSatisfied","Extremely \nSatisfied","") 
lims =c(0,100) 
g <- ggplot(mpg, aes(class))+ 
    geom_bar()+ 
    coord_flip()+ 
    scale_y_continuous(limit = lims, minor_breaks = breaks.minor, breaks = breaks.major, labels = labels.minor) + 
    theme(panel.grid.major.x = element_blank()) + 
    theme(panel.grid.major.y = element_blank()) + 
    theme(axis.ticks.x=element_blank()) + 
    theme(axis.title= element_blank()) 

だそれはこのプロットを生成します。

enter image description here

私はX軸のラベルの2セットを持っている必要があり、1カテゴリ名(すなわち、 「満足」など)と、breaks.minor位置(カテゴリの限界、すなわち垂直のパネルグリッド線に対応する)の値を示すものとの両方を含むことができる。現在のlabels.minorラベルを必要な追加ラベルより下にする必要があります。

私は現在、番号とカテゴリがすべて1つの長い文字列になるように改行を行いますが、プロットのサイズ変更で面白くなります。テキストボックスでこれを行うことができます。 ggplot?

余分なポイントあなたは自分のセクションの中央に私の現在のラベルを取得する場合(例えば、「非常に満足」中心から外れている)

これは私の所望の出力である(私の「mspaint」をご容赦)

enter image description here

+0

あなたは 'labels.minor'ラベルは' breaks.minor'位置に値を下回るようにしたいように見えるが、後者は5アムIを持っていながら、前者は6つの要素を持っています何かが欠けている? –

+0

@Weihuang Wong 6と7(実際には0と100を含む)は、カテゴリ境界とカテゴリの中間点です。希望する出力の更新された例を参照してください。 – Alex

答えて

3

これはおそらく、このようなものです。適切な間隔とカテゴリ名の位置に対処するために、両方の軸についてexpandの設定に注意してください。

実際にはのラベルは、カテゴリ境界の中央にありません。デフォルトでは、軸は少しだけ拡張されています。

あなたがもっと上手くしたいのであれば、プロット領域の外側に描画することもできますが、もう少し手間がかかります。 This questionを起動する必要があります。

ggplot(mpg, aes(class))+ 
    geom_bar()+ 
    geom_text(data = data.frame(br = breaks.minor), aes(y = br, label = br, x = 7.75), 
      size = 4, col = 'grey30') + 
    coord_flip()+ 
    scale_y_continuous(limit = lims, minor_breaks = breaks.minor, 
        breaks = breaks.major, labels = labels.minor, 
        expand = c(0, 0)) + 
    scale_x_discrete(expand = c(0.05, 0)) + 
    theme(panel.grid.major.x = element_blank()) + 
    theme(panel.grid.major.y = element_blank()) + 
    theme(axis.ticks.x=element_blank()) + 
    theme(axis.title= element_blank()) 

enter image description here

+0

恐ろしい@Axeman、私は引き続き努力とリンクをありがとう。残念ながら、プロットと「満足」などの数字が必要です(opの更新図を参照)。私はおそらくそれについてはっきりしていなかったかもしれませんが、あなたの提案は、これをネイティブに行うggplotの制限が与えられた別のアプローチであったかもしれません。 – Alex

+0

'geom_text'の' x'を小さなものに置き換えて、数字を下に移動して閉じます。 – Axeman

+0

また、それらのすべてを 'breaks'として追加し、改行(' \ n')を使っていくつかのラベルを上下に移動することもできます。 – Axeman

4

私は、これはあなたが探しているものだと思うん:

library(ggplot2) 
library(grid) 
library(gtable) 
library(gridExtra) 

breaks.major <- c(0, 15, 37.5, 52.5, 67.5, 82.5, 95, 100) 
breaks.minor <- c(30, 45, 60, 75, 90) 
labels.minor <- c("", "Extremely\nDissatisfied", "Dissatisfied", "Uncertain", 
        "Satisfied", "Very\nSatisfied", "Extremely\nSatisfied", "") 
lims <- c(0, 100) 

# build the main plot with the text axis 
gg1 <- ggplot(mpg, aes(class)) 
gg1 <- gg1 + geom_bar() 
gg1 <- gg1 + scale_y_continuous(expand=c(0,0), limit=lims, 
           minor_breaks=breaks.minor, 
           breaks=breaks.major, 
           labels=labels.minor) 
gg1 <- gg1 + coord_flip() 
gg1 <- gg1 + theme(panel.grid.major.x=element_blank()) 
gg1 <- gg1 + theme(panel.grid.major.y=element_blank()) 
gg1 <- gg1 + theme(axis.ticks.x=element_blank()) 
gg1 <- gg1 + theme(axis.title=element_blank()) 

# let ggplot2 do the work of building the second axis 
gg2 <- ggplot(mpg, aes(class)) 
gg2 <- gg2 + scale_y_continuous(expand=c(0,0), limit=lims, 
           breaks=c(0, breaks.minor, 100)) 
gg2 <- gg2 + coord_flip() 
gg2 <- gg2 + theme(axis.ticks.x=element_blank()) 
gg2 <- gg2 + theme(axis.text.x=element_text(hjust=c(0, 0.5, 0.5, 0.5, 0.5, 0.5, 1))) 

gt1 <- ggplot_gtable(ggplot_build(gg1)) 
gt2 <- ggplot_gtable(ggplot_build(gg2)) 
axis2 <- grid.arrange(gt2$grobs[[5]]) 

gt <- gtable_add_rows(gt1, unit(0.1, "null"), 4) 
grid.arrange(gtable_add_grob(gt, axis2, t=5, l=4, b=5, r=4)) 

enter image description here

+0

このサンプルコードではうまく機能する優れたソリューションです。残念なことに私の実際のプロットコードはずっと長く、条件付きのエラーバーやその他の複雑な書式があります。プロットルーチンのコードを効果的に倍増させると、物事が爆発します。 2つの答えを正確にマークすることはできますか? – Alex

+0

これはもう少し最小限にすることができると確信しています。私はそれをこの前夜に行くつもりです。 – hrbrmstr

+0

こんにちは@hrbrmstrあなたはこれを凝縮する方法を見つけましたか? – Alex

2

私は主要な-ラベルとしてすべてのラベルを書きました。

# OP's 
breaks.major <- c(0,15,37.5,52.5,67.5,82.5,95,100) #defines the midpoints of the categories (label locations) 
breaks.minor <- c(30,45,60,75,90) #defines the edges of the categories (second label set I need) 
labels.minor <- c("","Extremely \nDissatisfied","Dissatisfied","Uncertain","Satisfied","Very \nSatisfied","Extremely \nSatisfied","") 
lims =c(0,100) 

breaks.major2 <- c(0,15,37.5,52.5,67.5,82.5,95) 
breaks.minor2 <- c(30,45,60,75,90,100)  # put 100 into minor from major 

breaks.comb <- sort(c(breaks.major2, breaks.minor2 - 1.0E-6)) # avoid the just same value as minor 
label.comb <- c(0, "\nExtremely \nDissatisfied", 30, "\nDissatisfied", 45, "\nUncertain", 60, 
       "\nSatisfied", 75, "\nVery \nSatisfied", 90, "\nExtremely \nSatisfied", 100) 

library(ggplot2) 
g <- ggplot(mpg, aes(class))+ 
    geom_bar()+ 
    coord_flip()+ 
    scale_y_continuous(limit = lims, minor_breaks = breaks.minor2, breaks = breaks.comb, 
        labels = label.comb, expand = c(0,0)) + 
    theme(panel.grid.major.x = element_blank()) + 
    theme(panel.grid.major.y = element_blank()) + 
    theme(axis.ticks.x=element_blank()) + 
    theme(axis.title= element_blank()) + 
    theme(plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "lines")) 

enter image description here

関連する問題