4
votes

A reproducible subset of my dataframes that contain data I wish to plot:

# 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")

I plotted the data with the temperature data as lines (from MCsubsetTemp1112) and delta-T as bars (from MCsubsetDT1112). I mapped WaterType to color (labeled in legend as "Water Type"), SubstrateConcat to linetype (labeled in legend as "Substrate"), and in the geom_bar plot, I mapped SubstrateConcat to fill (labeled in legend as 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())

In the code above, the color="black" call produced slashes through, and filled black boxes, in the "Substrate" legend which should show various linetypes. Using ggplot legend slashes and Different Legends for two geom_bar with different data.frames, I was able to remove the black fill from the "Substrate" legend, however the slashes are still there. I cannot figure how to remove these slashes as the plot is coded above.

A workaround I came up with was to plot geom_bar first (see code below) using the methods in ggplot legend slashes. However, I am left with the unsatisfying white box with no black outline. Furthermore, the slashes are now gone from the legend. But, a new problem arises in that the "SubstrateConcat" in the geom_line plot calls for three values and forces that upon the geom_bar plot, resulting in the error: Error: Insufficient values in manual scale. 3 needed but only 2 provided. When an additional value, e.g. "grey" is added to the scale_fill_manual call, the plot is created but with the NA value in the "delta-T Substrate" legend.

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()) 

How can I modify my code such that the legend produce for the first plot is not modified by the second plot. I am new to R and ggplot2; I'm still figuring out exactly what happens when data are plotted such as I have done above.

I would like one or both of the following:

Plot 1 without the slashes through the legends.

Plot 2 with the delta-T legend that shows only white for "Bottom" and black for "Surface" with the bars outlined.

Please explain what is happening that one plot modifies the legend of the plot before it. Thank you!

1
This is a very small subset of my data, but I think it's enough to understand what I'm looking for. If not, I can edit/paste more data.BgnR

1 Answers

5
votes

Using the full data that you provided for me in chat, if I run this:

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))

I get the following graph:

enter image description here

I added the as.Date() calls just because the date structure was lost when reading in from csv.