繁体   English   中英

facet的不同geom_rect()对象

[英]Different geom_rect() objects for facets

我有一个数据框,我用它来创建一个ggplot对象,分成三个独立的图。

max_24h_lactate_cpet.long

First_24h_Lactate_Max,  Lactate_Above_Threshold,           Metric,           Value
2.3,                    High,                              AT_VO2_mL.kg.min, 17.00
2.3,                    High,                              VO2_Peak,         84.07
2.3,                    High,                              AT_VE_VCO2,       35.00

在输入格式:

dput(max_24h_lactate_cpet.long)
structure(list(First_24h_Lactate_Max = c(2.3, 2.3, 2.3), Lactate_Above_Threshold = structure(c(1L, 
1L, 1L), 
.Label = c("High", "Normal"), class = "factor"), Metric = structure(1:3, .Label = c("AT_VO2_mL.kg.min", 
"VO2_Peak", "AT_VE_VCO2"), class = "factor"), Value = c(17, 84.07, 
35)), .Names = c("First_24h_Lactate_Max", "Lactate_Above_Threshold", 
"Metric", "Value"), row.names = c(44L, 192L, 340L), class = "data.frame")

我想在每个方面上放置geom_rect()对象,但每个绘图的ymin和ymax值不同。

这是我目前的代码:

max_24h_lac_vs_cpet <- ggplot(max_24h_lactate_cpet.long, 
                              aes(x = max_24h_lactate_cpet.long$First_24h_Lactate_Max, 
                                  y = max_24h_lactate_cpet.long$Value))

max_24h_lac_vs_cpet + geom_point() +
  facet_wrap( ~ Metric, scales="free_y") +
  scale_color_brewer(palette="Set1") +
  labs(x = "Max Lactate Value < 24h after surgery (mmol)", 
       y = "Test Metric Value") +
  stat_smooth(method="lm") +
  annotate("rect", xmin=-Inf, xmax=1.6, ymin=-Inf, ymax=Inf,alpha=0.1,fill="blue")

这给出了以下图:

部分完成图

我在一个单独的数据帧中得到了我的阈值(geom_rect()对象的x和y限制),如下所示:

    Metric              xmin    xmax    ymin    ymax
    AT_VO2_mL.kg.min    -Inf    Inf     -Inf    10.2
    VO2_Peak            -Inf    Inf     -Inf    75.0
    AT_VE_VCO2          -Inf    Inf     42      Inf

输入代码:

dput(thresholds)
structure(list(Metric = structure(c(2L, 3L, 1L), .Label = c("AT_VE_VCO2", 
"AT_VO2_mL.kg.min", "VO2_Peak"), class = "factor"), xmin = c(-Inf, 
-Inf, -Inf), xmax = c(Inf, Inf, Inf), ymin = c(-Inf, -Inf, 42
), ymax = c(10.2, 75, Inf)), .Names = c("Metric", "xmin", "xmax", 
"ymin", "ymax"), class = "data.frame", row.names = c(NA, -3L))

并已将此代码段添加到我的ggplot调用中

 + geom_rect(data=thresholds$Metric, aes(xmin=xmin, xmax=xmax, 
                                         ymin=ymin, ymax=ymax, 
                                         alpha=0.1,fill="red"))

这给出了如下错误:

错误:ggplot2不知道如何处理类因子的数据

使用以下内容也会出错:

 + geom_rect(data=thresholds, aes(xmin=xmin, xmax=xmax, 
                                  ymin=ymin, ymax=ymax, 
                                  alpha=0.1,fill="red"))

错误:美学必须是长度为1或与dataProblems相同的长度:xmin,xmax,ymin,ymax

我看过其他 问题的例子,但我正在努力将他们的答案转化为我自己的问题。 任何帮助,将不胜感激!

因此你没有给我们提供labels ,只有第一组数据的三行,所以下面的内容是不完整的,但应该演示如何让rect工作:

max_24h_lac_vs_cpet <- ggplot(max_24h_lactate_cpet.long, 
                              aes(x = First_24h_Lactate_Max, 
                                  y = Value))

max_24h_lac_vs_cpet + geom_point() +
  facet_wrap( ~ Metric, scales="free_y") +
  scale_color_brewer(palette="Set1") +
  labs(x = "Max Lactate Value < 24h after surgery (mmol)", 
       y = "Test Metric Value") +
  stat_smooth(method="lm") + 
  geom_rect(data=thresholds, aes(x = NULL,y = NULL,xmin=xmin, xmax=xmax, 
                                  ymin=ymin, ymax=ymax, 
                                  alpha=0.1,fill="red"))

您在第一次aes()调用中使用$ 永远不要那样做。 然后,您需要在geom_rect图层中取消映射xy ,因为它们是从顶级ggplot调用继承的。 另一种选择是使用inherit.aes = FALSE

暂无
暂无

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

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