繁体   English   中英

更改图例形状以匹配绘图 (ggplot2)

[英]Changing legend shapes to match plot (ggplot2)

我正在尝试更改 ggplot2 中的图例以匹配我的实际情节。 我感觉这很困难,因为我没有使用分组。 我已经绘制了两个单独的变量(VO2 和 VCO2)和一个变量作为一条线(WorkRate)。 我还手动设置了颜色和形状。 如您所见,图例卡在默认形状上。 总的来说,我对 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"))

这是我目前的剧情。

在此处输入图片说明

数据如下:

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

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

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

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM