gpt4 book ai didi

r - Vioplot R : How to set axis labels

转载 作者:行者123 更新时间:2023-12-04 04:37:55 27 4
gpt4 key购买 nike

我有一个数据框 mdata看起来像:

>head(mdata)

ID variable value
SJ5444_MAXGT coding 4.241920
SJ5426_MAXGT coding 4.254331
HR1383_MAXGT coding 4.244994
HR5522_MAXGT missense 4.250347
CH30041_MAXGT missense 4.303174
SJ5438_MAXGT utr.3 4.242218

我正在尝试绘制这样的 fiddle 图:
x1<- mdata$value[mdata$variable=='coding']
x2<- mdata$value[mdata$variable=='missense']
x3<- mdata$value[mdata$variable=='utr.3']

vioplot(x1, x2, x3, names=as.character(unique(mdata$variable)), col="red")
title("Violin Plot: Log10 values")

但我有另一个数据框 ndata看起来像:
>head(ndata)

ID variable value
SJ5444_MAXGT coding 17455
SJ5426_MAXGT coding 17961
HR1383_MAXGT coding 17579
HR5522_MAXGT missense 17797
CH30041_MAXGT missense 20099
SJ5438_MAXGT utr.3 17467

基本上 mdata$value 是:
mdata$value = log10(ndata$value)

所以我可以让 fiddle 情节好起来。但我需要更改 Y 轴标签以匹配 ndata$value 而不是 mdata$value。我正在绘制 mdata$value 但想要从 ndata$value 中获取 Y 轴标签。仅供引用,这是实际数据的一个子集,实际数据中的最小值和最大值是 12 和 36937,我知道如何使用以下方法将其绘制在箱线图上:
axis(side=2,labels=round(10^(seq(log10(min(ndata$value)),log10(max(ndata$value)),len=5))),at=seq(log10(min(ndata$value)),log10(max(ndata$value)),len=5))

但我无法绘制 Y 轴标签以匹配 fiddle 图中的 ndata$value。有什么建议吗?

附言我找不到标签 vioplotviolinplot所以我无法标记它。

最佳答案

vioplot不是很灵活——它不允许你关闭轴标签或修改它们——但是你可以先创建自己的空图,然后用 vioplot(...,add=TRUE) 添加 fiddle 图到它。 ,然后手动添加标签,如下所示:

## make up data
set.seed(101)
x1 <- rlnorm(1000,meanlog=3,sdlog=1)
x2 <- rlnorm(1000,meanlog=3,sdlog=2)
x3 <- rlnorm(1000,meanlog=2,sdlog=2)

现在创建情节:
library(vioplot)
par(las=1,bty="l") ## my preferred setting
## set up empty plot
plot(0:1,0:1,type="n",xlim=c(0.5,3.5),ylim=range(log10(c(x1,x2,x3))),
axes=FALSE,ann=FALSE)
vioplot(log10(x1),log10(x2),log10(x3),add=TRUE)
axis(side=1,at=1:3,labels=c("first","second","third"))
axis(side=2,at=-2:4,labels=10^(-2:4))

enter image description here

或者,您可以使用 ggplot2::geom_violin()连同 scale_y_log10() (我认为)。

关于r - Vioplot R : How to set axis labels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19416768/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com