gpt4 book ai didi

r - 图例与来自不同数据帧的多个图的交互作用 : How does one deal with this interaction?

转载 作者:行者123 更新时间:2023-12-05 01:05:40 24 4
gpt4 key购买 nike

包含我希望绘制的数据的数据帧的可重现子集:

# Import library
library(ggplot2)

# Data Example--------------
MCsubsetDT1112 <- structure(list(Date = structure(c(15280, 15280, 15281, 15281,
15282, 15282, 15283, 15283, 15284, 15284, 15285, 15285, 15286,
15286, 15287, 15287, 15288, 15288, 15289, 15289), class = "Date"),
SubstrateConcat = structure(c(1L, 5L, 1L, 5L, 1L, 5L, 1L,
5L, 1L, 5L, 1L, 5L, 1L, 5L, 1L, 5L, 1L, 5L, 1L, 5L), .Label = c("B_B",
"C_C", "C1_C", "C2_C", "S_S", "S_S "), class = "factor"),
SiteSub = c("SW_MC.B_B", "SW_MC.S_S", "SW_MC.B_B", "SW_MC.S_S",
"SW_MC.B_B", "SW_MC.S_S", "SW_MC.B_B", "SW_MC.S_S", "SW_MC.B_B",
"SW_MC.S_S", "SW_MC.B_B", "SW_MC.S_S", "SW_MC.B_B", "SW_MC.S_S",
"SW_MC.B_B", "SW_MC.S_S", "SW_MC.B_B", "SW_MC.S_S", "SW_MC.B_B",
"SW_MC.S_S"), WaterType = c("WarmWater", "WarmWater", "WarmWater",
"WarmWater", "WarmWater", "WarmWater", "WarmWater", "WarmWater",
"WarmWater", "WarmWater", "WarmWater", "WarmWater", "WarmWater",
"WarmWater", "WarmWater", "WarmWater", "WarmWater", "WarmWater",
"WarmWater", "WarmWater"), Mean = c(5.134, 0.678, 5.153,
0.755, 5.126, 0.347, 6.687, 1.098, 6.647, 0.932, 6.145, 0.469,
5.629, 0.342, 5.36, 0.036, 5.392, 0.107, 5.941, 0.376)), .Names = c("Date",
"SubstrateConcat", "SiteSub", "WaterType", "Mean"), row.names = 3100:3119, class = "data.frame")

MCsubsetTemp1112 <- structure(list(Date = structure(c(15340, 15341, 15342, 15343,
15344, 15345, 15346, 15347, 15348, 15349, 15350, 15351, 15352,
15353, 15354, 15355, 15356, 15357, 15358, 15359), class = "Date"),
SubstrateConcat = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("B_B",
"C_C", "C1_C", "C2_C", "S_S", "S_S "), class = "factor"),
SiteSub = c("SW_MC.B_B", "SW_MC.B_B", "SW_MC.B_B", "SW_MC.B_B",
"SW_MC.B_B", "SW_MC.B_B", "SW_MC.B_B", "SW_MC.B_B", "SW_MC.B_B",
"SW_MC.B_B", "SW_MC.B_B", "SW_MC.B_B", "SW_MC.B_B", "SW_MC.B_B",
"SW_MC.B_B", "SW_MC.B_B", "SW_MC.B_B", "SW_MC.B_B", "SW_MC.B_B",
"SW_MC.B_B"), WaterType = c("WarmWater", "WarmWater", "WarmWater",
"WarmWater", "WarmWater", "WarmWater", "WarmWater", "WarmWater",
"WarmWater", "WarmWater", "WarmWater", "WarmWater", "WarmWater",
"WarmWater", "WarmWater", "WarmWater", "WarmWater", "WarmWater",
"WarmWater", "WarmWater"), Mean = c(28.115, 28.097, 28.028,
27.937, 27.824, 27.743, 27.678, 27.545, 27.465, 27.401, 27.246,
27.169, 27.155, 26.937, 26.493, 25.958, 25.502, 25.329, 25.247,
25.171)), .Names = c("Date", "SubstrateConcat", "SiteSub",
"WaterType", "Mean"), row.names = 1753:1772, class = "data.frame")

