私はこれまでの同様の質問を見てきました。まだ出力が得られません。ggplotにマニュアル凡例を追加
私は、ファセットグラフで表示しているたくさんの分布を持っています。私はそれらを通る垂直線を引いて、異なる介入を表します。
ディストリビューションの塗りつぶし色と余分な線の線色の両方を含む凡例を表示しようとしています。私が言う限りでは、凡例を定義するのにscale_colour_manual()
を使用して、aes()
の中でカラーコマンドを設定するなど、すべてを正しくやっています。私はまだ塗りつぶしの色の伝説を取得しています。
は、ここに私のコードです:
ggplot(modCosts, aes(x=cost)) + geom_density(aes(fill=group)) + theme_bw() +
facet_wrap(~ country, scales="free") + scale_x_continuous(label = dollar) +
scale_fill_brewer(palette = "RdGy", name = "Income group", labels = c("HIC" = "High income", "UMIC" = "Upper-middle income", "LIC" = "Low income")) +
labs(y = "Density", x = "Cost", title = "Medical costs of surgery\nActual vs. modeled") +
geom_vline(data = surgCosts, aes(xintercept = CS.tert.lo, color = "red4")) +
geom_vline(data = surgCosts, aes(xintercept = CS.tert.hi, color = "red4")) +
geom_vline(data = surgCosts, aes(xintercept = CS.prim.lo, color = "red4"), lty = "dashed") +
geom_vline(data = surgCosts, aes(xintercept = CS.prim.hi, color = "red4"), lty = "dashed") +
geom_vline(data = surgCosts, aes(xintercept = Lap.tert.lo, color = "deepskyblue")) +
geom_vline(data = surgCosts, aes(xintercept = Lap.tert.hi, color = "deepskyblue")) +
geom_vline(data = surgCosts, aes(xintercept = Lap.prim.lo, color = "deepskyblue"), lty = "dashed") +
geom_vline(data = surgCosts, aes(xintercept = Lap.prim.hi, color = "deepskyblue"), lty = "dashed") +
geom_vline(data = surgCosts, aes(xintercept = Fx.tert.lo, color = "yellowgreen")) +
geom_vline(data = surgCosts, aes(xintercept = Fx.tert.hi, color = "yellowgreen")) +
scale_color_manual(name = "Reported cost", values = c("red4" = "red4", "deepskyblue" = "deepskyblue", "yellowgreen" = "yellowgreen"),
labels = c("Int1", "Int2", "Int3")) +
theme(axis.ticks = element_blank(), axis.text.y = element_blank(), legend.position = "right")
任意の助けをいただければ幸いです!
データセットを提供した方が、dput(modCosts)やdput(surgCosts)などを使用する方が簡単です。 – jlhoward
'geom_vline(...)'に 'show_guide = ...'引数があり、デフォルトは 'FALSE'です。 – jlhoward
単一のデータフレームを使用し、色の美学をマッピングすることで、これらのレイヤーをすべて単一のgeom_vlineにまとめることをお勧めします。 – joran