R - 有没有办法像在 python 中使用“with”语句那样在 R 中临时设置上下文特定的绘图参数?

问题描述

在 Python 中,您可以使用 with 语句设置上下文特定(即临时更改)绘图参数,如下所示。
问题 2 :在 R 中也可以这样做吗?

我当前的代码:

# save default parameters
defop = options()

# sunction to set plot parameters
plot_pars = function(w=7,h=7) {options(repr.plot.width=w,repr.plot.height=h)}

# set plot size
plot_pars(10,4)

# generate boxplot
boxplot(Outstate ~ Elite,data=college,horizontal=T,col=5:6,xlab="Elite",ylab="Outstate tuition (USD)")

# reset parameters
options(defop)

每次更改绘图参数时,我都必须重置它们。必须有办法避免这种情况。

enter image description here

问题 2: 是否有类似于 Python 中 with 的功能/设备?

Python 代码:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import requests

## import data
url = "https://statlearning.com/College.csv"  # ISLR repository
df = pd.read_csv(url)
## generate boxplot with specified dimensions
with plt.rc_context():
    plt.figure(figsize=(10,3))
    sns.boxplot(y='Private',x='Outstate',data=df);
# boxplot generated with specified dimensions

enter image description here

## generate boxplot normally
sns.boxplot(y='Private',data=df);
# boxplot generated with default dimensions
# defaults were not changed as the plt.figure() was
# within 'with' container

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)