![](/img/trans.png)
[英]Don't show shape in color legend in a 2 legend plot (R ggplot2)
[英]ggplot2 legend showing but variables in plot don't have colour
我一直在研究 plot 的一些代碼,該圖具有多個變量以及輔助 Y 軸和圖例。 我通過以下示例實現了這一點:
ggplot2 折線圖給出“geom_path:每個組僅包含一個觀察值。您需要調整組審美嗎?”
但是,我目前正在使用同一圖表的兩個版本。 一個在 plot 本身中沒有顏色,但包括一個為每個變量提供正確顏色的圖例,以及在 plot 中具有顏色但沒有顯示圖例的第二個圖。 請看下面的圖和代碼。
我知道這與geom_point()
代碼行有關,正如我在上一個問題的答案中看到的那樣: ggplot2 圖例未出現的原因
colour= XYZ 應該在 aes() 內部,而不是外部
geom_point(aes(data, colour=XYZ)) #------>圖例
geom_point(aes(data),colour=XYZ) #------>沒有圖例
請在下面找到一部分數據集:
h1enraw <-structure(list(run = c(1738, 1739, 1740, 1741, 1742, 1743),
temp = c(19, 19, 19, 19, 21, 22),
avgbase = c(1386, 1386, 1389, 1389, 1352, 1336),
no2c = c(6.98, 6.96, 6.94, 6.99, 7.01, 7.01),
no3c = c(18.52, 17.6, 18.77, 19.81, 18.22, 18.60)),
row.names = c(NA, 6L), class = "data.frame")
根據上面引用的代碼,我理解為什么我的一個圖形版本有圖例而另一個沒有。 我只是不明白這如何影響圖表中的變量是否有顏色。 如果有人能指出我正確的方向,我將不勝感激。
沒有顏色但顯示圖例的圖表
# Choose number for dividing second Y axis
scaleRight <- 40
ymax <-43
h1no2<-ggplot(h1enraw, aes(x=run)) +
geom_path(aes(y=temp, group=1,colour="cornflowerblue"), size=0.9) +
geom_point(aes(y=no2c, group=1,colour="red")) +
geom_point(aes(y=no3c, group=1,colour="darkgreen")) +
geom_line(aes(y=avgbase/scaleRight, group=1,colour="chocolate1"), size=0.9) +
scale_y_continuous(breaks=seq(0,40,2), expand = expansion(mult = c(0,.05)),
sec.axis = sec_axis(~.*scaleRight, name = "Average Baseline (Transmitance Units)")) +
coord_cartesian(ylim = c(0, ymax)) +
theme_classic() +
labs( y="Temperature (°C) / Concentration (ppm)", x="Run", title = "H1 - High Temperature Cycle") +
theme(text = element_text(size=9),
axis.text.x = element_text(angle=90, vjust=0.6),
axis.text=element_text(size=12),
axis.title=element_text(size=14,face="bold"),
panel.border = element_rect(colour = "chocolate1", fill=NA, size=0.5),
legend.position = "bottom", legend.title=element_text(size=10), legend.text = element_text(size=8),
axis.title.y = element_text(size=10),
plot.title = element_text(size=14, face="bold")) +
theme(legend.title=element_blank()) +
scale_fill_manual(breaks=c("temp","no2c","no3c","avgbase"),
labels=c("Temperature", "NO2", "NO3", "Avg Baseline"),
values = c("temp"="cornflowerblue", "no2c"="red",
"no3c"="darkgreen", "avgbase"="chocolate1")) +
scale_color_manual(labels = c("Temperature", expression(NO[2]), expression(NO[3]), "Avg Baseline"),
values = c("temp"="cornflowerblue", "no2c"="red",
"no3c"="darkgreen", "avgbase"="chocolate1"),
breaks=c("temp","no2c","no3c","avgbase"))
有顏色但沒有圖例的圖表
# Choose number for dividing second Y axis
scaleRight <- 40
ymax <-43
h2no2<-ggplot(h2enraw, aes(x=run)) +
geom_path(aes(y=temp, group=1), colour="cornflowerblue", size=0.9) +
geom_point(aes(y=no2c, group=1 ),colour="red") +
geom_point(aes(y=no3c, group=1 ),colour="darkgreen") +
geom_line(aes(y=avgbase/scaleRight, group=1 ),colour="chocolate1", size=0.9) +
scale_y_continuous(breaks=seq(0,40,2), expand = expansion(mult = c(0,.05)),
sec.axis = sec_axis(~.*scaleRight, name = "Average Baseline (Transmitance Units)")) +
coord_cartesian(ylim = c(0, ymax)) +
theme_classic() +
labs( y="Temperature (°C) / Concentration (ppm)", x="Run", title = "H2 - High Temperature Cycle") +
theme(text = element_text(size=9),
axis.text.x = element_text(angle=90, vjust=0.6),
axis.text=element_text(size=12),
axis.title=element_text(size=14,face="bold"),
panel.border = element_rect(colour = "chocolate1", fill=NA, size=0.5),
legend.position = "bottom", legend.title=element_text(size=10), legend.text = element_text(size=8),
axis.title.y = element_text(size=10),
plot.title = element_text(size=14, face="bold")) +
scale_fill_manual(name="Legend",
breaks=c("temp","no2c","no3c","avgbase"),
labels=c("Temperature", "NO2", "NO3", "Avg Baseline"))
太感謝了
選項1
這應該適合你。
我計算了avgbase
的縮放版本,然后將您的數據轉換為長格式。 因為當您想要在 plot 中對變量進行分組時,這通常會有所幫助。
在ggplot()
中,我在每個geom_
中過濾我想要 plot 的特定數據點(即在geom_line()
中,我過濾temp
和avgbase_scaled
因為我希望它們繪制為線)。
library(tidyverse)
h1enraw <-structure(list(run = c(1738, 1739, 1740, 1741, 1742, 1743),
temp = c(19, 19, 19, 19, 21, 22),
avgbase = c(1386, 1386, 1389, 1389, 1352, 1336),
no2c = c(6.98, 6.96, 6.94, 6.99, 7.01, 7.01),
no3c = c(18.52, 17.6, 18.77, 19.81, 18.22, 18.60)),
row.names = c(NA, 6L), class = "data.frame")
scaleRight <- 40
ymax <- 43
h1enraw %>%
mutate(avgbase_scaled = avgbase/scaleRight, .keep = "unused") %>%
pivot_longer(c(-run), names_to = "value_type", values_to = "value") %>%
ggplot(aes(x = run, color = value_type)) +
geom_line(data = . %>% filter(value_type %in% c("avgbase_scaled",
"temp")),
aes(y = value), size=0.9)+
geom_point(data = . %>% filter(value_type %in% c("no2c",
"no3c")),
aes(y = value), size=1)+
scale_y_continuous(breaks=seq(0,40,2), expand = expansion(mult = c(0,.05)),
sec.axis = sec_axis(~.*scaleRight, name = "Average Baseline (Transmitance Units)"))+
coord_cartesian(ylim = c(0, ymax)) +
theme_classic() +
labs( y="Temperature (°C) / Concentration (ppm)", x="Run",
title = "H1 - High Temperature Cycle") +
theme(text = element_text(size=9),
axis.text.x = element_text(angle=90, vjust=0.6),
axis.text=element_text(size=12),
axis.title=element_text(size=14,face="bold"),
panel.border = element_rect(colour = "chocolate1", fill=NA, size=0.5),
legend.position = "bottom",
legend.title=element_blank(),
legend.text = element_text(size=8),
axis.title.y = element_text(size=10),
plot.title = element_text(size=14, face="bold"))+
scale_color_manual(labels = c("Temperature", expression(NO[2]), expression(NO[3]), "Avg Baseline"),
values = c("temp"="cornflowerblue", "no2c"="red",
"no3c"="darkgreen", "avgbase_scaled"="chocolate1"),
breaks=c("temp","no2c","no3c","avgbase_scaled"))
使用reprex v2.0.2創建於 2022-08-30
選項 2
這是一個沒有數據轉換的選項:您必須在aes()
中使用color
參數並分配正確的列名:
scaleRight <- 40
ymax <-43
ggplot(h1enraw, aes(x=run)) +
geom_path(aes(y=temp, color="temp"), size=0.9) +
geom_point(aes(y=no2c, color="no2c")) +
geom_point(aes(y=no3c, color="no3c"))+
geom_line(aes(y=avgbase/scaleRight, color="avgbase"), size=0.9) +
scale_y_continuous(breaks=seq(0,40,2), expand = expansion(mult = c(0,.05)),
sec.axis = sec_axis(~.*scaleRight, name = "Average Baseline (Transmitance Units)")) +
coord_cartesian(ylim = c(0, ymax)) +
theme_classic() +
labs( y="Temperature (°C) / Concentration (ppm)", x="Run",
title = "H1 - High Temperature Cycle") +
theme(text = element_text(size=9),
axis.text.x = element_text(angle=90, vjust=0.6),
axis.text = element_text(size=12),
axis.title = element_text(size=14,face="bold"),
legend.position = "bottom",
legend.title = element_blank(),
legend.text = element_text(size=8),
axis.title.y = element_text(size=10),
plot.title = element_text(size=14, face="bold"))+
scale_color_manual(labels=c("Temperature", "NO2", "NO3", "Avg Baseline"),
values = c("temp"="cornflowerblue", "no2c"="red",
"no3c"="darkgreen", "avgbase"="chocolate1"))
使用reprex v2.0.2創建於 2022-08-30
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.