我的数据框的可重现子集,包含我想要绘制的数据:
# 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
(在图例中标记为“Water Type”),将SubstrateConcat映射到linetype
(在图例中标记为“Substrate”),在geom_bar
图中,我映射了SubstrateConcat到fill
(在图例中标记为delta-T Substrate)。
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"
调用在“Substrate”图例中生成了斜杠,并填充了黑框,该图例应显示各种linetypes
。使用ggplot legend slashes和Different Legends for two geom_bar with different data.frames,我可以从“Substrate”图例中删除黑色填充,但是斜杠仍在那里。我无法想象如何删除这些斜线,因为上面的图是编码的。
我提出的解决方法是使用ggplot legend slashes中的方法首先绘制geom_bar
(请参阅下面的代码)。但是,我留下了不满意的白色方框,没有黑色轮廓。此外,这些斜线现在已经从传奇中消失了。但是,出现了一个新问题,geom_line
图中的“SubstrateConcat”需要三个值并强制在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,仅显示白色表示“底部”,黑色表示“表面”,并标有条形图。
请解释一个情节修改之前的情节图例的情况。谢谢!
答案 0 :(得分:5)
使用您在聊天中为我提供的完整数据,如果我运行它:
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))
我得到以下图表:
我添加了as.Date()
个调用只是因为从csv读入时日期结构丢失了。