R 更改图例形状以匹配打印(ggplot2)

R 更改图例形状以匹配打印(ggplot2),r,ggplot2,R,Ggplot2,我正在尝试更改ggplot2中的图例以匹配我的实际绘图。我觉得这很难,因为我没有使用分组。我画了两个独立的变量(VO2和VCO2),一个变量作为一条线(工作速率)。我还手动设置了颜色和形状。如您所见,图例固定在默认形状上。一般来说,我对ggplot和R有些陌生,所以下面的代码可能很糟糕。尽管如此,任何援助都将不胜感激 VO2VCO2 <- ggplot(parvo_data, aes(x=Time)) + geom_point(aes(y=VO2, colour="VO2"), siz

我正在尝试更改ggplot2中的图例以匹配我的实际绘图。我觉得这很难,因为我没有使用分组。我画了两个独立的变量(VO2和VCO2),一个变量作为一条线(工作速率)。我还手动设置了颜色和形状。如您所见,图例固定在默认形状上。一般来说,我对ggplot和R有些陌生,所以下面的代码可能很糟糕。尽管如此,任何援助都将不胜感激

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

我认为下面的代码产生了预期的效果

关键是使用“指南”关闭形状和线型的图例,并手动将形状和线型特性添加到颜色图例中

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

VO2VCO2我认为下面的代码产生了预期的效果

关键是使用“指南”关闭形状和线型的图例,并手动将形状和线型特性添加到颜色图例中

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