You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术求助:如何增大Tendril包生成的卷须图宽度?

调整Tendril包卷须图宽度的解决方案

你遇到的问题是plot.margin仅调整绘图区域的外边距,无法改变图本身的宽度。以下几种方法可以有效增大卷须图的宽度:

方法1:调整宽高比

通过theme()中的aspect.ratio参数控制图的宽高比例,设置小于1的值即可让图横向拉伸变宽:

library(Tendril)
library(Tplyr)
library(dplyr)

adae <- Tplyr::tplyr_adae %>% filter(SAFFL=='Y' & TRTAN %in% c(0, 54)) %>% select(USUBJID, TRTA, ASTDY, AEDECOD) %>% 
  arrange(USUBJID, TRTA, AEDECOD, ASTDY) %>% 
  rename_all(tolower) %>% 
  as.data.frame()

aelist <- table(adae$aedecod, adae$trta) %>% as.data.frame() %>% group_by(Var1) %>% filter(all(Freq>0)) %>% distinct() %>% 
  pull(Var1)

adae <- adae %>% filter(aedecod %in% aelist)

SubjList <- adae %>% select(usubjid, trta) %>% distinct() %>% as.data.frame()

data <- Tendril(mydata = adae,
                rotations = 1,
                AEfreqThreshold = 1,
                Tag = "EudraCT",
                Treatments = c("Placebo", "Xanomeline Low Dose"),
                Unique.Subject.Identifier = "usubjid",
                Terms = "aedecod",
                Treat = "trta",
                StartDay = "astdy",
                SubjList = SubjList,
                SubjList.subject = "usubjid",
                SubjList.treatment = "trta",
                compensate_imbalance_groups = T,
                filter_double_events = T,
                suppress_warnings = TRUE
)

# 调整宽高比,同时保留原margin设置
tendril_plot <- plot(data) +
  theme(
    plot.margin = margin(t = 1, r = 1, b = 1, l = 1, unit = "pt"),
    aspect.ratio = 0.3  # 值越小,图越宽
  )

print(tendril_plot)

方法2:导出时指定尺寸

使用ggsave()保存图片时直接设置宽度和高度,这是最直接控制输出尺寸的方式:

# 生成图后直接指定尺寸保存
ggsave("tendril_plot.png", tendril_plot, width = 12, height = 8, dpi = 300)

默认单位为英寸,可根据需求调整width和height的数值。

方法3:优化x轴元素适配宽度

如果x轴标签拥挤导致图视觉上偏窄,可同时调整标签样式和边距,配合宽高比优化:

tendril_plot <- plot(data) +
  theme(
    plot.margin = margin(t = 10, r = 10, b = 30, l = 10, unit = "pt"),  # 增加底部边距容纳旋转后的标签
    axis.text.x = element_text(angle = 45, hjust = 1, size = 8),  # 旋转x轴标签并缩小字号
    aspect.ratio = 0.4
  )

内容的提问来源于stack exchange,提问作者jkatam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 17:15:15