我将温度数据绘制为线(来自 MCsubsetTemp1112 ),将 delta-T 绘制为条形图(来自 MCsubsetDT1112 )。我将 WaterType 映射到 color (在图例中标记为“水类型”),SubstrateConcat 到 linetype (在图例中标记为“基材”),并在 geom_bar 中情节,我将 SubstrateConcat 映射到 fill (在图例中标记为 delta-T 底物)。
Plot1 <- ggplot(MCsubsetTemp1112, aes(x=Date, y=Mean, group=SiteSub,  color=WaterType, linetype=SubstrateConcat)) + 
geom_line(size=1) +
geom_hline(yintercept=20, linetype="dashed") +
scale_x_date(labels=date_format("%b %Y")) +
ylim(-5,35) +
labs(x= "Date", y=expression("Temperature " ( degree~C)), color="Water Type", linetype="Substrate") +
ggtitle("Daily Mean Temperatures of Two Sites and delta-T") +
scale_linetype_manual(values=c("solid", "dashed", "dotdash"), labels=c("Bottom", "Column", "Surface")) +
scale_color_manual(values=c("darkblue", "maroon"), labels=c("non-Warm", "Warm")) +
guides(linetype=guide_legend(override.aes=list(fill=NA)),
color=guide_legend(override.aes=list(fill=NA))) +
theme(
plot.title=element_text(color="black", size=16, face="bold"),
axis.line=element_line(color="black"),
axis.title=element_text(color="black", size=16),
axis.text=element_text(color="black", size=14),
panel.background=element_rect(fill="white"),
legend.background=element_blank(),
legend.text=element_text(color="black"),
legend.key=element_blank())


Plot1 + geom_bar(aes(fill=SubstrateConcat), position="dodge", stat="identity", color="black", data=MCsubsetDT1112) +
scale_fill_manual(values=c("white", "black"), labels=c("Bottom", "Surface")) +
labs(fill="delta-T Substrate") +
theme(legend.background=element_blank(),
legend.text=element_text(color="black"),
legend.key=element_blank())

在上面的代码中, color="black"调用在“基材”图例中产生斜线并填充黑框,该图例应显示各种 linetypes .使用 ggplot legend slashesDifferent Legends for two geom_bar with different data.frames ,我能够从“基材”图例中删除黑色填充,但是斜线仍然存在。我无法弄清楚如何删除这些斜线,因为该图已在上面编码。

我想出的解决方法是绘制 geom_bar首先(见下面的代码)使用 ggplot legend slashes 中的方法.但是,我留下了令人不满意的没有黑色轮廓的白色框。此外,斜线现在从传说中消失了。但是,出现了一个新问题,即 geom_line 中的“SubstrateConcat” plot 需要三个值和作用力 geom_bar绘图,导致错误: Error: Insufficient values in manual scale. 3 needed but only 2 provided.当附加值时,例如 "grey"已添加到 scale_fill_manual调用,绘图已创建,但在“delta-T Substrate”图例中具有 NA 值。
Plot2 <- ggplot(MCdataDT1112, aes(x=Date, y=Mean, group=SiteSub, fill=SubstrateConcat)) +
geom_bar(stat="identity", position="dodge") +
geom_bar(stat="identity", position="dodge", color="black", show_guide=FALSE) +
scale_fill_manual(values=c("white", "black", "grey"), labels=c("Bottom", "Surface")) +
labs(fill="delta-T Substrate") +
theme(legend.background=element_blank(),
legend.text=element_text(color="black"),
legend.key=element_rect(fill="black"))

