首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在x轴比例相同的情况下同时绘制两个图形

如何在x轴比例相同的情况下同时绘制两个图形
EN

Stack Overflow用户
提问于 2021-04-14 05:26:28
回答 1查看 42关注 0票数 0

我想把两个共用同一个x轴的图形画在一起。我该怎么做呢?我的数据可以使用代码构建:

代码语言:javascript
复制
df <-structure(list(SDTM_LabN = c("ALP", "AST", "ALT", "AST", "ALT", 
"ALT", "ALP", "AST", "ALP", "AST", "ALP", "ALT", "ALP", "ALP", 
"ALT", "AST", "ALT", "ALT", "ALT", "AST", "AST", "ALP", "AST", 
"ALT", "ALP", "ALP", "AST"), ADY = structure(c(45, 15, 1, 1, 
30, 58, 30, 45, 46, -6, 23, 46, -6, 15, 23, 46, 45, -6, 8, 30, 
58, 58, 23, 15, 8, 1, 8), class = "difftime", units = "days"), 
    result = c(0.841269841269841, 0.578947368421053, 0.625, 0.552631578947368, 
    0.416666666666667, 0.3125, 0.936507936507937, 0.447368421052632, 
    0.634920634920635, 0.657894736842105, 0.873015873015873, 
    0.291666666666667, 0.73015873015873, 0.857142857142857, 0.5, 
    0.447368421052632, 0.479166666666667, 0.625, 0.604166666666667, 
    0.5, 0.526315789473684, 0.849206349206349, 0.526315789473684, 
    0.5, 1.00793650793651, 0.896825396825397, 0.894736842105263
    )), row.names = c(NA, -27L), class = "data.frame")

df2<-structure(list(ID = c(101, 101, 101, 101, 101, 101), AEDECOD = c("Diarrhoea", 
"Vitreous floaters", "Musculoskeletal pain", "Diarrhoea", "Decreased appetite", 
"Fatigue"), AESTDY = structure(c(101, 74, 65, 2, 33, 27), class = "difftime", units = "days"), 
    AEENDY = structure(c(105, 99, NA, 5, NA, NA), class = "difftime", units = "days")), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

我的绘图代码是:

代码语言:javascript
复制
ggplot(df, aes(colour=SDTM_LabN)) + 
    geom_line(aes(x=ADY,y=result)) 

ggplot(df2, aes(colour=AEDECOD)) + 
    geom_segment(aes(x=AESTDY, xend=AEENDY, y=AEDECOD, yend=AEDECOD),) +
    xlab("Duration")

我怎样才能得到像这样的东西:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-14 05:49:05

您应该首先确保计算两个系列的公共xmin-xmax。

然后在comments或cowplot中使用patwhwork a建议

代码语言:javascript
复制
xmin <- min(df$ADY ,df2$AESTDY)
xmax <- max(df$ADY ,df2$AESTDY)

p1 <- ggplot(df, aes(colour=SDTM_LabN)) + 
        geom_line(aes(x=ADY,y=result)) +
        coord_cartesian(xlim = c(xmin,xmax))


p2 <- ggplot(df2, aes(colour=AEDECOD)) + 
        geom_segment(aes(x=AESTDY, xend=AEENDY, y=AEDECOD, yend=AEDECOD),) +
        xlab("Duration") + 
        coord_cartesian(xlim = c(xmin,xmax))

library(cowplot)
plot_grid(plotlist = list(p1,p2),align='v',ncol=1)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67082615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档