简体   繁体   English

使用 ggsave 将图例保存到箱线图

[英]Save a legend to a Boxplot with ggsave

I'm pretty new to R and have a question regarding ggsave.我对 R 很陌生,并且对 ggsave 有疑问。 I use the following code to first create a box plot, then add a bee swarm plot on top of it and save it with ggsave.我使用下面的代码首先创建一个盒子 plot,然后在上面添加一个蜂群 plot 并用 ggsave 保存。 Now, I would like to add a p-value on the bottom of the plot.现在,我想在 plot 的底部添加一个 p 值。 However, I don't know how to include it in the script for it to be saved.但是,我不知道如何将它包含在脚本中以便保存。 I tried adding "+" or "," but it never showed in the output pdf.我尝试添加“+”或“”,但它从未在 output pdf 中显示。 Maybe you can help me?也许你能帮助我? That would be great, thanks: :) Tobey那太好了,谢谢::) 托比

ggsave(filename="test.pdf", plot=print(
    boxplot(X ~ Y, data = df, 
            main = 'title',
            add=FALSE,
            ) +
    beeswarm(X ~ Y, data = df, method="hex",
              corral="random",
              add = TRUE)
            ),
scale=1.5,
dpi=300,
width=10, height=10,
units="cm")

I would now like to add the following legend with the legend() function.我现在想用 legend() function 添加以下图例。

legend("bottom",legend="p=0.014")

Try:尝试:

ggsave(filename="test.pdf", plot=print(
  beeswarm(X ~ Y, data = df, method="hex",
           corral="random",
           add = F),legend("bottom",legend="p=0.014") +
    boxplot(X ~ Y, data = df, 
            add=T)
),
scale=1.5,
dpi=300,
width=10, height=10,
units="cm",
)

Tip for posting in SO: If possible share a sample of your data or simply use dput(df) , where df is your dataset.在 SO 中发布的提示:如果可能,请分享您的数据样本或简单地使用dput(df) ,其中df是您的数据集。

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

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