如何根据系数重新组织R中的绘图

问题描述 投票:0回答:1

我尝试使用 R 中的plot()函数绘制模型的聚合二项式回归估计。绘图代码如下:

par(mar=c(10,6,2,2), mgp=c(3, 1, 0))
plot(lasso.model.bin.df.nozero$rank, lasso.model.bin.df.nozero$lasso.coef, col=lasso.model.bin.df.nozero$color, pch=16, las=3, ylab="Aggregate binomial regression estimate\n(verbs retained by lasso regularisation)", xaxt="n", xlab="", main="", cex.main=1)
abline(h=lasso.model.bin.intercept, lty="longdash", col="black")
abline(v=1:nrow(lasso.model.bin.df.nozero),lty="dotted", col="grey")
axis(side=1, at=c(1:nrow(lasso.model.bin.df.nozero)), labels=lasso.model.bin.df.nozero$verb, las=2, cex.axis=0.75) 

结果图如下:

我想知道如何翻转绘图以使其垂直显示。我知道可以使用 ggplot 中的 coord_flip() 来实现,但我不知道如何在基础包中执行此操作。另外,我想知道如何根据估计的大小(即“lasso.coef”)重新组织 x 轴,以便具有最高估计的级别位于顶部,然后是其他级别的降序方式。原始数据集可以在此 OSF link 中找到。

r plot visualization
1个回答
0
投票

这样可以吗 套索.model.bin.df.nozero <- read.csv("~/Desktop/Job/lasso.model.bin.df.nozero.csv") View(lasso.model.bin.df.nozero)

# Check if 'lasso.model.bin.intercept' is present in the data frame
if ("lasso.model.bin.intercept" %in% colnames(lasso.model.bin.df.nozero)) {
  intercept_value <- lasso.model.bin.df.nozero$lasso.model.bin.intercept
} else {
  intercept_value <- 0  # Set a default value if not found
}

# Reorganize the data frame based on 'lasso.coef'
order_indices <- order(lasso.model.bin.df.nozero$lasso.coef, decreasing = TRUE)
ordered_data <- lasso.model.bin.df.nozero[order_indices, ]

# Flip the plot by swapping x and y values
par(mar=c(10,6,2,2), mgp=c(3, 1, 0))
plot(rev(ordered_data$lasso.coef), ordered_data$rank, col=ordered_data$color, pch=16, las=3, xlab="Aggregate binomial regression estimate\n(verbs retained by lasso regularisation)", yaxt="n", ylab="", main="", cex.main=1)
abline(h=intercept_value, lty="longdash", col="black")
abline(v=1:nrow(ordered_data),lty="dotted", col="grey")
axis(side=2, at=c(1:nrow(ordered_data)), labels=ordered_data$verb, las=2, cex.axis=0.75) 

[![enter image description here][1]][1]
© www.soinside.com 2019 - 2024. All rights reserved.