2016-06-16 51 views
3

これは役に立たないと私は長い間解決しています。私はループ内のggplot2のいくつかの横線を持つボックスプロットをグラフ化しています。私は、線の美しさの設定 "色"を私が凡例で欲しい名前としてマップすることを考え出しました。ただし、線のインデックス値は更新されないように見えます。描画されるのは、すべてのプロットの線の値、特にループの最後の項目の値です。ループ内のボックスプロット/水平線を実行しているときに、hlineインデックスの問題が発生しない

次のように私は、虹彩データセットの問題を再現:

library(dplyr) 
library(ggplot2) 
library(tidyr) 
liris<-gather(iris,"Param","Value",1:4) 
liris<-as.data.frame(liris) 

indices<-unique(liris$Param) 
plot_list<-list() 
for (i in indices) { 
sub<-filter(liris,Param==i) 

min<-min(sub$Value) 
max<-max(sub$Value) 

g<-ggplot(sub,aes(x=Param,y=Value,fill=Param)) 
g<-g+geom_boxplot() 
g<-g+facet_wrap(~Species) 
g<-g+geom_hline(aes(yintercept=min,colour="Min Sepal Length"),linetype="dashed",show_guide=TRUE) 
g<-g+geom_hline(aes(yintercept=max,colour="Max Sepal Length"),linetype="dashed",show_guide=TRUE) 
g<-g+theme()+scale_color_manual(values=c("blue","black")) 
g<-g+theme(legend.position="bottom")+guides(colour=guide_legend(title=NULL)) 
g<-g+theme(legend.position="bottom")+guides(fill=guide_legend(title=NULL)) 
g<-g+theme(legend.box="horizontal") 

plot_list[[i]] = g 

} 

pdf("TEST.pdf",height = 8.5, width = 11,onefile=TRUE,paper="special") 
for (i in indices) { 
print(plot_list[[i]]) 
} 
dev.off() 

をこれは素敵な伝説を持つプロットを生成するが、水平線が全て同じ値です。私はそれがggplot2で私の限られた経験とラインの美学をマッピングすることであると言うことができます。私がそれらをマップしてこれを行うと、行の値は正しく変わりますが、凡例は変わりません。

library(dplyr) 
library(ggplot2) 
library(tidyr)  
liris<-gather(iris,"Param","Value",1:4) 
liris<-as.data.frame(liris) 

indices<-unique(liris$Param) 
plot_list<-list() 
for (i in indices) { 
sub<-filter(liris,Param==i) 

min<-min(sub$Value) 
max<-max(sub$Value) 

g<-ggplot(sub,aes(x=Param,y=Value,fill=Param)) 
g<-g+geom_boxplot() 
g<-g+facet_wrap(~Species) 
g<-g+geom_hline(yintercept=min,colour="black",linetype="dashed",show_guide=TRUE) 
g<-g+geom_hline(yintercept=max,colour="blue",linetype="dashed",show_guide=TRUE) 
g<-g+theme(legend.position="bottom")+guides(colour=guide_legend(title=NULL)) 
g<-g+theme(legend.position="bottom")+guides(fill=guide_legend(title=NULL)) 
g<-g+theme(legend.box="horizontal") 

plot_list[[i]] = g 

} 

pdf("TEST2.pdf",height = 8.5, width = 11,onefile=TRUE,paper="special") 
for (i in indices) { 
print(plot_list[[i]]) 
} 
dev.off() 

これを修正することは非常に歓迎されます。どうもありがとう。

答えて

3

私はプロットの書式設定に少し慣れてきましたが、ここではあなたが探していた最小値と最大値を変更する必要があります。 geom_boxplot()はxlims()を設定しているため、破線は常にプロット範囲を囲んでいますが、スケール値は変動しています。

