jtm
jtm

Reputation: 45

Changing legend shapes to match plot (ggplot2)

I am attempting to change the legend in ggplot2 to match my actual plot. I have a feeling this is proving difficult as I am not using grouping. I have plotted two separate variables (VO2 and VCO2) and one variable as a line (WorkRate). I have also manually set colours and shapes. As you can see, the legend is stuck on the default shapes. I am somewhat new to ggplot and R in general, so the following code may be horrible. Nonetheless, any assistance would be greatly appreciated.

VO2VCO2 <- ggplot(parvo_data, aes(x=Time)) +
  geom_point(aes(y=VO2, colour="VO2"), size= 4, alpha = 1, shape = 1) +
  geom_point(aes(y=VCO2, colour="VCO2"), size= 4, alpha = 0.5, shape = 15) +
  labs(colour ="Type", 
       x="Time(mins)",
       y="Vol.(L.min)",
       title="VO2/VCO2") +
  theme(plot.title = element_text(hjust = 0.5)) +
  expand_limits(x = 0, y = 0) +
  scale_colour_manual(values=c("blue", "red", "magenta")) +
  theme(legend.position=c(0.1, 0.9)) +
  geom_vline(xintercept=parvo_data$Time[parvo_data$VO2==max(parvo_data$VO2)], linetype= "dashed") +
  geom_hline(yintercept=c(max(parvo_data$VO2)), linetype="dashed") + #dotted line for pred VO2max
  geom_text(aes(0,(max(parvo_data$VO2)),label = "VO2pred", vjust = -1)) +
  geom_vline(xintercept=c(0, 0.5), linetype="dashed") + #dotted lines indicating warm up period
  scale_y_continuous(breaks=seq(0,5,0.5), sec.axis = sec_axis(~.*100, name= "Work Rate (W)", breaks = seq(0,450,50))) +
  geom_line(aes(y=WorkRate / 100, colour="WR"))

This is my current plot.

enter image description here

Data is below:

structure(list(Time = c(0.191, 0.397667, 0.531833, 0.699, 0.836, 
1.061, 1.223667, 1.348833, 1.538, 1.694, 1.835833, 2.032667, 
2.190334, 2.352, 2.528, 2.679667, 2.843833, 3.001833, 3.174333, 
3.342333, 3.501666, 3.679833, 3.836333, 4.023166, 4.176332, 4.355666, 
4.460999), VO2 = c(0.003425, 0.211259, 0.601556, 0.571776, 0.708932, 
0.709895, 0.815558, 0.877767, 1.391387, 1.384706, 2.294108, 2.25044, 
3.146537, 2.888784, 3.228814, 3.001643, 3.405195, 3.434325, 3.553129, 
3.517707, 3.542363, 3.783896, 3.612951, 3.601732, 3.823555, 3.354527, 
3.231447), VCO2 = c(0.175524, 0.173959, 0.505978, 0.479254, 0.593675, 
0.591813, 0.664213, 0.710146, 1.167262, 1.2205, 1.917673, 1.920305, 
2.78258, 2.744944, 3.283684, 3.217068, 3.757941, 3.911467, 4.210799, 
4.236459, 4.348312, 4.678401, 4.510573, 4.467245, 4.702829, 4.143716, 
3.850687), WorkRate = c(80, 80, 80, 80, 80, 80, 120, 120, 120, 
120, 120, 120, 160, 160, 160, 160, 160, 160, 200, 200, 200, 200, 
200, 200, 240, 0, 0)), row.names = 4:30, class = "data.frame")

Upvotes: 0

Views: 156

Answers (1)

Seshadri
Seshadri

Reputation: 679

I think the following code produced the desired effect.

The key was to use the "guide" to turn off the legends for shape and linetype, and manually added the shape and linetype properties to the color legend.

VO2VCO2 <- ggplot(parvo_data, aes(x=Time)) +
  geom_point(aes(y=VO2, colour="VO2", shape = "VO2"), size= 4, alpha = 1,) +
  geom_point(aes(y=VCO2, colour="VCO2", shape = "VCO2"), size= 4, alpha = 0.5) +
  geom_line(aes(y=WorkRate / 100, colour="WR")) + 

  labs(color = "Type",
      x="Time(mins)",
       y="Vol.(L.min)",
       title="VO2/VCO2") +
  theme(plot.title = element_text(hjust = 0.5)) +
  expand_limits(x = 0, y = 0) +
  scale_colour_manual(values=c("blue", "red", "magenta")) +
  scale_shape_manual(values=c(1, 15, 20)) +
  geom_vline(xintercept=parvo_data$Time[parvo_data$VO2==max(parvo_data$VO2)], linetype= "dashed") +
  geom_hline(yintercept=c(max(parvo_data$VO2)), linetype="dashed") + #dotted line for pred VO2max
  geom_text(aes(0,(max(parvo_data$VO2)),label = "VO2pred", vjust = -1)) +
  geom_vline(xintercept=c(0, 0.5), linetype="dashed") + #dotted lines indicating warm up period
  scale_y_continuous(breaks=seq(0,5,0.5), sec.axis = sec_axis(~.*100, name= "Work Rate (W)", breaks = seq(0,450,50))) + 
  guides(shape = F, linetype = F, color = guide_legend(override.aes = list(shape = c(1,15,NA), linetype = c("blank","blank","solid"))))

enter image description here

Upvotes: 2

Related Questions