我有以下代碼:
library("ggplot2")
x1 = as.numeric(x=QualidadeARO3$Ilhavo)
x2 = as.numeric(x=QualidadeARO3$VNTelha_Maia)
ggplot (QualidadeARO3, aes(x=x1, color="ílhavo"))
geom_histogram(fill="black", position="dodge", alpha = 0.2)
theme(legend.position="top")
xlab("microgramas por metro cúbico")
ylab("horas")
ggplot(QualidadeARO3, aes(x=x2, color="VN Telha-Maia"))
geom_histogram(fill="blue", position="dodge", alpha = 0.2)
theme(legend.position="top")
xlab("microgramas por metro cúbico")
ylab("horas")
從 Excel 匯入的資料表在哪里QualidadeARO3,如下所示:

和
ggplot (QualidadeARO3, aes(x=x1, color="ílhavo"))
geom_histogram(fill="black", position="dodge", alpha = 0.2)
theme(legend.position="top")
xlab("microgramas por metro cúbico")
ylab("horas")
給出以下輸出:

和
ggplot(QualidadeARO3, aes(x=x2, color="VN Telha-Maia"))
geom_histogram(fill="blue", position="dodge", alpha = 0.2)
theme(legend.position="top")
xlab("microgramas por metro cúbico")
ylab("horas")
給出:

到目前為止一切都很好,但我的問題是兩個圖都獨立“運行”,即,當我呼叫第二個ggplot圖時,第一個圖消失了,當我想將兩者重疊成一個直方圖以及兩個顏色標簽時。我在 R 中看到過使用 ggplot2 疊加直方圖,但仍然沒有任何線索。有什么幫助嗎?
uj5u.com熱心網友回復:
嘗試像這樣有單獨的幾何層:
ggplot (QualidadeARO3)
geom_histogram(aes(x=x1, color="ílhavo", fill="ílhavo"),
position="dodge", alpha = 0.2)
geom_histogram(aes(x=x2, color="VN Telha-Maia", fill="VN Telha-Maia"),
position="dodge", alpha = 0.2)
scale_fill_manual("Type", labels = c("ílhavo", "VN Telha-Maia"),
values= c("black", "blue"))
scale_color_discrete("Type", labels = c("ílhavo", "VN Telha-Maia"))
theme(legend.position="top")
xlab("microgramas por metro cúbico")
ylab("horas")
如果你想有特定的顏色,你也可以使用scale_fill_manual而不是。scale_color_discrete
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/474409.html
上一篇:R-在ggplot中包括兩個y軸
