图例与来自不同数据帧的多个图的交互:如何处理这种交互?

Posted

技术标签:

【中文标题】图例与来自不同数据帧的多个图的交互:如何处理这种交互?【英文标题】:Legend interactions with multiple plots from different dataframes: How does one deal with this interaction? 【发布时间】:2014-02-05 15:14:22 【问题描述】:

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

# 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 基板)。

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,我能够从“基板”图例中删除黑色填充,但是斜线仍然存在。我无法弄清楚如何删除这些斜线,因为上面的代码是这样编码的。

我想出的解决方法是首先使用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 调用中,绘图已创建,但 NA 值位于“delta-T Substrate”图例中。

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 仅显示“底部”为白色,“表面”为黑色,并带有条形轮廓。

请解释一个情节修改了之前情节的传说是怎么回事。谢谢!

【问题讨论】:

这是我数据的一个非常小的子集,但我认为这足以理解我在寻找什么。如果没有,我可以编辑/粘贴更多数据。 【参考方案1】:

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

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 读取时丢失了日期结构。

【讨论】:

以上是关于图例与来自不同数据帧的多个图的交互:如何处理这种交互?的主要内容,如果未能解决你的问题,请参考以下文章

Android/Parse-如何处理来自 ParseQuery 的多个回调

大牛教你如何处理好前后端分离的 API 问题

DLL 如何处理来自多个进程的并发?

Arduino如何处理中断?

C++:如何处理 NULL 值(例如来自数据库)?

如何处理圆图的Datachange信号?