これは役に立たないと私は長い間解決しています。私はループ内の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()
これを修正することは非常に歓迎されます。どうもありがとう。
おかげネイサン・デイそんなに、それは)(本当にあなたがする必要があるすべては、それぞれのgeom_hlineの内側場で分(バリュー)とmax(バリュー)関数を呼び出すことです」でした(最小、最大)」だけではなく、プロット(liris)のために供給されているデータから来ると考えられています。私はyinterceptの値を行のエースに移動し、それは完全に機能しました。ラインと私のすべての伝説を修正してください。フォーマットのクリーンナップもありがとう!私はちょうど学んでいます。 –
お手伝いします! ggplot2は学ぶためのすばらしいツールです。私はgesplotエラーの99%がaes()問題のために見つかっていることを発見しました。幸運コーディング、あなたがつかまえられる以上の問題がある場合、威嚇して自由に感じる。 – Nate