我正在尝试更改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")发布于 2020-01-23 10:57:23
我认为下面的代码产生了预期的效果。
关键是使用“向导”关闭造型和线型的图例,并手动将造型和线型特性添加到颜色图例中。
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"))))

https://stackoverflow.com/questions/59870445
复制相似问题