Plot2 + geom_line(aes(group=SiteSub, color=WaterType, linetype=SubstrateConcat), data=MCdataTemp1112, size=1) +
geom_hline(yintercept=20, linetype="dashed") +
scale_x_date(labels=date_format("%b %Y")) +
ylim(-5,35) +
labs(x= "Date", y=expression("Temperature " ( degree~C)), color="Water Type", linetype="Substrate") +
ggtitle("Daily Mean Temperatures of Two Sites and delta-T") +
scale_linetype_manual(values=c("solid", "dashed", "dotdash"), labels=c("Bottom", "Column", "Surface")) +
scale_color_manual(values=c("darkblue", "maroon"), labels=c("non-Warm", "Warm")) +
guides(linetype=guide_legend(override.aes=list(fill=NA)),
color=guide_legend(override.aes=list(fill=NA))) +
theme(
plot.title=element_text(color="black", size=16, face="bold"),
axis.line=element_line(color="black"),
axis.title=element_text(color="black", size=16),
axis.text=element_text(color="black", size=14),
panel.background=element_rect(fill="white"),
legend.background=element_blank(),
legend.text=element_text(color="black"),
legend.key=element_blank())

如何修改我的代码,以便第二个图不会修改第一个图的图例生成。我是 R 和 ggplot2 的新手;我仍在弄清楚当数据被绘制时会发生什么,就像我上面所做的那样。

我想要以下一项或两项:

在图例中不带斜线绘制 1。

带有 delta-T 图例的图 2 仅显示“底部”的白色和“表面”的黑色,并带有轮廓线。

请解释发生了什么,一个情节修改了它之前的情节图例。谢谢!

最佳答案

使用你在聊天中为我提供的完整数据,如果我运行这个:

MCsubsetDT1112 <- read.csv("~/Downloads/MCsubsetDT1112.csv")
MCsubsetTemp1112 <- read.csv("~/Downloads/MCsubsetTemp1112.csv")

ggplot() +
geom_bar(data = MCsubsetDT1112,
aes(x=as.Date(Date,"%m/%d/%Y"), y=Mean, group=SiteSub, fill=SubstrateConcat),
stat="identity", position="dodge") +
geom_bar(data = MCsubsetDT1112,
aes(x=as.Date(Date,"%m/%d/%Y"), y=Mean, group=SiteSub, fill=SubstrateConcat),
stat="identity", position="dodge", color="black", show_guide=FALSE) +
geom_line(aes(x=as.Date(Date,"%m/%d/%Y"),y=Mean,group=SiteSub, color=WaterType, linetype=SubstrateConcat),
data=MCsubsetTemp1112, size=1) +
geom_hline(yintercept=20, linetype="dashed") +
ylim(-5,35) +
labs(x= "Date", y=expression("Temperature " ( degree~C)), color="Water Type",
linetype="Substrate",fill="delta-T Substrate") +
scale_linetype_manual(values=c("solid", "dashed", "dotdash"),
labels=c("Bottom", "Column", "Surface")) +
scale_x_date(labels=date_format("%b %Y")) +
scale_color_manual(values=c("darkblue", "maroon"), labels=c("non-Warm", "Warm")) +
scale_fill_manual(values=c("white", "black","grey"), labels=c("Bottom", "Surface")) +
guides(linetype=guide_legend(override.aes=list(fill=NA)),
color=guide_legend(override.aes=list(fill=NA)),
fill = guide_legend(override.aes = list(color = "black"))) +
theme(plot.title=element_text(color="black", size=16, face="bold"),
axis.line=element_line(color="black"),
axis.title=element_text(color="black", size=16),
axis.text=element_text(color="black", size=14),
panel.background=element_rect(fill="white"),
legend.background=element_blank(),
legend.text=element_text(color="black"),
legend.key = element_rect(colour = "black",fill = NA))

我得到以下图表:

enter image description here

我添加了 as.Date()调用只是因为从 csv 读入时丢失了日期结构。

关于r - 图例与来自不同数据帧的多个图的交互作用 : How does one deal with this interaction?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21166442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com