【问题标题】:Problem of visualizing a Python written Boxplot in R在 R 中可视化 Python 编写的箱线图的问题
【发布时间】:2021-01-25 00:35:11
【问题描述】:

我想在 Boxplot 中可视化一些数据。我有用 Python 编写的代码,但我想用 R 重写它。

这是 Python 中的代码:

fig, ax = plt.subplots()

d = df.to_numpy()
f = [d[m] for d, m in zip(d.T, ~np.isnan(d).T)]

ax.boxplot(f)
ax.set_ylim([0, 150])
ax.set_ylabel('IRE binding activity (%)', fontsize=14)
ax.set_xticklabels(['NF', 'F'])
ax.tick_params(axis='x', labelsize=14, labelrotation=45)
ax.tick_params(axis='y', labelsize=14)

glue('fig1', fig, display=False)

这是我在 R 中尝试过的:

na_if(d, df)
f <- [d[m] for d, m in zip(d.T, ~np.isnan(d).T)]
boxplot(f)
boxplot(NF ~ F, data = f, col = "lightgray", varwidth = TRUE, 
        main = "IRE binding activity for non-failing (NF) and failing (F) hearts.",
        ylab = "IRE binding activity (%)",xlab = "['NF', 'F']")
fivenum(f)

我的数据代码包含一个 t-test 函数:

labels <- list('non-failing heart (NF)', 'failing heart (F)')

data <- list(c(99, 52), c(96, 40), c(100, 38), c(105, 18), 
             c(NA_integer_, 11), c(NA_integer_, 5), c(NA_integer_, 42), 
             c(NA_integer_, 55), c(NA_integer_, 53), c(NA_integer_, 39),
             c(NA_integer_, 42), c(NA_integer_, 50))

df <- setNames(do.call(rbind.data.frame, 
                       lapply(data, function(d) data.frame(d[1], d[2]))),
              labels)    
df
                           
                           
results <- t.test(df[['non-failing heart (NF)']], df[['failing heart (F)']])

results
                           
results$statistic
results$estimate
results$p.value
                           
ceiling(results$p.value * 1000.0)/ 1000.0

【问题讨论】:

  • 如果没有数据样本,我无法将代码放在一起,但您是否使用ggplot 看过? sthda.com/english/wiki/…
  • 我们没有您的数据。从技术上讲,我不明白你想要完成什么。如果是在 R 中运行 python 脚本,只需使用 reticulate::source_python(your_python_file)
  • @NColl 对不起各位,我忘记放数据了,我只是编辑了我的代码,你可以看看它
  • @Onyambu 我应该重写它 R 而不是使用 reticulate
  • R 没有列表解析。 f &lt;- Filter(Negate(is.na), d) 是您应该拥有的第一个代码

标签: python r python-3.x boxplot


【解决方案1】:

我已经成功重写了R中的代码,我想和你分享解决方案:

boxplot(df[1:2],
        data=df,
        main="box plot",
        ylab="Degree Fahrenheit",
        col="orange",
        border="brown",
        ylim = c(0, 120)
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 2012-08-07
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多