indices<-unique(liris$Param) 
plot_list<-list() 
for (i in indices) { 
    sub <- filter(liris,Param==i) 
    #min <- min(sub$Value) 
    #max <- max(sub$Value) 

    g <- ggplot(sub,aes(x=Param,y=Value)) + 
     geom_boxplot(fill = "green", alpha = .5) + 
     facet_wrap(~Species) + 
     geom_hline(aes(yintercept=min(Value),colour="Max"),linetype="dashed",show.legend=TRUE) + 
     geom_hline(aes(yintercept=max(Value),colour="Min"),linetype="dashed",show.legend=TRUE) + 
     scale_color_manual(values=c("blue","red")) + 
     theme_bw() + 
     theme(legend.position="bottom")+guides(colour=guide_legend(title=NULL)) + 
     theme(legend.position="bottom")+guides(fill=guide_legend(title=NULL)) + 
     theme(legend.box="horizontal") + 
     labs(title = paste0(i, " Facetted by Species"), x = "", y = "cm") 

    plot_list[[i]] <- g 
} 
pdf("TEST2.pdf",height = 8.5, width = 11,onefile=TRUE,paper="special") 
for (i in plot_list) { 
    print(i) 
} 
dev.off() 

は本当にあなたがする必要があるすべては、()の代わりにプロットビルド前のS-それぞれgeom_hlineの内側にその場で分(バリュー)とmax(バリュー)関数を呼び出すことです。内部にAESのいずれかの要素は()のプロット(liris)、ご使用の環境内だけではなく、変数(MIN、MAX)ここで

enter image description here

+0

おかげネイサン・デイそんなに、それは)(本当にあなたがする必要があるすべては、それぞれのgeom_hlineの内側場で分(バリュー)とmax(バリュー)関数を呼び出すことです」でした(最小、最大)」だけではなく、プロット(liris)のために供給されているデータから来ると考えられています。私はyinterceptの値を行のエースに移動し、それは完全に機能しました。ラインと私のすべての伝説を修正してください。フォーマットのクリーンナップもありがとう!私はちょうど学んでいます。 –

+0

お手伝いします! ggplot2は学ぶためのすばらしいツールです。私はgesplotエラーの99%がaes()問題のために見つかっていることを発見しました。幸運コーディング、あなたがつかまえられる以上の問題がある場合、威嚇して自由に感じる。 – Nate

3

のために調達されたデータから来ることになっていると、その番組の可能なソリューションです。 ggplot2に補助データまたは要約データを含める一般的な方法です。あなたの質問に対する正確な解決策ではありませんが、それは@ Nathan Dayによって提供されています。

以下、サマリーデータ(最小値と最大値)を含む2番目のdata.frameを作成します。重要な概念は、aes()の目的は、data.frameのとプロットの視覚的な機能との間の関連付けを作成することです。 geom_hline()コールでは、aes()を使用してStatValue列をyinterceptフィーチャにマップし、SummaryStatistic列をcolourフィーチャに関連付けます。要約データ列の情報にかかわらず、すべての8つのラインに同じ線種を持たせたいので、linetypeの外にあり、aes()であることに注意してください。

# Create separate data.frame containing summary data. 
summary_data <- liris %>% 
       group_by(Param) %>% 
       summarise(Maximum=max(Value), Minimum=min(Value)) %>% 
       gather(key=SummaryStatistic, value=StatisticValue, -Param) 
summary_data 
#   Param SummaryStatistic StatisticValue 
#   (chr)   (chr)   (dbl) 
# 1 Petal.Length   Maximum   6.9 
# 2 Petal.Width   Maximum   2.5 
# 3 Sepal.Length   Maximum   7.9 
# 4 Sepal.Width   Maximum   4.4 
# 5 Petal.Length   Minimum   1.0 
# 6 Petal.Width   Minimum   0.1 
# 7 Sepal.Length   Minimum   4.3 
# 8 Sepal.Width   Minimum   2.0 

# Create named vector, for use with manual color scales. 
summary_colors = c(Maximum="darkorange", Minimum="royalblue") 

p = ggplot(liris, aes(x=Species, y=Value)) + 
    geom_boxplot() + 
    geom_hline(data=summary_data, 
       aes(yintercept=StatisticValue, colour=SummaryStatistic), 
       linetype="dashed", size=1.6) + 
    scale_colour_manual(values=summary_colors) + 
    facet_grid(. ~ Param) 

enter image description here

関連する問題