2017-04-15 16 views
0

棒グラフを特定のデータ依存の順序でプロットすることは、私にとってはうまくいくように思われます。私にはわかりにくい理由から、うまくいかないようです。具体的には、自分のデータフレームの行を正しい順序で並べ替えることができますが、バーを識別する名前の列を順序付けされた要素にすることができます。したがって、棒グラフを望む順番に並べ替えると、グラフはデータフレームの列私が望む順序で。積み重ねられたgeom_bar内のアイテムの順序付け

tab <- structure(list(Item = c("Personal", "Peripheral", "Communication", "Multimedia", "Office", "Social Media"), `Not at all` = c(3.205128, 18.709677, 5.844156, 31.578947, 20.666667, 25.827815), Somewhat = c(30.76923, 23.87097, 24.67532, 18.42105, 30, 16.55629), `Don't know` = c(0.6410256, 2.5806452, 1.9480519, 11.1842105, 2.6666667, 5.9602649), Confident = c(32.69231, 29.67742, 33.11688, 17.10526, 23.33333, 27.15232), `Very confident` = c(32.69231, 25.16129, 34.41558, 21.71053, 23.33333, 24.50331)), .Names = c("Item", "Not at all", "Somewhat", "Don't know", "Confident", "Very confident"), row.names = c(NA, -6L), class = "data.frame") 

Title <- 'Plot title' 
ResponseLevels <- c("Not at all", "Somewhat", "Don't know", "Confident", "Very confident") # Labels for bars 

pal.1 <- brewer.pal(category, 'BrBG') # Colours 

tab <- tab %>% arrange(.[,2]) # Sort by first columns of responses 
tab$Item <- factor(tab$Item, levels = tab$Item[order(tab[,2])], ordered = TRUE) # Reorder factor levels 

tab.m <- melt(tab, id = 'Item') 
tab.m$col <- rep(pal.1, each = items) # Set colours 

g <- ggplot(data = tab.m, aes(x = Item, y = value, fill = col)) + 
    geom_bar(position = "stack", stat = "identity", aes(group = variable)) + 
    coord_flip() + 
    scale_fill_identity("Percent", labels = ResponseLevels, 
         breaks = pal.1, guide = "legend") + 
    labs(title = Title, y = "", x = "") + 
    theme(plot.title = element_text(size = 14, hjust = 0.5)) + 
    theme(axis.text.y = element_text(size = 16,hjust = 0)) + 
    theme(legend.position = "bottom") 

g 

stacked bar chart, running in the wrong direction

バーの積み重ねられた作品は「非常に自信を持って」から「すべてではない」から、右から左に実行する例。項目は「マルチメディア」から「個人」までの正しい順序で、各項目に「まったくない」と言った人の割合で並べられています。

私が得たいのは、このグラフは、凡例と同じように、左側に「まったくない」から右側に「非常に確信が持てる」という応答を逆順に並べたものです。私はこの注文がどのように設定されているか、それをどのように変更するかを理解できません。

私は '類似の質問'を読んだが、この特定のクエリに対しては何の答えも見られない。 ggplotを使用して、Rのグラフィックをベースにしないでください。

OK]をクリックして、有益な、とallstaireからの感謝の回答を踏まえ、私は次の

library(tidyverse) 

tab <- structure(list(Item = c("Personal", "Peripheral", "Communication", "Multimedia", "Office", "Social Media"), `Not at all` = c(3.205128, 18.709677, 5.844156, 31.578947, 20.666667, 25.827815), Somewhat = c(30.76923, 23.87097, 24.67532, 18.42105, 30, 16.55629), `Don't know` = c(0.6410256, 2.5806452, 1.9480519, 11.1842105, 2.6666667, 5.9602649), Confident = c(32.69231, 29.67742, 33.11688, 17.10526, 23.33333, 27.15232), `Very confident` = c(32.69231, 25.16129, 34.41558, 21.71053, 23.33333, 24.50331)), .Names = c("Item", "Not at all", "Somewhat", "Don't know", "Confident", "Very confident"), row.names = c(NA, -6L), class = "data.frame") 

tab <- tab %>% select(1,6,5,4,3,2,1) ## Re-order the columns of tab 

tab.m <- tab %>% arrange(`Not at all`) %>% 
mutate(Item = factor(Item, levels = Item[order(`Not at all`)])) %>% 
gather(variable, value, -Item, factor_key = TRUE) 

ggplot(data = tab.m, aes(x = Item, y = value, fill = variable)) + 
geom_col() + 
coord_flip() + 
scale_fill_brewer("Percent", type = 'cat', palette = 'BrBG', 
        guide = guide_legend(reverse = TRUE)) + 
labs(title = 'Plot title', y = NULL, x = NULL) + 
theme(legend.position = "bottom") 

