2017-12-02 20 views
-1

ggplot2でboxplotを作成したいのですが、データフレームの順番でプロットを整理したいと思います。ggplotでボックスプロットと凡例のラベルを並べ替え

私は、Rがボックスプロットをアルファベット順に整理していることを知っています。どのように私がすることができます

  1. が注文味にX軸を整理する - カラー - 容量すなわち最初の緑と、その後オレンジ、代わりにオレンジと緑の
  2. スイッチ伝説の順あまりに
  3. スイッチボックスを、 、第NaClおよびその後O_ {2}
library(ggplot2) 
library(readxl) 

Chemical <- rep(c("NaCl", "Al2"), times = 3, each = 4) 
Quality <- rep(c("Taste", "Color of package", "Capacity"), times = 1, each = 8) 
Accepted <- seq(0, 100, by = 100/23) 

DF <- data.frame(Chemical, Quality, Accepted) 

ggplot(DF, aes(x = Quality, y = Accepted, fill = Chemical)) + 
    geom_boxplot() + 
    scale_fill_manual(values = c("orange", "green"), 
        labels = expression("Al"[2], "NaCl")) + 
    xlab("") + 
    theme(legend.position = "top", legend.title = element_blank()) 

enter image description here

答えて

0

出力を制御するさまざまな方法があります。迅速な解決策は、次のようになります。

ggplot(DF, aes(x = Quality, y = Accepted, fill = Chemical)) + 
geom_boxplot() + 
scale_fill_manual(values = c("green", "orange"), 
       labels = expression("Al"[2], "NaCl")) + 
xlab("") + 
theme(legend.position = "top", legend.title = element_blank()) + 
guides(fill=guide_legend(reverse=TRUE)) + 
scale_x_discrete(limits=c("Taste", "Color of package", "Capacity")) 

引数guides(fill=guide_legend(reverse=TRUE))、手動で色の順序を変更し、scale_x_discreteとX軸上の特定の順序を固定が達成されるだけで。

enter image description here

DF$Quality <- factor (DF$Quality, levels = c ("Taste", "Color of package", "Capacity"))でレベルの順序を変更し、使用scale_x_discrete()せずに同じ結果を達成することも可能です。

関連する問題