facet不同的geom_rect()对象 [英] Different geom_rect() objects for facets

查看:159
本文介绍了facet不同的geom_rect()对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  max_24h_lactate_cpet.long 

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

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

输入格式:

<$ p code> 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()对象,但每个plot的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<手术后24小时(mmol),
y =测试度量值)+
stat_smooth(method =lm)+
annotate(rect,xmin = -Inf,xmax = 1.6,ymin = -Inf,ymax = Inf,alpha = 0.1,fill =blue)

这给出了以下情节:



我已经在单独的数据框中获得了我的阈值(geom_rect()对象的x和y限制),如下所示:

 公制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

产品代码:

<$ p(p(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,I nf),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调用中


$ b $

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

其中给出的错误如下:


错误:ggplot2不知道如何处理class factor的数据

使用下面的代码也会给出一个错误:

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




r:美学要么是长度为1,要么是与
dataProblems相同的长度:xmin,xmax,ymin,ymax

我看过其他 问题,但我努力翻译他们的答案对我自己的问题。任何帮助,将不胜感激!

解决方案

所以你没有向我们提供标签,并且只有三行第一个数据集,所以接下来是不完整的,但应该演示如何获得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 =最大乳酸值<手术后24h(mmol),$ b $通过=测试度量值)+
stat_smooth(method =lm)+
geom_rect(x = NULL,y = NULL,xmin = xmin,xmax = xmax,
ymin = ymin,ymax = ymax,
alpha = 0.1,fill =red))

你是在第一个 aes()调用中使用 $ 从不那样做。然后,您需要在 geom_rect x y 因为它们是从顶级 ggplot 调用继承的。另一种选择是使用 inherit.aes = FALSE


I have a dataframe which I've used to create a ggplot object faceted into three separate plots.

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

In dput format:

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

I want to put geom_rect() objects on each of these facets, but with different ymin and ymax values for each plot.

Here's my current code:

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

This gives the following plot:

I've got my thresholds (x and y limits for geom_rect() objects) in a separate dataframe as follows:

    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 code:

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

And have added this code snippet to my ggplot call

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

Which gives an error as follows:

Error: ggplot2 doesn't know how to deal with data of class factor

Using the following also gives an error:

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

Error: Aesthetics must either be length one, or the same length as the dataProblems:xmin, xmax, ymin, ymax

I've looked at examples on other questions, but am struggling to translate their answers to my own problem. Any help would be appreciated!

解决方案

So you didn't provide us with labels, and only three rows of the first data set, so what follows is incomplete, but should demonstrate how to get the rect's working:

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

You were using $ in the first aes() call. Never do that. Then you need to un-map x and y in the geom_rect layer, since they are inherited from the top level ggplot call. The other option would be to use inherit.aes = FALSE.

这篇关于facet不同的geom_rect()对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