を試してみて、これはまさに私が欲しいグラフであるので、私の押す問題が解決されます。

Stacked bar plot laid out correctly

私は私が手絵がこの

Stacked bar chart, legend going in wrong direction

である代わりに

ggplot(data = tab.m, aes(x = Item, y = value, fill = variable)) + 
geom_col() + 
coord_flip() + 
scale_fill_brewer("Percent", type = 'cat', palette = 'BrBG', 
        guide = guide_legend(reverse = FALSE)) + 
labs(title = 'Plot title', y = NULL, x = NULL) + 
theme(legend.position = "bottom") 

を言う場合は、ここではチャートの体が正しいですが、伝説があります間違った方向に進む。

これは私の問題を解決しますが、私の質問にはあまり答えません。私はデータフレームから始め、私が必要とするものを得るために、データ列の順序を逆にして、ガイドの凡例を逆にする必要があります。これは明らかに機能しますが、それはうんざりです。

スタックされた棒グラフは、積み重ねられたアイテムをどのように表示するのですか?溶けたデータセットの順序とは明らかに関係していますが、単に順序を変更すると、伝説が間違った方向に向いてしまいます。溶けたデータセットtab.mを上から下に見ると、レスポンスは「非常に自信がある」から「まったくありません」の順になりますが、デフォルトの凡例は逆の順序で「全くありません」から「非常に自信あり」です。

答えて

1

文字列ではなくguide_legendを渡す場合は、reverseパラメータをTRUEに設定できます。、編集のため

library(tidyverse) 

tab <- structure(list(Item = c("Personal", "Peripheral", "Communication", "Multimedia", "Office", "Social Media"), `Not at all` = c(3.205128, 18.709677, 5.844156, 31.578947, 20.666667, 25.827815), Somewhat = c(30.76923, 23.87097, 24.67532, 18.42105, 30, 16.55629), `Don't know` = c(0.6410256, 2.5806452, 1.9480519, 11.1842105, 2.6666667, 5.9602649), Confident = c(32.69231, 29.67742, 33.11688, 17.10526, 23.33333, 27.15232), `Very confident` = c(32.69231, 25.16129, 34.41558, 21.71053, 23.33333, 24.50331)), .Names = c("Item", "Not at all", "Somewhat", "Don't know", "Confident", "Very confident"), row.names = c(NA, -6L), class = "data.frame") 

tab.m <- tab %>% arrange(`Not at all`) %>% 
    mutate(Item = factor(Item, levels = Item[order(`Not at all`)])) %>% 
    gather(variable, value, -Item, factor_key = TRUE) 

ggplot(data = tab.m, aes(x = Item, y = value, fill = variable)) + 
    geom_col() + 
    coord_flip() + 
    scale_fill_brewer("Percent", palette = 'BrBG', 
         guide = guide_legend(reverse = TRUE)) + 
    labs(title = 'Plot title', y = NULL, x = NULL) + 
    theme(legend.position = "bottom") 


をビットを簡素化:

バー順序は上記に起因する列の順序によって決定される因子レベルの順序によって決定されますファクタを作成するためにgatherを使用しますが、coord_flipはあまり明確ではありません。レベルオーダをlevels<-でやり直すことは簡単ですが、ファクタを再組み立てすることもできます。色を同じレベルに保つには、direction = -1scale_fill_brewerに渡して順序を逆にします。

tab.m <- tab %>% arrange(`Not at all`) %>% 
    mutate(Item = factor(Item, levels = Item[order(`Not at all`)])) %>% 
    gather(variable, value, -Item, factor_key = TRUE) %>% 
    mutate(variable = factor(variable, levels = rev(levels(variable)), ordered = TRUE)) 

ggplot(data = tab.m, aes(x = Item, y = value, fill = variable)) + 
    geom_col() + 
    coord_flip() + 
    scale_fill_brewer("Percent", palette = 'BrBG', direction = -1, 
         guide = guide_legend(reverse = TRUE)) + 
    labs(title = 'Plot title', y = NULL, x = NULL) + 
    theme(legend.position = "bottom") 

+0

これは素晴らしい提案です、そして私の注意にそれを描画していただきありがとうございます。グラフはまだ間違った方法で実行されているので、私が必要とするものはまったくありません。私の仕事の大部分では、自然な順序づけがあります。この場合、「まったくではない」から「非常に確信している」までです。読者がx軸の右に大きな数字を期待するように、彼らは「非常に確信している」と思っています。 ResponseLevelsを逆順に配置して、グラフが正しい順序で表示されるかどうかを確認します。再度、感謝します! – astaines

+1

パーフェクト、私は今それを得る。あなたの明快な説明をもう一度ありがとう。 – astaines

関連する問題