簡體   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