7564 lines
329 KiB
Python
7564 lines
329 KiB
Python
import os
|
||
|
||
import joypy
|
||
import matplotlib.pyplot as plt
|
||
import numpy as np
|
||
import pandas as pd
|
||
import seaborn as sns
|
||
|
||
|
||
def keep_txt_to_file(code, script_path):
|
||
with open(script_path, 'w', encoding='utf-8') as f:
|
||
f.write(code)
|
||
|
||
|
||
def scatter_plot(file_path, chart_out_path, out_path, x_scale_font_angle, y_scale_font_angle, x_column_name,
|
||
y_column_name, group_by_column='category', x_name="x", y_name="y",
|
||
title="chart", dpi=100, width=10, height=6, title_font_size=12, ):
|
||
midwest = pd.read_csv(file_path) # 导入文件
|
||
|
||
plt.figure(figsize=(int(width), int(height)), dpi=int(dpi), facecolor='w', edgecolor='k')
|
||
|
||
categories = np.unique(midwest[group_by_column])
|
||
|
||
colors = [
|
||
plt.cm.Set1(i / float(len(categories) - 1)) for i in range(len(categories))
|
||
]
|
||
|
||
for i, category in enumerate(categories):
|
||
plt.scatter(x_column_name,
|
||
y_column_name,
|
||
data=midwest.loc[midwest.category == category, :],
|
||
s=20,
|
||
color=colors[i],
|
||
label=str(category))
|
||
|
||
plt.gca().set(
|
||
xlim=(0.0, 0.1),
|
||
ylim=(0, 90000),
|
||
)
|
||
|
||
# xy轴的刻度
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
# xy轴的名称
|
||
plt.xlabel(x_name, fontdict={'fontsize': 10})
|
||
plt.ylabel(y_name, fontdict={'fontsize': 10})
|
||
|
||
# 标题
|
||
plt.title(title, fontsize=int(title_font_size))
|
||
|
||
# 加上图例
|
||
plt.legend(fontsize=10)
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
def Each_regression_line_in_its_own_column(file_path, chart_out_path, out_path, group_by_column, group_by_range,
|
||
x_column_name, y_column_name,
|
||
title, title_font_size, x_name, y_name, color):
|
||
df = pd.read_csv(file_path)
|
||
group_by_range = [group_by_range.split("-")[0], group_by_range.split("-")[1]]
|
||
df_select = df.loc[df[group_by_column].isin(group_by_range), :]
|
||
plt.figure(figsize=(5, 3), dpi=200)
|
||
gridobj = sns.lmplot(x=x_column_name,
|
||
y=y_column_name,
|
||
data=df_select,
|
||
robust=True,
|
||
palette=color,
|
||
col=group_by_column,
|
||
scatter_kws=dict(s=60, linewidths=.7, edgecolors='black'))
|
||
|
||
# Decorations
|
||
sns.set(style="whitegrid", font_scale=1.5)
|
||
gridobj.fig.set_size_inches(10, 6)
|
||
|
||
plt.title(title, fontsize=int(title_font_size))
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
|
||
# xy轴的名称
|
||
plt.xlabel(x_name, fontdict={'fontsize': 10})
|
||
plt.ylabel(y_name, fontdict={'fontsize': 10})
|
||
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
def Jittering_with_stripplot(file_path, chart_out_path, out_path, x_column_name, y_column_name,
|
||
title, title_font_size, x_name, y_name, color):
|
||
df = pd.read_csv(file_path)
|
||
fig, ax = plt.subplots(figsize=(5, 3), dpi=180)
|
||
print(fig, ax)
|
||
sns.stripplot(df[x_column_name],
|
||
df[y_column_name],
|
||
jitter=0.25,
|
||
size=8,
|
||
ax=ax,
|
||
linewidth=.5,
|
||
palette=color)
|
||
|
||
sns.set(style="whitegrid", font_scale=1.1)
|
||
|
||
plt.title(title, fontsize=int(title_font_size))
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
|
||
plt.legend(fontsize=10)
|
||
|
||
# xy轴的名称
|
||
plt.xlabel(x_name, fontdict={'fontsize': 10})
|
||
plt.ylabel(y_name, fontdict={'fontsize': 10})
|
||
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
def Marginal_Histogram(file_path, chart_out_path, out_path, x_column_name, y_column_name,
|
||
title, x_name, y_name, chart_bar_x_color='#0078d4', chart_bar_y_color='#098154'):
|
||
df = pd.read_csv(file_path)
|
||
|
||
# Create Fig and gridspec
|
||
fig = plt.figure(figsize=(9.4, 4.25), dpi=200)
|
||
|
||
grid = plt.GridSpec(4, 4, hspace=0.5, wspace=0.2)
|
||
|
||
# Define the axes
|
||
ax_main = fig.add_subplot(grid[:-1, :-1])
|
||
ax_right = fig.add_subplot(grid[:-1, -1], xticklabels=[], yticklabels=[])
|
||
ax_bottom = fig.add_subplot(grid[-1, 0:-1], xticklabels=[], yticklabels=[])
|
||
|
||
# Scatterplot on main ax
|
||
ax_main.scatter(x_column_name,
|
||
y_column_name,
|
||
c=df.manufacturer.astype('category').cat.codes,
|
||
alpha=.9,
|
||
data=df,
|
||
cmap="Set1",
|
||
edgecolors='gray',
|
||
linewidths=.5)
|
||
|
||
# histogram on the right
|
||
ax_bottom.hist(df[x_column_name],
|
||
40,
|
||
histtype='stepfilled',
|
||
orientation='vertical',
|
||
color=chart_bar_x_color)
|
||
ax_bottom.invert_yaxis()
|
||
|
||
# histogram in the bottom
|
||
ax_right.hist(df[y_column_name],
|
||
40,
|
||
histtype='stepfilled',
|
||
orientation='horizontal',
|
||
color=chart_bar_y_color)
|
||
|
||
ax_main.set(title=title,
|
||
xlabel=x_name,
|
||
ylabel=y_name)
|
||
|
||
for item in ([ax_main.xaxis.label, ax_main.yaxis.label] +
|
||
ax_main.get_xticklabels() + ax_main.get_yticklabels()):
|
||
item.set_fontsize(10)
|
||
|
||
xlabels = ax_main.get_xticks().tolist()
|
||
ax_main.set_xticklabels(xlabels)
|
||
|
||
plt.savefig(chart_out_path) # 保存图片
|
||
|
||
plt.legend(fontsize=10)
|
||
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
def Correllogram(file_path, chart_out_path, out_path,
|
||
title, title_font_size, font_size, font_color, weight):
|
||
df = pd.read_csv(file_path)
|
||
plt.figure(figsize=(9.4, 4.25), dpi=200)
|
||
sns.heatmap(
|
||
df.corr(),
|
||
xticklabels=df.corr().columns,
|
||
yticklabels=df.corr().columns,
|
||
cmap='Set1',
|
||
center=0,
|
||
annot=True,
|
||
annot_kws={
|
||
'size': font_size,
|
||
'weight': weight,
|
||
'color': font_color
|
||
},
|
||
)
|
||
|
||
plt.title(title, fontsize=int(title_font_size))
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
|
||
plt.legend(fontsize=10)
|
||
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
def Marginal_Boxplot(file_path, chart_out_path, out_path, x_column_name, y_column_name,
|
||
title, title_font_size, x_scale_font_angle, y_scale_font_angle, x_name, y_name, color):
|
||
# 边缘箱图(Marginal Boxplot)
|
||
df = pd.read_csv(file_path)
|
||
|
||
fig = plt.figure(figsize=(9.4, 4.25), dpi=100)
|
||
grid = plt.GridSpec(
|
||
4, 4, hspace=0.5, wspace=0.2
|
||
)
|
||
|
||
# Define the axes
|
||
ax_main = fig.add_subplot(grid[:-1, :-1])
|
||
ax_right = fig.add_subplot(grid[:-1, -1], xticklabels=[], yticklabels=[])
|
||
ax_bottom = fig.add_subplot(grid[-1, 0:-1], xticklabels=[], yticklabels=[])
|
||
|
||
# Scatterplot on main ax
|
||
ax_main.scatter(x_column_name,
|
||
y_column_name,
|
||
c=df.manufacturer.astype('category').cat.codes,
|
||
alpha=.9,
|
||
data=df,
|
||
cmap="Set1",
|
||
edgecolors='black',
|
||
linewidths=.5)
|
||
|
||
# Add a graph in each part
|
||
sns.boxplot(df[y_column_name], ax=ax_right, orient="v", linewidth=1, palette=color)
|
||
sns.boxplot(df[x_column_name], ax=ax_bottom, orient="h", linewidth=1, palette=color)
|
||
|
||
ax_bottom.set(xlabel='')
|
||
ax_right.set(ylabel='')
|
||
|
||
ax_main.title.set_fontsize(fontsize=int(title_font_size))
|
||
ax_main.set(title=title,
|
||
xlabel=x_name,
|
||
ylabel=y_name)
|
||
|
||
for item in ([ax_main.xaxis.label, ax_main.yaxis.label] +
|
||
ax_main.get_xticklabels() + ax_main.get_yticklabels()):
|
||
item.set_fontsize(11)
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
|
||
plt.legend(fontsize=10)
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
# xy轴的名称
|
||
plt.xlabel(x_name, fontdict={'fontsize': 10})
|
||
plt.ylabel(y_name, fontdict={'fontsize': 10})
|
||
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
def Pairwise_Plot(file_path, chart_out_path, out_path, title, title_font_size, features_column, color):
|
||
df = pd.read_csv(file_path)
|
||
|
||
plt.figure(figsize=(9.4, 4.25), dpi=100)
|
||
|
||
plt.title(title, fontsize=int(title_font_size))
|
||
sns.pairplot(df,
|
||
hue=features_column,
|
||
palette=color,
|
||
plot_kws=dict(s=80, edgecolor="white", linewidth=2.5))
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
def Diverging_Bars(file_path, chart_out_path, out_path, x_column_name, y_column_name,
|
||
title, title_font_size, x_scale_font_angle, y_scale_font_angle, x_name, y_name):
|
||
df = pd.read_csv(file_path)
|
||
x = df.loc[:, [x_column_name]]
|
||
df[x_column_name + "_z"] = (x - x.mean()) / x.std()
|
||
df['colors'] = ['red' if x < 0 else 'green' for x in df[x_column_name + "_z"]]
|
||
df.sort_values(x_column_name + "_z", inplace=True)
|
||
df.reset_index(inplace=True)
|
||
|
||
# Draw plot
|
||
plt.figure(figsize=(9.4, 4.25), dpi=80)
|
||
plt.hlines(y=df.index,
|
||
xmin=0,
|
||
xmax=df.mpg_z,
|
||
color=df.colors,
|
||
alpha=0.8,
|
||
linewidth=5)
|
||
|
||
# Decorations
|
||
plt.gca().set(ylabel=y_name, xlabel=x_name)
|
||
plt.yticks(df.index, df[y_column_name], fontsize=12)
|
||
plt.xticks(fontsize=12)
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.title(title, fontsize=int(title_font_size))
|
||
|
||
plt.grid(linestyle='--', alpha=0.5)
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
def Diverging_Bars_vertical(file_path, chart_out_path, out_path, x_column_name, y_column_name,
|
||
title, title_font_size, x_scale_font_angle, y_scale_font_angle, x_name, y_name):
|
||
df = pd.read_csv(file_path)
|
||
x = df.loc[:, [y_column_name]]
|
||
|
||
df[y_column_name + 'z'] = (x - x.mean()) / x.std()
|
||
df['colors'] = ['red' if x < 0 else 'green' for x in df[y_column_name + 'z']]
|
||
df.sort_values(y_column_name + 'z', inplace=True)
|
||
df.reset_index(inplace=True)
|
||
|
||
# Draw plot
|
||
plt.figure(figsize=(10, 6), dpi=80)
|
||
plt.vlines(x=df.index,
|
||
ymin=0,
|
||
ymax=df[y_column_name + 'z'],
|
||
color=df.colors,
|
||
alpha=0.8,
|
||
linewidth=5)
|
||
|
||
plt.gca().set(ylabel=y_name, xlabel=x_name)
|
||
|
||
for y, x, tex in zip(df[y_column_name + 'z'], df.index, df[y_column_name + 'z']):
|
||
plt.text(x,
|
||
y + 0.2,
|
||
round(tex, 1),
|
||
horizontalalignment='center',
|
||
fontdict={
|
||
'color': 'black' if x < 0 else 'black',
|
||
'size': 8
|
||
})
|
||
|
||
# Decorations
|
||
plt.xticks(df.index, df[x_column_name], fontsize=12, rotation=90)
|
||
plt.yticks(fontsize=12)
|
||
plt.title(title, fontdict={'size': int(title_font_size)})
|
||
plt.grid(linestyle='--', alpha=0.5)
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.title(title, fontsize=int(title_font_size))
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
def Ordered_Bar_Chart(file_path, chart_out_path, out_path, x_column_name, y_column_name,
|
||
title, title_font_size, y_scale_font_angle, x_name, y_name, color):
|
||
df_raw = pd.read_csv(file_path)
|
||
df = df_raw[[x_column_name, y_column_name]].groupby(y_column_name).apply(lambda x: x.mean())
|
||
df.sort_values(x_column_name, inplace=True)
|
||
df.reset_index(inplace=True)
|
||
|
||
import matplotlib.patches as patches
|
||
|
||
plt.gca().set(ylabel=y_name, xlabel=x_name)
|
||
|
||
fig, ax = plt.subplots(figsize=(9.4, 4.25), facecolor='white', dpi=80)
|
||
ax.vlines(x=df.index,
|
||
ymin=0,
|
||
ymax=df.cty,
|
||
color=color,
|
||
alpha=0.7,
|
||
linewidth=20)
|
||
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
# Annotate Text
|
||
for i, cty in enumerate(df.cty):
|
||
ax.text(i, cty + 0.5, round(cty, 1), horizontalalignment='center')
|
||
|
||
# Title, Label, Ticks and Ylim
|
||
|
||
plt.xticks(df.index,
|
||
df.manufacturer.str.upper(),
|
||
rotation=60,
|
||
horizontalalignment='right',
|
||
fontsize=10)
|
||
plt.yticks(fontsize=12)
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
plt.ylim = (0, 30)
|
||
|
||
# 添加底纹
|
||
p1 = patches.Rectangle((.57, -0.005),
|
||
width=.33,
|
||
height=.13,
|
||
alpha=.1,
|
||
facecolor='green',
|
||
transform=fig.transFigure)
|
||
p2 = patches.Rectangle((.124, -0.005),
|
||
width=.446,
|
||
height=.13,
|
||
alpha=.1,
|
||
facecolor='red',
|
||
transform=fig.transFigure)
|
||
fig.add_artist(p1)
|
||
fig.add_artist(p2)
|
||
|
||
plt.title(title, fontsize=int(title_font_size))
|
||
plt.savefig(f"{chart_out_path}") # 保存图片 输出一个展示的png
|
||
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
def Lollipop_Chart(file_path, chart_out_path, out_path, x_column_name, y_column_name,
|
||
title, title_font_size, x_scale_font_angle, y_scale_font_angle, x_name, y_name, color):
|
||
df_raw = pd.read_csv(file_path)
|
||
df = df_raw[[x_column_name,
|
||
y_column_name]].groupby(y_column_name).apply(lambda x: x.mean())
|
||
df.sort_values(x_column_name, inplace=True)
|
||
df.reset_index(inplace=True)
|
||
|
||
# Draw plot
|
||
fig, ax = plt.subplots(figsize=(9.4, 4.25), dpi=200)
|
||
|
||
ax.vlines(x=df.index,
|
||
ymin=0,
|
||
ymax=df.cty,
|
||
color=color,
|
||
alpha=0.7,
|
||
linewidth=4)
|
||
|
||
ax.scatter(x=df.index, y=df[x_column_name], s=85, color=color, alpha=0.7)
|
||
|
||
# Title, Label, Ticks and Ylim
|
||
|
||
ax.set_xticks(df.index)
|
||
ax.set_xticklabels(df.manufacturer.str.upper(),
|
||
rotation=60,
|
||
fontdict={
|
||
'horizontalalignment': 'right',
|
||
'size': 11
|
||
})
|
||
ax.set_ylim(0, 30)
|
||
plt.yticks(fontsize=12)
|
||
|
||
# Annotate
|
||
for row in df.itertuples():
|
||
ax.text(row.Index,
|
||
row.cty + .5,
|
||
s=round(row.cty, 2),
|
||
horizontalalignment='center',
|
||
verticalalignment='bottom',
|
||
fontsize=12)
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
plt.title(title, fontsize=int(title_font_size))
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
def Dot_Plot(file_path, chart_out_path, out_path, x_column_name, y_column_name,
|
||
title, title_font_size, x_scale_font_angle, y_scale_font_angle, x_name, y_name, line_color, point_color):
|
||
df_raw = pd.read_csv(file_path)
|
||
df = df_raw[[x_column_name,
|
||
y_column_name]].groupby(y_column_name).apply(lambda x: x.mean())
|
||
df.sort_values('cty', inplace=True)
|
||
df.reset_index(inplace=True)
|
||
|
||
# Draw plot
|
||
fig, ax = plt.subplots(figsize=(10, 6), dpi=80)
|
||
ax.hlines(y=df.index,
|
||
xmin=11,
|
||
xmax=26,
|
||
color=line_color,
|
||
alpha=0.7,
|
||
linewidth=1,
|
||
linestyles='dashdot')
|
||
ax.scatter(y=df.index, x=df.cty, s=75, color=point_color, alpha=0.7)
|
||
|
||
ax.set_yticks(df.index)
|
||
ax.set_yticklabels(df.manufacturer.str.title(),
|
||
fontdict={
|
||
'horizontalalignment': 'right',
|
||
'fontsize': 12,
|
||
})
|
||
|
||
ax.set_xlim(10, 27)
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.xlabel(x_name, fontsize=12)
|
||
|
||
plt.title(title, fontsize=int(title_font_size))
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
def Slope_Chart(file_path, chart_out_path, out_path, column_name_1, column_name_2, column_name_3,
|
||
title, title_font_size, x_scale_font_angle, y_scale_font_angle, x_name, y_name):
|
||
import matplotlib.lines as mlines
|
||
df = pd.read_csv(file_path)
|
||
|
||
def newline(p1, p2):
|
||
ax = plt.gca()
|
||
l = mlines.Line2D([p1[0], p2[0]], [p1[1], p2[1]],
|
||
color='red' if p1[1] - p2[1] > 0 else 'green',
|
||
marker='o',
|
||
markersize=6)
|
||
ax.add_line(l)
|
||
return l
|
||
|
||
fig, ax = plt.subplots(1, 1, figsize=(9.4, 4.25), dpi=80)
|
||
|
||
# Vertical Lines
|
||
ax.vlines(x=1,
|
||
ymin=500,
|
||
ymax=13000,
|
||
color='black',
|
||
alpha=0.7,
|
||
linewidth=1,
|
||
linestyles='dotted')
|
||
ax.vlines(x=3,
|
||
ymin=500,
|
||
ymax=13000,
|
||
color='black',
|
||
alpha=0.7,
|
||
linewidth=1,
|
||
linestyles='dotted')
|
||
|
||
ax.scatter(y=df[column_name_1],
|
||
x=np.repeat(1, df.shape[0]),
|
||
s=10,
|
||
color='black',
|
||
alpha=0.7)
|
||
ax.scatter(y=df[column_name_2],
|
||
x=np.repeat(3, df.shape[0]),
|
||
s=10,
|
||
color='black',
|
||
alpha=0.7)
|
||
|
||
# Line Segmentsand Annotation
|
||
for p1, p2, c in zip(df[column_name_1], df[column_name_2], df[column_name_3]):
|
||
newline([1, p1], [3, p2])
|
||
ax.text(1 - 0.05,
|
||
p1,
|
||
c + ', ' + str(round(p1)),
|
||
horizontalalignment='right',
|
||
verticalalignment='center',
|
||
fontdict={'size': 14})
|
||
ax.text(3 + 0.05,
|
||
p2,
|
||
c + ', ' + str(round(p2)),
|
||
horizontalalignment='left',
|
||
verticalalignment='center',
|
||
fontdict={'size': 14})
|
||
|
||
# 'Before' and 'After' Annotations
|
||
ax.text(1 - 0.05,
|
||
13000,
|
||
'BEFORE',
|
||
horizontalalignment='right',
|
||
verticalalignment='center',
|
||
fontdict={
|
||
'size': 15,
|
||
'weight': 700
|
||
})
|
||
ax.text(3 + 0.05,
|
||
13000,
|
||
'AFTER',
|
||
horizontalalignment='left',
|
||
verticalalignment='center',
|
||
fontdict={
|
||
'size': 15,
|
||
'weight': 700
|
||
})
|
||
|
||
# Decoration
|
||
|
||
ax.set(xlim=(0, 4), ylim=(0, 14000))
|
||
|
||
ax.set_xticks([1, 3])
|
||
ax.set_xticklabels([column_name_1, column_name_2], fontdict={'size': 15, 'weight': 700})
|
||
plt.yticks(np.arange(500, 13000, 2000), rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
# Lighten borders
|
||
plt.gca().spines["top"].set_alpha(.0)
|
||
plt.gca().spines["bottom"].set_alpha(.0)
|
||
plt.gca().spines["right"].set_alpha(.0)
|
||
plt.gca().spines["left"].set_alpha(.0)
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
plt.title(title, fontsize=int(title_font_size))
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
def Dumbbell_Plot(file_path, chart_out_path, out_path, column_name_1, column_name_2,
|
||
title, title_font_size, x_scale_font_angle, y_scale_font_angle, x_name, y_name):
|
||
import matplotlib.lines as mlines
|
||
|
||
# Import Data
|
||
df = pd.read_csv(file_path)
|
||
df.sort_values(column_name_1, inplace=True)
|
||
df.reset_index(inplace=True)
|
||
|
||
# Func to draw line segment
|
||
def newline(p1, p2):
|
||
ax = plt.gca()
|
||
l = mlines.Line2D([p1[0], p2[0]], [p1[1], p2[1]], color='#d5695d')
|
||
ax.add_line(l)
|
||
return l
|
||
|
||
# Figure and Axes
|
||
fig, ax = plt.subplots(1, 1, figsize=(9.4, 4.25), facecolor='#f8f2e4', dpi=80)
|
||
|
||
# Vertical Lines
|
||
ax.vlines(x=.05,
|
||
ymin=0,
|
||
ymax=26,
|
||
color='black',
|
||
alpha=1,
|
||
linewidth=1,
|
||
linestyles='dotted')
|
||
ax.vlines(x=.10,
|
||
ymin=0,
|
||
ymax=26,
|
||
color='black',
|
||
alpha=1,
|
||
linewidth=1,
|
||
linestyles='dotted')
|
||
ax.vlines(x=.15,
|
||
ymin=0,
|
||
ymax=26,
|
||
color='black',
|
||
alpha=1,
|
||
linewidth=1,
|
||
linestyles='dotted')
|
||
ax.vlines(x=.20,
|
||
ymin=0,
|
||
ymax=26,
|
||
color='black',
|
||
alpha=1,
|
||
linewidth=1,
|
||
linestyles='dotted')
|
||
|
||
# Points
|
||
ax.scatter(y=df['index'], x=df[column_name_1], s=50, color='#dc2624')
|
||
ax.scatter(y=df['index'], x=df[column_name_2], s=50, color='#e87a59')
|
||
|
||
# Line Segments
|
||
for i, p1, p2 in zip(df['index'], df[column_name_1], df[column_name_2]):
|
||
newline([p1, i], [p2, i])
|
||
|
||
# Decoration
|
||
ax.set_facecolor('#f8f2e4')
|
||
|
||
ax.set(xlim=(0, .25), ylim=(-1, 27))
|
||
plt.yticks(fontsize=15)
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
plt.title(title, fontsize=int(title_font_size))
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
def Stacked_Histogram_for_Continuous_Variable(file_path, chart_out_path, out_path, x_column_name, group_by_column,
|
||
title, title_font_size, x_scale_font_angle, y_scale_font_angle, x_name,
|
||
y_name):
|
||
df = pd.read_csv(file_path)
|
||
|
||
# Prepare data
|
||
x_var = x_column_name
|
||
groupby_var = group_by_column
|
||
df_agg = df.loc[:, [x_var, groupby_var]].groupby(groupby_var)
|
||
vals = [df[x_var].values.tolist() for i, df in df_agg]
|
||
|
||
# Draw
|
||
plt.figure(figsize=(9.4, 4.25), dpi=180)
|
||
colors = [plt.cm.Set1(i / float(len(vals) - 1)) for i in range(len(vals))]
|
||
n, bins, patches = plt.hist(vals,
|
||
30,
|
||
stacked=True,
|
||
density=False,
|
||
color=colors[:len(vals)])
|
||
|
||
# Decoration
|
||
plt.legend({
|
||
group: col
|
||
for group, col in zip(
|
||
np.unique(df[groupby_var]).tolist(), colors[:len(vals)])
|
||
})
|
||
|
||
plt.xlabel(x_var)
|
||
|
||
# plt.ylim(0, 25)
|
||
plt.xticks(ticks=bins[::3], labels=[round(b, 1) for b in bins[::3]])
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
plt.title(title, fontsize=int(title_font_size))
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 23、密度图(Density_Plot)
|
||
# 测试文件 mpg.csv
|
||
# 基础参数 图片的长/宽/分辨率/字号/x轴字号/标题的字号、xy轴限制/颜色
|
||
|
||
def Density_Plot(file_path, chart_out_path, x_scale_font_angle, y_scale_font_angle, y_name, x_name, y_font_size,
|
||
x_font_size, out_path,
|
||
scale_font_size, title, title_font_size, column_name):
|
||
df = pd.read_csv(file_path)
|
||
|
||
# Draw Plot
|
||
plt.figure(figsize=(9.4, 4.25), dpi=180)
|
||
sns.kdeplot(df.loc[df[column_name] == 4, column_name],
|
||
shade=True,
|
||
color="#01a2d9",
|
||
label=f"{column_name}=4",
|
||
alpha=.7)
|
||
sns.kdeplot(df.loc[df[column_name] == 5, column_name],
|
||
shade=True,
|
||
color="#dc2624",
|
||
label=f"{column_name}=5",
|
||
alpha=.7)
|
||
sns.kdeplot(df.loc[df[column_name] == 6, column_name],
|
||
shade=True,
|
||
color="#C89F91",
|
||
label=f"{column_name}=6",
|
||
alpha=.7)
|
||
sns.kdeplot(df.loc[df[column_name] == 8, column_name],
|
||
shade=True,
|
||
color="#649E7D",
|
||
label=f"{column_name}=8",
|
||
alpha=.7)
|
||
|
||
# Decoration
|
||
sns.set(style="whitegrid", font_scale=scale_font_size)
|
||
plt.title(title, fontsize=title_font_size)
|
||
plt.legend()
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=y_font_size)
|
||
plt.ylabel(x_name, fontsize=x_font_size)
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 25、山峰叠峦图(Joy_Plot)
|
||
# 测试文件 mpg.csv
|
||
# 基础参数 图片的长/宽/分辨率/字号/x轴字号/标题的字号、xy轴限制/颜色
|
||
|
||
def Joy_Plot(file_path, chart_out_path, out_path, title, title_font_size, x_scale_font_angle, y_scale_font_angle,
|
||
y_name, x_name, column_name_1, column_name_2,
|
||
group_by_column, y_scale_limit):
|
||
mpg = pd.read_csv(file_path)
|
||
|
||
# Draw Plot
|
||
plt.figure(figsize=(9.4, 4.25), dpi=180)
|
||
fig, axes = joypy.joyplot(mpg,
|
||
column=[column_name_1, column_name_2],
|
||
by=group_by_column,
|
||
ylim=y_scale_limit,
|
||
colormap=plt.cm.Set1,
|
||
figsize=(10, 6))
|
||
|
||
# Decoration
|
||
plt.title(title, fontsize=title_font_size)
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 27、箱图(boxplot)
|
||
|
||
# 测试文件 mpg.csv
|
||
# 基础参数 图片的长/宽/分辨率/字号/x轴字号/标题的字号、xy轴限制/颜色
|
||
|
||
def boxplot(file_path, chart_out_path, out_path,
|
||
scale_font_size, title, title_font_size, font_size, font_color, y_scale_min, y_scale_max, x_column_name,
|
||
y_column_name, x_scale_font_angle, y_scale_font_angle, y_name, x_name, color):
|
||
df = pd.read_csv(file_path)
|
||
# Draw Plot
|
||
plt.figure(figsize=(9.4, 4.25), dpi=180)
|
||
sns.boxplot(
|
||
x=x_column_name,
|
||
y=y_column_name,
|
||
data=df,
|
||
notch=False,
|
||
palette=color,
|
||
)
|
||
|
||
# Add N Obs inside boxplot (optional)
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
def add_n_obs(df, group_col, y):
|
||
medians_dict = {
|
||
grp[0]: grp[1][y].median()
|
||
for grp in df.groupby(group_col)
|
||
}
|
||
xticklabels = [x.get_text() for x in plt.gca().get_xticklabels()]
|
||
n_obs = df.groupby(group_col)[y].size().values
|
||
for (x, xticklabel), n_ob in zip(enumerate(xticklabels), n_obs):
|
||
plt.text(x,
|
||
medians_dict[xticklabel] * 1.01,
|
||
"#obs : " + str(n_ob),
|
||
horizontalalignment='center',
|
||
fontdict={'size': font_size},
|
||
color=font_color)
|
||
|
||
add_n_obs(df, group_col=x_column_name, y=y_column_name)
|
||
|
||
# Decoration
|
||
sns.set(style="whitegrid", font_scale=scale_font_size)
|
||
plt.title(title, fontsize=title_font_size)
|
||
plt.ylim(y_scale_min, y_scale_max)
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 28、箱图结合点图(Dot_Box_Plot)
|
||
# 测试文件 mpg.csv
|
||
|
||
|
||
def Dot_Box_Plot(file_path, chart_out_path, out_path,
|
||
title, title_font_size, point_size, line_color, x_scale_font_angle, y_scale_font_angle, y_name, x_name,
|
||
x_column_name, y_column_name, group_by_column, legend_name, color):
|
||
df = pd.read_csv(file_path)
|
||
plt.figure(figsize=(9.4, 4.25), dpi=180)
|
||
sns.boxplot(
|
||
x=x_column_name,
|
||
y=y_column_name,
|
||
data=df,
|
||
hue=legend_name,
|
||
palette=color,
|
||
)
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
plt.legend(loc=9)
|
||
sns.stripplot(x=x_column_name,
|
||
y=y_column_name,
|
||
data=df,
|
||
color='#dc2624',
|
||
size=point_size,
|
||
jitter=1)
|
||
|
||
for i in range(len(df[group_by_column].unique()) - 1):
|
||
plt.vlines(i + .5, 10, 45, linestyles='solid', colors=line_color, alpha=0.2)
|
||
|
||
# Decoration
|
||
plt.title(title, fontsize=title_font_size)
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 28、箱图结合点图(Dot_Box_Plot)
|
||
# 测试文件 mpg.csv
|
||
|
||
|
||
def Violin_Plot(file_path, chart_out_path, out_path,
|
||
title, title_font_size, x_column_name, y_column_name, x_scale_font_angle, y_scale_font_angle, y_name,
|
||
x_name, color):
|
||
df = pd.read_csv(file_path)
|
||
|
||
# Draw Plot
|
||
plt.figure(figsize=(9.4, 4.25), dpi=180)
|
||
sns.violinplot(x=x_column_name,
|
||
y=y_column_name,
|
||
data=df,
|
||
scale='width',
|
||
palette=color,
|
||
inner='quartile')
|
||
|
||
# Decoration
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
plt.title(title, fontsize=title_font_size)
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 30、金字塔图(Population_Pyramid)
|
||
|
||
# 测试文件 email_campaign_funnel.csv
|
||
|
||
|
||
def Population_Pyramid(file_path, chart_out_path, out_path,
|
||
title, title_font_size, x_name, y_name, group_by_column, x_scale_font_angle, y_scale_font_angle,
|
||
y_column_name, x_column_name):
|
||
df = pd.read_csv(file_path)
|
||
|
||
# Draw Plot
|
||
plt.figure(figsize=(9.4, 4.25), dpi=180)
|
||
group_col = group_by_column
|
||
order_of_bars = df.Stage.unique()[::-1]
|
||
colors = [
|
||
plt.cm.Set1(i / float(len(df[group_col].unique()) - 1))
|
||
for i in range(len(df[group_col].unique()))
|
||
]
|
||
|
||
for c, group in zip(colors, df[group_col].unique()):
|
||
sns.barplot(x=x_column_name,
|
||
y=y_column_name,
|
||
data=df.loc[df[group_col] == group, :],
|
||
order=order_of_bars,
|
||
color=c,
|
||
label=group)
|
||
|
||
# Decorations
|
||
plt.title(title, fontsize=title_font_size)
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
plt.legend()
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 32、华夫饼图(Waffle_Chart)
|
||
|
||
# 测试文件 mpg.csv
|
||
|
||
|
||
def Waffle_Chart(file_path, chart_out_path, out_path, rows_number, group_by_column, x_scale_font_angle,
|
||
y_scale_font_angle, y_name, x_name,
|
||
title_font_size, title):
|
||
df_raw = pd.read_csv(file_path)
|
||
|
||
from pywaffle import Waffle
|
||
|
||
# Prepare Data
|
||
df = df_raw.groupby(group_by_column).size().reset_index(name='counts')
|
||
n_categories = df.shape[0]
|
||
colors = [plt.cm.Set1(i / float(n_categories))
|
||
for i in range(n_categories)]
|
||
|
||
# Draw Plot and Decorate
|
||
plt.figure(FigureClass=Waffle,
|
||
plots={
|
||
'111': {
|
||
'values':
|
||
df['counts'],
|
||
'labels': [
|
||
"{0} ({1})".format(n[0], n[1])
|
||
for n in df[[group_by_column, 'counts']].itertuples()
|
||
],
|
||
'legend': {
|
||
'loc': 'upper left',
|
||
'bbox_to_anchor': (1.05, 1),
|
||
'fontsize': 12
|
||
},
|
||
},
|
||
},
|
||
rows=rows_number,
|
||
colors=colors,
|
||
dpi=180,
|
||
figsize=(9.4, 4.25))
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
plt.title(title, fontsize=title_font_size)
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 33、饼图(Pie_Chart)
|
||
# 测试文件 mpg.csv
|
||
|
||
|
||
def Pie_Chart(file_path, chart_out_path, out_path, y_name, title, group_by_column, title_font_size, y_scale_font_angle):
|
||
df_raw = pd.read_csv(file_path)
|
||
|
||
plt.figure(figsize=(9.4, 4.25), dpi=180)
|
||
# Prepare Data
|
||
df = df_raw.groupby(group_by_column).size()
|
||
|
||
# Make the plot with pandas
|
||
df.plot(kind='pie', subplots=True)
|
||
plt.title(title, fontsize=title_font_size)
|
||
|
||
plt.ylabel(y_name)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 34、树状图(Treemap)
|
||
# 测试文件 mpg.csv
|
||
|
||
|
||
def Treemap(file_path, chart_out_path, out_path, title, group_by_column, title_font_size):
|
||
import squarify
|
||
|
||
df_raw = pd.read_csv(file_path)
|
||
|
||
# Prepare Data
|
||
df = df_raw.groupby(group_by_column).size().reset_index(name='counts')
|
||
labels = df.apply(lambda x: str(x[0]) + "\n (" + str(x[1]) + ")", axis=1)
|
||
sizes = df['counts'].values.tolist()
|
||
colors = [plt.cm.Set2(i / float(len(labels))) for i in range(len(labels))]
|
||
|
||
# Draw Plot
|
||
plt.figure(figsize=(9.4, 4.25), dpi=180)
|
||
squarify.plot(sizes=sizes, label=labels, color=colors, alpha=.8)
|
||
|
||
# Decorate
|
||
plt.title(title, fontsize=title_font_size)
|
||
plt.axis('off')
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 35、柱状图(Bar_Chart
|
||
# 测试文件 mpg.csv
|
||
|
||
|
||
def Bar_Chart(file_path, chart_out_path, out_path, title, title_font_size, font_size, y_scale_min, y_scale_max, width,
|
||
group_by_column, x_scale_font_angle,
|
||
y_scale_font_angle, y_name, x_name):
|
||
import random
|
||
df_raw = pd.read_csv(file_path)
|
||
|
||
df = df_raw.groupby(group_by_column).size().reset_index(name='counts')
|
||
n = df[group_by_column].unique().__len__() + 1
|
||
all_colors = list(plt.cm.colors.cnames.keys())
|
||
random.seed(100)
|
||
c = random.choices(all_colors, k=n)
|
||
|
||
# Plot Bars
|
||
plt.figure(figsize=(9.4, 4.25), dpi=180)
|
||
plt.bar(df[group_by_column], df['counts'], color=c, width=width)
|
||
for i, val in enumerate(df['counts'].values):
|
||
plt.text(i,
|
||
val,
|
||
float(val),
|
||
horizontalalignment='center',
|
||
verticalalignment='bottom',
|
||
fontdict={
|
||
'fontweight': 500,
|
||
'size': font_size
|
||
})
|
||
|
||
# Decoration
|
||
plt.gca().set_xticklabels(df[group_by_column],
|
||
rotation=60,
|
||
horizontalalignment='right')
|
||
plt.title(title, fontsize=title_font_size)
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
plt.ylim(y_scale_min, y_scale_max)
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 36、时间序列图(Time_Series_Plot)
|
||
# 测试文件 AirPassengers.csv
|
||
|
||
|
||
def Time_Series_Plot(file_path, chart_out_path, out_path,
|
||
polt_color, title, title_font_size, x_column, y_column, x_font_size, y_font_size, y_scale_min,
|
||
y_scale_max, x_scale_font_angle,
|
||
y_scale_font_angle, y_name, x_name):
|
||
df = pd.read_csv(file_path)
|
||
|
||
# Draw Plot
|
||
plt.figure(figsize=(9.4, 4.25), dpi=180)
|
||
plt.plot(df[x_column], df[y_column], color=polt_color)
|
||
# plt.plot(df['date'], df['value'], color='#365770')
|
||
# print(file_path)
|
||
# Decoration
|
||
plt.ylim(y_scale_min, y_scale_max)
|
||
xtick_location = df.index.tolist()[::12]
|
||
xtick_labels = [x[-4:] for x in df.date.tolist()[::12]]
|
||
plt.xticks(ticks=xtick_location,
|
||
labels=xtick_labels,
|
||
rotation=0,
|
||
fontsize=x_font_size,
|
||
horizontalalignment='center',
|
||
alpha=.7)
|
||
plt.yticks(fontsize=y_font_size, alpha=.7)
|
||
plt.title(title, fontsize=title_font_size)
|
||
plt.grid(axis='both', alpha=.3)
|
||
plt.show()
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
# Remove borders
|
||
plt.gca().spines["top"].set_alpha(0.0)
|
||
plt.gca().spines["bottom"].set_alpha(0.3)
|
||
plt.gca().spines["right"].set_alpha(0.0)
|
||
plt.gca().spines["left"].set_alpha(0.3)
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 41、多重时间序列图(Multiple_Time_Series)!!!
|
||
# 测试文件 mortality.csv
|
||
|
||
def Multiple_Time_Series(file_path, chart_out_path, out_path,
|
||
font_size, title, title_font_size, x_font_size, y_font_size, x_scale_min, x_scale_max,
|
||
x_scale_font_angle, y_scale_font_angle, y_name,
|
||
x_name):
|
||
df = pd.read_csv(file_path)
|
||
|
||
y_LL = 100
|
||
y_UL = int(df.iloc[:, 1:].max().max() * 1.1)
|
||
y_interval = 400
|
||
mycolors = ['tab:red', 'tab:blue', 'tab:green', 'tab:orange']
|
||
|
||
fig, ax = plt.subplots(1, 1, figsize=(9.4, 4.25), dpi=180)
|
||
|
||
columns = df.columns[1:]
|
||
for i, column in enumerate(columns):
|
||
plt.plot(df.date.values, df[column].values, lw=1.5, color=mycolors[i])
|
||
plt.text(df.shape[0] + 1,
|
||
df[column].values[-1],
|
||
column,
|
||
fontsize=font_size,
|
||
color=mycolors[i])
|
||
|
||
for y in range(y_LL, y_UL, y_interval):
|
||
plt.hlines(y,
|
||
xmin=0,
|
||
xmax=71,
|
||
colors='black',
|
||
alpha=0.3,
|
||
linestyles="--",
|
||
lw=0.5)
|
||
|
||
# Decorations
|
||
plt.tick_params(axis="both",
|
||
which="both",
|
||
bottom=False,
|
||
top=False,
|
||
labelbottom=True,
|
||
left=False,
|
||
right=False,
|
||
labelleft=True)
|
||
|
||
# Lighten borders
|
||
plt.gca().spines["top"].set_alpha(.3)
|
||
plt.gca().spines["bottom"].set_alpha(.3)
|
||
plt.gca().spines["right"].set_alpha(.3)
|
||
plt.gca().spines["left"].set_alpha(.3)
|
||
|
||
plt.title(title,
|
||
fontsize=title_font_size)
|
||
plt.yticks(range(y_LL, y_UL, y_interval),
|
||
[str(y) for y in range(y_LL, y_UL, y_interval)],
|
||
fontsize=y_font_size)
|
||
plt.xticks(range(0, df.shape[0], 12),
|
||
df.date.values[::12],
|
||
horizontalalignment='left',
|
||
rotation=45,
|
||
fontsize=x_font_size)
|
||
plt.ylim(y_LL, y_UL)
|
||
plt.xlim(x_scale_min, x_scale_max)
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 42、双坐标系时间序列图(Plotting_with_different_scales_using_secondary_Y_axis)
|
||
# 测试文件 economics.csv
|
||
|
||
def Plotting_with_different_scales_using_secondary_Y_axis(file_path, chart_out_path, out_path,
|
||
x_name, x_font_size, y_name_1, y_font_size_1, y_font_color_1,
|
||
y_name_2, y_font_size_2,
|
||
y_font_color_2, title, title_font_size, x_column, y_column_1,
|
||
y_column_2, x_scale_font_angle,
|
||
y_scale_font_angle):
|
||
df = pd.read_csv(file_path)
|
||
|
||
x = df[x_column]
|
||
y1 = df[y_column_1]
|
||
y2 = df[y_column_2]
|
||
|
||
# Plot Line1 (Left Y Axis)
|
||
fig, ax1 = plt.subplots(1, 1, figsize=(9.4, 4.25), dpi=180)
|
||
ax1.plot(x, y1, color='tab:red')
|
||
|
||
# Plot Line2 (Right Y Axis)
|
||
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
|
||
ax2.plot(x, y2, color='tab:blue')
|
||
|
||
# Decorations
|
||
# ax1 (left Y axis)
|
||
ax1.set_xlabel(x_name, fontsize=x_font_size)
|
||
ax1.tick_params(axis='x', rotation=70, labelsize=12)
|
||
ax1.set_ylabel(y_name_1, color=y_font_color_1, fontsize=y_font_size_1)
|
||
ax1.tick_params(axis='y', rotation=0, labelcolor='#dc2624')
|
||
ax1.grid(alpha=.4)
|
||
|
||
# ax2 (right Y axis)
|
||
ax2.set_ylabel(y_name_2, color=y_font_color_2, fontsize=y_font_size_2)
|
||
ax2.tick_params(axis='y', labelcolor='#01a2d9')
|
||
ax2.set_xticks(np.arange(0, len(x), 60))
|
||
ax2.set_xticklabels(x[::60], rotation=90, fontdict={'fontsize': 10})
|
||
ax2.set_title(
|
||
title,
|
||
fontsize=title_font_size)
|
||
fig.tight_layout()
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name_1, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 44、堆积面积图(Stacked_Area_Chart)
|
||
# 测试文件 nightvisitors.csv
|
||
|
||
def Stacked_Area_Chart(file_path, chart_out_path, out_path,
|
||
y_scale_min, y_scale_max, title, title_font_size, data_column, x_scale_font_angle,
|
||
y_scale_font_angle, y_name, x_name):
|
||
df = pd.read_csv(file_path)
|
||
|
||
# Decide Colors
|
||
mycolors = ['#dc2624', '#2b4750', '#45a0a2', '#e87a59',
|
||
'#7dcaa9', '#649E7D', '#dc8018', '#C89F91']
|
||
|
||
# Draw Plot and Annotate
|
||
|
||
fig, ax = plt.subplots(1, 1, figsize=(9.4, 4.25), dpi=180)
|
||
columns = df.columns[1:]
|
||
labs = columns.values.tolist()
|
||
|
||
# Prepare data
|
||
x = df[data_column].values.tolist()
|
||
y0 = df[columns[0]].values.tolist()
|
||
y1 = df[columns[1]].values.tolist()
|
||
y2 = df[columns[2]].values.tolist()
|
||
y3 = df[columns[3]].values.tolist()
|
||
y4 = df[columns[4]].values.tolist()
|
||
y5 = df[columns[5]].values.tolist()
|
||
y6 = df[columns[6]].values.tolist()
|
||
y7 = df[columns[7]].values.tolist()
|
||
y = np.vstack([y0, y2, y4, y6, y7, y5, y1, y3])
|
||
|
||
# Plot for each column
|
||
labs = columns.values.tolist()
|
||
ax = plt.gca()
|
||
ax.stackplot(x, y, labels=labs, colors=mycolors, alpha=0.8)
|
||
ax.tick_params(axis='x', rotation=45, labelsize=12)
|
||
# Decorations
|
||
ax.set_title(title, fontsize=title_font_size)
|
||
ax.set(ylim=[y_scale_min, y_scale_max])
|
||
ax.legend(fontsize=10, ncol=4)
|
||
plt.xticks(x[::5], fontsize=10, horizontalalignment='center')
|
||
plt.yticks(np.arange(10000, 100000, 20000))
|
||
plt.xlim(x[0], x[-1])
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
# Lighten borders
|
||
plt.gca().spines["top"].set_alpha(0)
|
||
plt.gca().spines["bottom"].set_alpha(.3)
|
||
plt.gca().spines["right"].set_alpha(0)
|
||
plt.gca().spines["left"].set_alpha(.3)
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 45、非堆积面积图(Area_Chart_UnStacked)
|
||
# 测试文件 economics.csv
|
||
|
||
def Area_Chart_UnStacked(file_path, chart_out_path, out_path,
|
||
y_scale_min, y_scale_max, title, title_font_size, x_column, y_column_1, y_column_2,
|
||
x_scale_font_angle, y_scale_font_angle, y_name, x_name):
|
||
df = pd.read_csv(file_path)
|
||
|
||
# Prepare Data
|
||
x = df[x_column].values.tolist()
|
||
y1 = df[y_column_1].values.tolist()
|
||
y2 = df[y_column_2].values.tolist()
|
||
columns = [y_column_1, y_column_2]
|
||
|
||
# Draw Plot
|
||
fig, ax = plt.subplots(1, 1, figsize=(9.4, 4.25), dpi=180)
|
||
ax.fill_between(x,
|
||
y1=y1,
|
||
y2=0,
|
||
label=columns[1],
|
||
alpha=0.5,
|
||
color='#dc2624',
|
||
linewidth=2)
|
||
ax.fill_between(x,
|
||
y1=y2,
|
||
y2=0,
|
||
label=columns[0],
|
||
alpha=0.5,
|
||
color='#649E7D',
|
||
linewidth=2)
|
||
|
||
# Decorations
|
||
ax.set_title(title,
|
||
fontsize=title_font_size)
|
||
ax.set(ylim=[y_scale_min, y_scale_max])
|
||
ax.legend(loc='best', fontsize=12)
|
||
plt.xticks(x[::50], fontsize=10, horizontalalignment='center')
|
||
plt.yticks(np.arange(2.5, 30.0, 2.5))
|
||
plt.xlim(-10, x[-1])
|
||
plt.tick_params(axis='x', rotation=45, labelsize=12)
|
||
|
||
# Draw Tick lines
|
||
for y in np.arange(2.5, 30.0, 2.5):
|
||
plt.hlines(y,
|
||
xmin=0,
|
||
xmax=len(x),
|
||
colors='black',
|
||
alpha=0.3,
|
||
linestyles="--",
|
||
lw=0.5)
|
||
|
||
# Lighten borders
|
||
plt.gca().spines["top"].set_alpha(0)
|
||
plt.gca().spines["bottom"].set_alpha(.3)
|
||
plt.gca().spines["right"].set_alpha(0)
|
||
plt.gca().spines["left"].set_alpha(.3)
|
||
|
||
plt.xticks(rotation=x_scale_font_angle, fontsize=10)
|
||
plt.yticks(rotation=y_scale_font_angle, fontsize=10)
|
||
|
||
plt.ylabel(y_name, fontsize=12)
|
||
plt.ylabel(x_name, fontsize=12)
|
||
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 48、聚类树形图(Dendrogram) !!!
|
||
# 测试文件 USArrests.csv
|
||
#
|
||
|
||
def Dendrogram(file_path, chart_out_path, out_path,
|
||
x_font_size, y_font_size, title, title_font_size):
|
||
import scipy.cluster.hierarchy as shc
|
||
|
||
df = pd.read_csv(file_path)
|
||
|
||
# Plot
|
||
plt.figure(figsize=(9.4, 4.25), dpi=180)
|
||
plt.title(title, fontsize=title_font_size)
|
||
dend = shc.dendrogram(shc.linkage(df[['Murder', 'Assault', 'UrbanPop',
|
||
'Rape']],
|
||
method='ward'),
|
||
labels=df.State.values,
|
||
color_threshold=200)
|
||
plt.xticks(fontsize=x_font_size)
|
||
plt.yticks(fontsize=y_font_size)
|
||
plt.savefig(f"{chart_out_path}") # 保存图片
|
||
plt.savefig(f"{out_path}.png")
|
||
plt.savefig(f"{out_path}.svg")
|
||
plt.savefig(f"{out_path}.pdf")
|
||
|
||
|
||
# 冯洋
|
||
# 基础参数 file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size, x_font_size, y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle, y_scale_font_angle
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 Composition_Bar.csv
|
||
|
||
def Composition_Bar(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size,
|
||
y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle, y_scale_font_angle,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color):
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
freqtable <- table(datas[,1])
|
||
df <- as.data.frame.table(freqtable)
|
||
library(ggplot2)
|
||
theme_set(theme_classic())
|
||
|
||
x <- df[,1]
|
||
|
||
y <- df[,2]
|
||
|
||
# Plot
|
||
g <- ggplot(df, aes(x, y))
|
||
gg <- g + geom_bar(stat="identity", width = 0.5, fill="tomato2") +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
def Composition_BarOfColorsDataByGroup(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name,
|
||
title_font_size, x_font_size, y_font_size, x_scale_font_size, y_scale_font_size,
|
||
x_scale_font_angle, y_scale_font_angle,
|
||
legend,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color,
|
||
title_font_color, legend_name,
|
||
legend_position):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
library(ggplot2)
|
||
theme_set(theme_classic())
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
# Plot
|
||
g <- ggplot(datas, aes(x))
|
||
gg <- g + geom_bar(aes(fill=y), width = 0.5) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
fill="{legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"),
|
||
legend.position="{legend_position}")
|
||
|
||
gg <- if(!{legend}) gg+guides(fill="none") else gg
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name, legend_position
|
||
# 实例文件 Composition_Pie.csv
|
||
|
||
def Composition_Pie(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size,
|
||
y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle, y_scale_font_angle, legend,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name,
|
||
legend_position):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
library(ggplot2)
|
||
theme_set(theme_classic())
|
||
|
||
# Source: Frequency table
|
||
df <- as.data.frame(table(datas))
|
||
colnames(df) <- c("class", "freq")
|
||
pie <- ggplot(df, aes(x = "", y=freq, fill = factor(class))) +
|
||
geom_bar(width = 1, stat = "identity") +
|
||
theme(axis.line = element_blank(),
|
||
plot.title = element_text(hjust=0.5)) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
fill="{legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"),
|
||
legend.position="{legend_position}")
|
||
|
||
gg <- pie + coord_polar(theta = "y", start=0)
|
||
|
||
gg <- if(!{legend}) gg+guides(fill="none") else gg
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name, legend_position
|
||
# 实例文件 Composition_Pie.csv
|
||
|
||
def Composition_Waffles(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size, y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle,
|
||
y_scale_font_angle, legend,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color,
|
||
legend_name, legend_position, color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
library("ggplot2")
|
||
|
||
var <- datas[,1] # the categorical data
|
||
|
||
nrows <- 10
|
||
df <- expand.grid(y = 1:nrows, x = 1:nrows)
|
||
categ_table <- round(table(var) * ((nrows*nrows)/(length(var))))
|
||
|
||
df$category <- factor(rep(names(categ_table), categ_table))
|
||
|
||
## Plot
|
||
gg <- ggplot(df, aes(x = x, y = y, fill = category)) +
|
||
geom_tile(color = "black", size = 0.5) +
|
||
scale_x_continuous(expand = c(0, 0)) +
|
||
scale_y_continuous(expand = c(0, 0), trans = 'reverse') +
|
||
scale_fill_brewer(palette = "{color}") +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
fill="{legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"),
|
||
legend.position="{legend_position}")
|
||
|
||
gg <- if(!{legend}) gg+guides(fill="none") else gg
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, model2, model3, model4, model5
|
||
# 实例文件 Correlation_Box.csv
|
||
|
||
def Correlation_Box(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size,
|
||
y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle, y_scale_font_angle, model1,
|
||
legend,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, model2,
|
||
model3, model4,
|
||
model5):
|
||
code_1 = """ggMarginal(gg, type = "histogram", fill="transparent")"""
|
||
code_2 = """ggMarginal(gg, type = "density", fill="transparent")"""
|
||
code_3 = """ggMarginal(gg, type = "boxplot", fill="transparent")"""
|
||
code_4 = """ggMarginal(gg, type = "violin", fill="transparent")"""
|
||
code_5 = """ggMarginal(gg, type = "densigram", fill="transparent")"""
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
# load package and data
|
||
library(ggplot2)
|
||
library(ggExtra)
|
||
|
||
# Scatterplot
|
||
theme_set(theme_bw()) # pre-set the bw theme.
|
||
|
||
gg <- ggplot(datas, aes(x, y)) +
|
||
geom_count(show.legend={legend}) +
|
||
geom_smooth(method="lm", se=F) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"))
|
||
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
if({model1}) {code_1}
|
||
if({model2}) {code_2}
|
||
if({model3}) {code_3}
|
||
if({model4}) {code_4}
|
||
if({model5}) {code_5}
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
if({model1}) {code_1}
|
||
if({model2}) {code_2}
|
||
if({model3}) {code_3}
|
||
if({model4}) {code_4}
|
||
if({model5}) {code_5}
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
if({model1}) {code_1}
|
||
if({model2}) {code_2}
|
||
if({model3}) {code_3}
|
||
if({model4}) {code_4}
|
||
if({model5}) {code_5}
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
if({model1}) {code_1}
|
||
if({model2}) {code_2}
|
||
if({model3}) {code_3}
|
||
if({model4}) {code_4}
|
||
if({model5}) {code_5}
|
||
dev.off()
|
||
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 Correlation_Bubble.csv
|
||
|
||
def Correlation_Bubble(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size, y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle,
|
||
y_scale_font_angle, size_legend_name, legend_name,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color):
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
z <- datas[,3]
|
||
|
||
w <- datas[,4]
|
||
|
||
library(ggplot2)
|
||
|
||
# Scatterplot
|
||
theme_set(theme_bw()) # pre-set the bw theme.
|
||
g <- ggplot(datas, aes(y, z)) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
col="{legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"))
|
||
|
||
gg <- g + geom_jitter(aes(col=x, size=w)) +
|
||
geom_smooth(aes(col=x), method="lm", se=F) +
|
||
labs(size="{size_legend_name}")
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 Correlation_Correlation.csv
|
||
|
||
def Correlation_Correlation(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name,
|
||
title_font_size,
|
||
x_font_size, y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle,
|
||
y_scale_font_angle,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color):
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
library(ggplot2)
|
||
library(ggcorrplot)
|
||
|
||
# Correlation matrix
|
||
corr <- round(cor(datas), 1)
|
||
|
||
# Plot
|
||
gg <- ggcorrplot(corr, hc.order = TRUE,
|
||
type = "lower",
|
||
lab = TRUE,
|
||
lab_size = 3,
|
||
method="circle",
|
||
colors = c("tomato2", "white", "springgreen3"),
|
||
title="{title}",
|
||
ggtheme=theme_bw) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 Correlation_Count.csv
|
||
|
||
def Correlation_Count(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size,
|
||
y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle, y_scale_font_angle, legend,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
# load package and data
|
||
library(ggplot2)
|
||
|
||
# Scatterplot
|
||
theme_set(theme_bw()) # pre-set the bw theme.
|
||
gg <- ggplot(datas, aes(x, y)) +
|
||
geom_count(col="tomato3", show.legend={legend}) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 Correlation_Jitter.csv
|
||
|
||
def Correlation_Jitter(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size, y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle,
|
||
y_scale_font_angle, legend, legend_name, legend_position,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
# load package and data
|
||
library(ggplot2)
|
||
|
||
# Scatterplot
|
||
theme_set(theme_bw()) # pre-set the bw theme.
|
||
g <- ggplot(datas, aes(x, y, fill=x))
|
||
gg <- g + geom_jitter(width = .5, size=1) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
fill="{legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"),
|
||
legend.position="{legend_position}")
|
||
|
||
gg <- if(!{legend}) gg+guides(fill="none") else gg
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 Correlation_Scatter.csv
|
||
|
||
def Correlation_Scatter(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size, y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle,
|
||
y_scale_font_angle, size_legend_name, legend_name,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
z <- datas[,3]
|
||
|
||
w <- datas[,4]
|
||
|
||
library(ggplot2)
|
||
|
||
options(scipen=999)
|
||
library(ggplot2)
|
||
theme_set(theme_bw())
|
||
|
||
# Scatterplot
|
||
gg <- ggplot(datas, aes(x=y, y=z)) +
|
||
geom_point(aes(col=x, size=w)) +
|
||
geom_smooth(method="loess", se=F) +
|
||
xlim(c(0, 0.1)) +
|
||
ylim(c(0, 500000)) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
col="{legend_name}",
|
||
size="{size_legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 Deviation_Area.csv
|
||
|
||
def Deviation_Area(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size,
|
||
y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle, y_scale_font_angle,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
library(ggplot2)
|
||
library(lubridate)
|
||
|
||
# Compute % Returns
|
||
datas$returns_perc <- c(0, diff(y)/y[-length(y)])
|
||
|
||
x <- as.Date(x)
|
||
|
||
# Create break points and labels for axis ticks
|
||
brks <- x[seq(1, length(x), 12)]
|
||
lbls <- lubridate::year(x[seq(1, length(x), 12)])
|
||
|
||
# Plot
|
||
gg <- ggplot(datas, aes(x, returns_perc)) +
|
||
geom_area() +
|
||
scale_x_date(breaks=brks, labels=lbls) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 Deviation_Bar.csv
|
||
|
||
def Deviation_Bar(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size,
|
||
y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle, y_scale_font_angle, type_1,
|
||
type_2,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}", row.name = 1)
|
||
|
||
y <- datas[,1]
|
||
|
||
library(ggplot2)
|
||
theme_set(theme_bw())
|
||
|
||
# Data Prep
|
||
x <- rownames(datas)
|
||
z <- round((y - mean(y))/sd(y), 2)
|
||
w <- ifelse(z < 0, "{type_2}", "{type_1}")
|
||
datas <- datas[order(z), ] # sort
|
||
x <- factor(x,levels = x)
|
||
|
||
#Diverging Barcharts
|
||
gg <- ggplot(datas, aes(x, z, label=z)) +
|
||
geom_bar(stat='identity', aes(fill=w), width=.5) +
|
||
scale_fill_manual(
|
||
labels = c("{type_1}", "{type_2}"),
|
||
values = c("{type_1}"="#00ba38", "{type_2}"="#f8766d")) +
|
||
coord_flip() +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 Deviation_Dots.csv
|
||
|
||
def Deviation_Dots(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size,
|
||
y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle, y_scale_font_angle, type_1,
|
||
type_2, x_font_color, y_font_color,
|
||
x_scale_font_color, y_scale_font_color, title_font_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
z <- datas[,3]
|
||
|
||
library(ggplot2)
|
||
theme_set(theme_bw())
|
||
|
||
# Plot
|
||
gg <- ggplot(datas, aes(x, y, label=y)) +
|
||
geom_point(stat='identity', aes(col=z), size=6) +
|
||
scale_color_manual(
|
||
labels = c("{type_1}", "{type_2}"),
|
||
values = c("{type_1}"="#00ba38", "{type_2}"="#f8766d")) +
|
||
geom_text(color="white", size=2) +
|
||
ylim(-2.5, 2.5) +
|
||
coord_flip() +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 Deviation_Lollipop.csv
|
||
|
||
def Deviation_Lollipop(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size, y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle,
|
||
y_scale_font_angle,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
library(ggplot2)
|
||
theme_set(theme_bw())
|
||
|
||
gg <- ggplot(datas, aes(x, y, label=y)) +
|
||
geom_point(stat='identity', fill="black", size=6) +
|
||
geom_segment(aes(y = 0,
|
||
x = x,
|
||
yend = y,
|
||
xend = x),
|
||
color = "black") +
|
||
geom_text(color="white", size=2) +
|
||
ylim(-2.5, 2.5) +
|
||
coord_flip() +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
axis.text.x=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}),
|
||
axis.title.x=element_text(size={y_font_size}, color="{y_font_color}"), axis.title.y=element_text(size={x_font_size}, color="{x_font_color}"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name, legend_position, line_color, point_color
|
||
# 实例文件 Distribution_BoxWithDots.csv
|
||
|
||
def Distribution_BoxWithDots(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name,
|
||
title_font_size,
|
||
x_font_size, y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle,
|
||
y_scale_font_angle, legend,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color,
|
||
legend_name,
|
||
legend_position, line_color, point_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
library(ggplot2)
|
||
theme_set(theme_bw())
|
||
|
||
# plot
|
||
g <- ggplot(datas, aes(x, y, fill=x))
|
||
gg <- g + geom_boxplot(color="{line_color}") +
|
||
geom_dotplot(binaxis='y',
|
||
stackdir='center',
|
||
dotsize = .5,
|
||
fill="{point_color}",
|
||
binwidth=0.5) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
fill="{legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"),
|
||
legend.position="{legend_position}")
|
||
|
||
gg <- if(!{legend}) gg+guides(fill="none") else gg
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, line_color
|
||
# 实例文件 Distribution_Box_1.csv
|
||
|
||
def Distribution_Box_1(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size, y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle,
|
||
y_scale_font_angle,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color,
|
||
line_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
library(ggplot2)
|
||
theme_set(theme_classic())
|
||
|
||
g <- ggplot(datas, aes(x, y))
|
||
gg <- g + geom_boxplot(varwidth=T, fill="plum", color="{line_color}") +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name, legend_position, line_color
|
||
# 实例文件 Distribution_Box_2.csv
|
||
|
||
def Distribution_Box_2(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size, y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle,
|
||
y_scale_font_angle, legend,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color,
|
||
legend_name, legend_position,
|
||
line_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
z <- datas[,3]
|
||
|
||
library(ggthemes)
|
||
library(ggplot2)
|
||
|
||
g <- ggplot(datas, aes(x, y))
|
||
gg <- g + geom_boxplot(aes(fill=factor(z)), color="{line_color}") +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
fill="{legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"),
|
||
legend.position="{legend_position}")
|
||
|
||
gg <- if(!{legend}) gg+guides(fill="none") else gg
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name, legend_position, line_color
|
||
# 实例文件 Distribution_DensityFunction.csv
|
||
|
||
def Distribution_DensityFunction(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name,
|
||
title_font_size, x_font_size, y_font_size, x_scale_font_size, y_scale_font_size,
|
||
x_scale_font_angle, y_scale_font_angle,
|
||
legend,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color,
|
||
legend_name,
|
||
legend_position, line_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
library(ggplot2)
|
||
theme_set(theme_classic())
|
||
|
||
g <- ggplot(datas, aes(x))
|
||
gg <- g + geom_density(aes(fill=factor(y)), alpha=0.8, color="{line_color}") +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
fill="{legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"),
|
||
legend.position="{legend_position}")
|
||
|
||
gg <- if(!{legend}) gg+guides(fill="none") else gg
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name, legend_position, model
|
||
# 实例文件 Distribution_Histogram.csv
|
||
|
||
def Distribution_Histogram(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name,
|
||
title_font_size,
|
||
x_font_size, y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle,
|
||
y_scale_font_angle, legend, model, x_font_color,
|
||
y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name,
|
||
legend_position, color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
library(ggplot2)
|
||
theme_set(theme_classic())
|
||
|
||
# Histogram on a Continuous (Numeric) Variable
|
||
g <- ggplot(mpg, aes(x)) +
|
||
|
||
scale_fill_brewer(palette = "{color}")
|
||
|
||
|
||
g1 <- g + geom_histogram(aes(fill=y),
|
||
binwidth = .1,
|
||
col="black",
|
||
size=.1) # change binwidth
|
||
|
||
g2 <- g + geom_histogram(aes(fill=y),
|
||
bins=5,
|
||
col="black",
|
||
size=.1) # change number of bins
|
||
|
||
gg <- if({model}) g2 else g1
|
||
|
||
gg <- gg + labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
fill="{legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"),
|
||
legend.position="{legend_position}")
|
||
|
||
gg <- if(!{legend}) gg+guides(fill="none") else gg
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name, legend_position
|
||
# 实例文件 Distribution_HistogramOfClassificationVariables.csv
|
||
|
||
def Distribution_HistogramOfClassificationVariables(file_path, chart_out_path, out_path, title, caption, subtitle,
|
||
x_name, y_name, title_font_size, x_font_size, y_font_size,
|
||
x_scale_font_size,
|
||
y_scale_font_size, x_scale_font_angle, y_scale_font_angle, legend,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color,
|
||
title_font_color,
|
||
legend_name, legend_position):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
library(ggplot2)
|
||
theme_set(theme_classic())
|
||
|
||
# Histogram on a Categorical variable
|
||
g <- ggplot(datas, aes(x))
|
||
gg <- g + geom_bar(aes(fill=y), width = 0.5) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
fill="{legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"),
|
||
legend.position="{legend_position}")
|
||
|
||
gg <- if(!{legend}) gg+guides(fill="none") else gg
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name, legend_position, color
|
||
# 实例文件 Distribution_PopulationPyramid.csv
|
||
|
||
def Distribution_PopulationPyramid(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name,
|
||
legend, legend_name, color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
legend <- datas[,3]
|
||
|
||
library(ggplot2)
|
||
library(ggthemes)
|
||
options(scipen = 999) # turns of scientific notations like 1e+40
|
||
|
||
# X Axis Breaks and Labels
|
||
brks <- seq(-15000000, 15000000, 5000000)
|
||
lbls = paste0(as.character(c(seq(15, 0, -5),
|
||
seq(5, 15, 5))), "m")
|
||
|
||
# Plot
|
||
g <- ggplot(datas, aes(x = x,
|
||
y = y, fill = legend)) # Fill column
|
||
gg <- g + geom_bar(stat = "identity", width = .6) + # draw the bars
|
||
scale_y_continuous(breaks = brks, # Breaks
|
||
labels = lbls) + # Labels
|
||
coord_flip() + # Flip axes
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
fill="{legend_name}")+
|
||
theme_tufte() + # Tufte theme from ggfortify
|
||
theme(plot.title = element_text(hjust = .5),
|
||
axis.ticks = element_blank()) + # Centre plot title
|
||
scale_fill_brewer(palette = "{color}") # Color palette
|
||
|
||
gg <- if(!{legend}) gg+guides(fill="none") else gg
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name, legend_position, color
|
||
# 实例文件 Distribution_TufteBox.csv
|
||
|
||
def Distribution_TufteBox(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name,
|
||
title_font_size,
|
||
x_font_size, y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle,
|
||
y_scale_font_angle, legend,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color,
|
||
legend_name, legend_position,
|
||
color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
library(ggthemes)
|
||
library(ggplot2)
|
||
|
||
theme_set(theme_tufte()) # from ggthemes
|
||
|
||
# plot
|
||
g <- ggplot(datas, aes(x, y, fill=x))
|
||
gg <- g + geom_tufteboxplot(color="{color}") +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
fill="{legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"),
|
||
legend.position="{legend_position}")
|
||
|
||
gg <- if(!{legend}) gg+guides(fill="none") else gg
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name, legend_position, line_color
|
||
# 实例文件 Distribution_Violin.csv
|
||
|
||
def Distribution_Violin(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size, y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle,
|
||
y_scale_font_angle, legend,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color,
|
||
legend_name, legend_position,
|
||
line_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
library(ggplot2)
|
||
theme_set(theme_bw())
|
||
|
||
g <- ggplot(datas, aes(x, y, fill=x))
|
||
gg <- g + geom_violin(color="{line_color}") +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
fill="{legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"),
|
||
legend.position="{legend_position}")
|
||
|
||
gg <- if(!{legend}) gg+guides(fill="none") else gg
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
# 基础参数 file_path, chart_out_path, out_path, title, caption, subtitle, title_font_size,
|
||
|
||
|
||
# 高级参数 title_font_color
|
||
# 实例文件 Group_HierarchicalTree.csv
|
||
|
||
def Group_HierarchicalTree(file_path, chart_out_path, out_path, title, caption, subtitle, title_font_size,
|
||
title_font_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}", row.names = 1)
|
||
|
||
library(ggplot2)
|
||
library(ggdendro)
|
||
theme_set(theme_bw())
|
||
|
||
hc <- hclust(dist(datas), "ave") # hierarchical clustering
|
||
|
||
# plot
|
||
gg <- ggdendrogram(hc, rotate = TRUE, size = 2) + labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name, legend_position
|
||
# 实例文件 Sort_Dots.csv
|
||
|
||
def Sort_Dots(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size, y_font_size,
|
||
x_scale_font_size, y_scale_font_size, x_scale_font_angle, y_scale_font_angle, legend,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name,
|
||
legend_position):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
library(ggplot2)
|
||
library(scales)
|
||
theme_set(theme_classic())
|
||
|
||
# Plot
|
||
gg <- ggplot(datas, aes(x, y, fill=x)) +
|
||
geom_point(col="tomato2", size=3) + # Draw points
|
||
geom_segment(aes(x=x,
|
||
xend=x,
|
||
y=min(y),
|
||
yend=max(y)),
|
||
linetype="dashed",
|
||
size=0.1) + # Draw dashed lines
|
||
coord_flip() +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
fill="{legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"),
|
||
legend.position="{legend_position}")
|
||
|
||
gg <- if(!{legend}) gg+guides(fill="none") else gg
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, species_1, species_2
|
||
# 实例文件 Sort_Lean.csv
|
||
|
||
def Sort_Lean(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size, y_font_size,
|
||
x_scale_font_size, y_scale_font_size, x_scale_font_angle, y_scale_font_angle, time_1, time_2,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, species_1,
|
||
species_2):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
df <- read.csv("{file_path}")
|
||
|
||
library(ggplot2)
|
||
library(scales)
|
||
theme_set(theme_classic())
|
||
|
||
# prep data
|
||
colnames(df) <- c("continent", "{time_1}", "{time_2}")
|
||
left_label <- paste(df$continent, round(df$`{time_1}`),sep=", ")
|
||
right_label <- paste(df$continent, round(df$`{time_2}`),sep=", ")
|
||
df$class <- ifelse((df$`{time_2}` - df$`{time_1}`) < 0, "red", "green")
|
||
|
||
# Plot
|
||
g <- ggplot(df) +
|
||
geom_segment(aes(x=1, xend=2,
|
||
y=`{time_1}`, yend=`{time_2}`,
|
||
col=class), size=.75,
|
||
show.legend=F) +
|
||
geom_vline(xintercept=1, linetype="dashed", size=.1) +
|
||
geom_vline(xintercept=2, linetype="dashed", size=.1) +
|
||
scale_color_manual(labels = c("Up", "Down"),
|
||
values = c("green"="#00ba38",
|
||
"red"="#f8766d")) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}")) +
|
||
xlim(.5, 2.5) + ylim(0,(1.1*(max(df$`{time_1}`, df$`{time_2}`))))
|
||
|
||
# Add texts
|
||
g <- g + geom_text(label=left_label, y=df$`{time_1}`,
|
||
x=rep(1, NROW(df)), hjust=1.1, size=3.5)
|
||
g <- g + geom_text(label=right_label, y=df$`{time_2}`,
|
||
x=rep(2, NROW(df)), hjust=-0.1, size=3.5)
|
||
g <- g + geom_text(label="{species_1}", x=1, y=1.1*(max(df$`{time_1}`,
|
||
df$`{time_2}`)), hjust=1.2, size=5)
|
||
g <- g + geom_text(label="{species_2}", x=2, y=1.1*(max(df$`{time_1}`,
|
||
df$`{time_2}`)), hjust=-0.1, size=5)
|
||
|
||
# Minify theme
|
||
gg <- g + theme(panel.grid = element_blank(),
|
||
axis.ticks = element_blank(),
|
||
axis.text.x = element_blank(),
|
||
panel.border = element_blank(),
|
||
plot.margin = unit(c(1,2,1,2), "cm"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name, legend_position
|
||
# 实例文件 Sort_Lollipop.csv
|
||
|
||
def Sort_Lollipop(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size,
|
||
y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle, y_scale_font_angle, legend,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name,
|
||
legend_position):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
library(ggplot2)
|
||
theme_set(theme_bw())
|
||
|
||
# Plot
|
||
gg <- ggplot(datas, aes(x, y, fill=x)) +
|
||
geom_point(size=3) +
|
||
geom_segment(aes(x=x,
|
||
xend=x,
|
||
y=0,
|
||
yend=y)) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
fill="{legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"),
|
||
legend.position="{legend_position}")
|
||
|
||
gg <- if(!{legend}) gg+guides(fill="none") else gg
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 Sort_OrderedBar.csv
|
||
|
||
def Sort_OrderedBar(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name, title_font_size,
|
||
x_font_size,
|
||
y_font_size, x_scale_font_size, y_scale_font_size, x_scale_font_angle, y_scale_font_angle,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
x_name = x_name.replace(' ', '_')
|
||
|
||
y_name = y_name.replace(' ', '_')
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
df <- aggregate(y, by=list(x), FUN=mean)
|
||
colnames(df) <- c("{x_name}", "{y_name}")
|
||
df <- df[order(df${y_name}), ]
|
||
df${x_name} <- factor(df${x_name}, levels = df${x_name})
|
||
library(ggplot2)
|
||
theme_set(theme_bw())
|
||
|
||
# Draw plot
|
||
gg <- ggplot(df, aes(x={x_name}, y={y_name}, fill={x_name})) +
|
||
geom_bar(stat="identity", width=.5, fill="tomato3") +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 TemporalChanges_Accumulation.csv
|
||
|
||
def TemporalChanges_Accumulation(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name,
|
||
title_font_size, x_font_size, y_font_size, x_scale_font_size, y_scale_font_size,
|
||
x_scale_font_angle, y_scale_font_angle,
|
||
type_1, type_2,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
z <- datas[,3]
|
||
|
||
library(ggplot2)
|
||
library(lubridate)
|
||
theme_set(theme_bw())
|
||
|
||
x <- as.Date(x)
|
||
|
||
# labels and breaks for X axis text
|
||
brks <- x[seq(1, length(x), 12)]
|
||
lbls <- lubridate::year(brks)
|
||
|
||
# plot
|
||
gg <- ggplot(datas, aes(x)) +
|
||
geom_area(aes(y=y+z, fill="{type_1}")) +
|
||
geom_area(aes(y=z, fill="{type_2}")) +
|
||
scale_x_date(labels = lbls, breaks = brks) +
|
||
scale_fill_manual(
|
||
values = c("{type_1}"="#00ba38", "{type_2}"="#f8766d")) +
|
||
theme(panel.grid.minor = element_blank()) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color, legend_name, legend_position
|
||
# 实例文件 TemporalChanges_CalendarHeat.csv
|
||
|
||
def TemporalChanges_CalendarHeat(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name,
|
||
title_font_size, x_font_size, y_font_size, x_scale_font_size, y_scale_font_size,
|
||
x_scale_font_angle, y_scale_font_angle,
|
||
legend,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color,
|
||
legend_name,
|
||
legend_position):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
df <- read.csv("{file_path}")
|
||
|
||
x <- df[,1]
|
||
|
||
y <- df[,2]
|
||
|
||
z <- df[,3]
|
||
|
||
w <- df[,4]
|
||
|
||
v <- df[,5]
|
||
|
||
u <- df[,6]
|
||
|
||
library(ggplot2)
|
||
library(plyr)
|
||
library(scales)
|
||
library(zoo)
|
||
|
||
x <- as.Date(x) # format date
|
||
|
||
names(df) <- c("x", "y", "z", "w", "v", "u")
|
||
|
||
# Create Month Week
|
||
df$yearmonth <- as.yearmon(x)
|
||
df$yearmonthf <- factor(df$yearmonth)
|
||
df <- ddply(df,.(yearmonthf), transform, monthweek=1+u-min(u))
|
||
|
||
# Plot
|
||
gg <- ggplot(df, aes(monthweek, v, fill = y)) +
|
||
geom_tile(colour = "white") +
|
||
facet_grid(z~w) +
|
||
scale_fill_gradient(low="red", high="green") +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
fill="{legend_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"),
|
||
legend.position="{legend_position}")
|
||
|
||
gg <- if(!{legend}) gg+guides(fill="none") else gg
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 TemporalChanges_DataframeTimeSeries.csv
|
||
|
||
def TemporalChanges_DataframeTimeSeries(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name,
|
||
title_font_size, x_font_size, y_font_size, x_scale_font_size, y_scale_font_size,
|
||
x_scale_font_angle,
|
||
y_scale_font_angle,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color,
|
||
title_font_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
x <- as.Date(x)
|
||
|
||
library(ggplot2)
|
||
theme_set(theme_classic())
|
||
|
||
# Allow Default X Axis Labels
|
||
gg <- ggplot(datas, aes(x)) +
|
||
geom_line(aes(y=y)) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 TemporalChanges_SeasonalTimeSeries.csv
|
||
|
||
def TemporalChanges_SeasonalTimeSeries(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name,
|
||
title_font_size, x_font_size, y_font_size, x_scale_font_size, y_scale_font_size,
|
||
x_scale_font_angle, y_scale_font_angle,
|
||
time,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color,
|
||
title_font_color):
|
||
time = time.split("-")
|
||
|
||
time = (int(time[0]), int(time[1]))
|
||
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
datas <- ts(datas, frequency = 12, start = c({time[0]},{time[1]}))
|
||
|
||
library(ggplot2)
|
||
library(forecast)
|
||
theme_set(theme_classic())
|
||
|
||
# Plot
|
||
gg <-ggseasonplot(datas) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 TemporalChanges_ShowMultipleTimeSeriesSimultaneously.csv
|
||
|
||
def TemporalChanges_ShowMultipleTimeSeriesSimultaneously(file_path, chart_out_path, out_path, title, caption, subtitle,
|
||
x_name, y_name, title_font_size, x_font_size, y_font_size,
|
||
x_scale_font_size,
|
||
y_scale_font_size, x_scale_font_angle, y_scale_font_angle,
|
||
type_1, type_2,
|
||
x_font_color, y_font_color, x_scale_font_color,
|
||
y_scale_font_color, title_font_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
z <- datas[,3]
|
||
|
||
library(ggplot2)
|
||
library(lubridate)
|
||
theme_set(theme_bw())
|
||
|
||
x <- as.Date(x)
|
||
|
||
# labels and breaks for X axis text
|
||
brks <- x[seq(1, length(x), 12)]
|
||
lbls <- lubridate::year(brks)
|
||
|
||
# plot
|
||
gg <- ggplot(datas, aes(x)) +
|
||
geom_line(aes(y=y, col=z)) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}",
|
||
color=NULL) +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}")) + # title and caption
|
||
scale_x_date(labels = lbls, breaks = brks) +
|
||
scale_color_manual(labels = c("{type_1}", "{type_2}"),
|
||
values = c("{type_1}"="#00ba38",
|
||
"{type_2}"="#f8766d")) +
|
||
theme(panel.grid.minor = element_blank())
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 TemporalChanges_TimeSeries.csv
|
||
|
||
def TemporalChanges_TimeSeries(file_path, chart_out_path, out_path, title, caption, subtitle, x_name, y_name,
|
||
title_font_size, x_font_size, y_font_size, x_scale_font_size, y_scale_font_size,
|
||
x_scale_font_angle, y_scale_font_angle, time,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color):
|
||
time = time.split("-")
|
||
|
||
time = (int(time[0]), int(time[1]))
|
||
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
datas <- ts(datas, frequency = 12, start = c({time[0]},{time[1]}))
|
||
|
||
library(ggplot2)
|
||
library(ggfortify)
|
||
theme_set(theme_classic())
|
||
|
||
# Plot
|
||
gg <- autoplot(datas) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}"))
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 TemporalChanges_TimeSeriesOfAnnualData.csv
|
||
|
||
def TemporalChanges_TimeSeriesOfAnnualData(file_path, chart_out_path, out_path, title, caption, subtitle, x_name,
|
||
y_name, title_font_size, x_font_size, y_font_size, x_scale_font_size,
|
||
y_scale_font_size, x_scale_font_angle,
|
||
y_scale_font_angle,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color,
|
||
title_font_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
library(ggplot2)
|
||
library(lubridate)
|
||
theme_set(theme_bw())
|
||
|
||
x <- as.Date(x)
|
||
|
||
# labels and breaks for X axis text
|
||
brks <- x[seq(1, length(x), 12)]
|
||
lbls <- lubridate::year(brks)
|
||
|
||
# plot
|
||
gg <- ggplot(datas, aes(x)) +
|
||
geom_line(aes(y=y)) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}")) + # title and caption
|
||
scale_x_date(labels = lbls,
|
||
breaks = brks) +
|
||
theme(panel.grid.minor = element_blank())
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 高级参数 x_font_color, y_font_color, x_scale_font_color, y_scale_font_color, title_font_color
|
||
# 实例文件 TemporalChanges_TimeSeriesOfMonthlyData.csv
|
||
|
||
def TemporalChanges_TimeSeriesOfMonthlyData(file_path, chart_out_path, out_path, title, caption, subtitle, x_name,
|
||
y_name, title_font_size, x_font_size, y_font_size, x_scale_font_size,
|
||
y_scale_font_size, x_scale_font_angle,
|
||
y_scale_font_angle,
|
||
x_font_color, y_font_color, x_scale_font_color, y_scale_font_color,
|
||
title_font_color):
|
||
file_path = file_path.replace("\\", "\\\\")
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
x <- datas[,1]
|
||
|
||
y <- datas[,2]
|
||
|
||
library(ggplot2)
|
||
library(lubridate)
|
||
theme_set(theme_bw())
|
||
|
||
x <- as.Date(x)
|
||
|
||
# labels and breaks for X axis text
|
||
lbls <- paste0(month.abb[month(x)], " ", lubridate::year(x))
|
||
brks <- x
|
||
|
||
# plot
|
||
gg <- ggplot(datas, aes(x)) +
|
||
geom_line(aes(y=y)) +
|
||
labs(title="{title}",
|
||
subtitle="{subtitle}",
|
||
caption="{caption}",
|
||
x="{x_name}",
|
||
y="{y_name}") +
|
||
theme(title=element_text(size={title_font_size}, color="{title_font_color}"),
|
||
|
||
axis.text.x=element_text(color="{x_scale_font_color}", size={x_scale_font_size}, angle={x_scale_font_angle}, vjust=0.6), axis.text.y=element_text(color="{y_scale_font_color}", size={y_scale_font_size}, angle={y_scale_font_angle}),
|
||
axis.title.x=element_text(size={x_font_size}, color="{x_font_color}"), axis.title.y=element_text(size={y_font_size}, color="{y_font_color}")) + # title and caption
|
||
scale_x_date(labels = lbls,
|
||
breaks = brks) +
|
||
theme(panel.grid.minor = element_blank())
|
||
|
||
png("{chart_out_path}",width=900,height=408)
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
pdf("{out_path}.pdf")
|
||
plot(gg)
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
plot(gg)
|
||
dev.off()
|
||
"""
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R")
|
||
|
||
|
||
# 最后一组
|
||
|
||
# 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, scale_font_size, font_size, heading_1, heading_2, heading_font_size
|
||
# 高级参数 title_font_color, scale_font_color, line_color, font_color, heading_font_color
|
||
# 示例文件 Aggregated_Pyramids.xlsx
|
||
|
||
def Aggregated_Pyramids(file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, scale_font_size,
|
||
font_size,
|
||
heading_1, heading_2, heading_font_size,
|
||
title_font_color, scale_font_color, line_color, font_color, heading_font_color):
|
||
code = f"""
|
||
library(gdata)
|
||
x<-read.xls("{file_path}", encoding="latin1")
|
||
names(x) <- c("","Group","M","F","des")
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(mai=c(0.2,0.25,0.8,0.25),omi=c(0.75,0.2,0.85,0.2),cex=0.75,las=1)
|
||
|
||
right<-t(as.matrix(data.frame(800,x$F)))
|
||
left<--t(as.matrix(data.frame(800,x$M)))
|
||
|
||
myColour_right<-c(par("bg"),rgb(255,0,210,150,maxColorValue=255))
|
||
myColour_left<-c(par("bg"),rgb(191,239,255,maxColorValue=255))
|
||
|
||
b1<-barplot(right,axes=F,horiz=T,axis.lty=0,border=NA,col=myColour_right,xlim=c(-8000,8000))
|
||
barplot(left,axes=F,horiz=T,axis.lty=0,border=NA,col=myColour_left,xlim=c(-7500,7500),add=T)
|
||
|
||
abline(v=seq(0,6000,by=2000)+800,col="{line_color}",lty=3)
|
||
abline(v=seq(-6000,0,by=2000)-800,col="{line_color}",lty=3)
|
||
|
||
mtext(format(seq(0,6000,by=2000),big.mark="."),at=seq(0,6000,by=2000)+800,1,line=0,cex={scale_font_size},col="{scale_font_color}")
|
||
mtext(format(abs(seq(-6000,0,by=2000)),big.mark="."),at=seq(-6000,0,by=2000)-800,1,line=0,cex={scale_font_size},col="{scale_font_color}")
|
||
text(0,b1,x$des,cex={font_size},font=3,col="{font_color}")
|
||
|
||
mtext("{heading_1}",3,line=1,adj=0.25,cex={heading_font_size},col="{heading_font_color}")
|
||
mtext("{heading_2}",3,line=1,adj=0.75,cex={heading_font_size},col="{heading_font_color}")
|
||
|
||
mtext("{title}",3,line=2,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-0.5,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("...",1,line=2,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(mai=c(0.2,0.25,0.8,0.25),omi=c(0.75,0.2,0.85,0.2),cex=0.75,las=1)
|
||
|
||
right<-t(as.matrix(data.frame(800,x$F)))
|
||
left<--t(as.matrix(data.frame(800,x$M)))
|
||
|
||
myColour_right<-c(par("bg"),rgb(255,0,210,150,maxColorValue=255))
|
||
myColour_left<-c(par("bg"),rgb(191,239,255,maxColorValue=255))
|
||
|
||
b1<-barplot(right,axes=F,horiz=T,axis.lty=0,border=NA,col=myColour_right,xlim=c(-8000,8000))
|
||
barplot(left,axes=F,horiz=T,axis.lty=0,border=NA,col=myColour_left,xlim=c(-7500,7500),add=T)
|
||
|
||
abline(v=seq(0,6000,by=2000)+800,col="{line_color}",lty=3)
|
||
abline(v=seq(-6000,0,by=2000)-800,col="{line_color}",lty=3)
|
||
|
||
mtext(format(seq(0,6000,by=2000),big.mark="."),at=seq(0,6000,by=2000)+800,1,line=0,cex={scale_font_size},col="{scale_font_color}")
|
||
mtext(format(abs(seq(-6000,0,by=2000)),big.mark="."),at=seq(-6000,0,by=2000)-800,1,line=0,cex={scale_font_size},col="{scale_font_color}")
|
||
text(0,b1,x$des,cex={font_size},font=3,col="{font_color}")
|
||
|
||
mtext("{heading_1}",3,line=1,adj=0.25,cex={heading_font_size},col="{heading_font_color}")
|
||
mtext("{heading_2}",3,line=1,adj=0.75,cex={heading_font_size},col="{heading_font_color}")
|
||
|
||
mtext("{title}",3,line=2,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-0.5,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("...",1,line=2,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(mai=c(0.2,0.25,0.8,0.25),omi=c(0.75,0.2,0.85,0.2),cex=0.75,las=1)
|
||
|
||
right<-t(as.matrix(data.frame(800,x$F)))
|
||
left<--t(as.matrix(data.frame(800,x$M)))
|
||
|
||
myColour_right<-c(par("bg"),rgb(255,0,210,150,maxColorValue=255))
|
||
myColour_left<-c(par("bg"),rgb(191,239,255,maxColorValue=255))
|
||
|
||
b1<-barplot(right,axes=F,horiz=T,axis.lty=0,border=NA,col=myColour_right,xlim=c(-8000,8000))
|
||
barplot(left,axes=F,horiz=T,axis.lty=0,border=NA,col=myColour_left,xlim=c(-7500,7500),add=T)
|
||
|
||
abline(v=seq(0,6000,by=2000)+800,col="{line_color}",lty=3)
|
||
abline(v=seq(-6000,0,by=2000)-800,col="{line_color}",lty=3)
|
||
|
||
mtext(format(seq(0,6000,by=2000),big.mark="."),at=seq(0,6000,by=2000)+800,1,line=0,cex={scale_font_size},col="{scale_font_color}")
|
||
mtext(format(abs(seq(-6000,0,by=2000)),big.mark="."),at=seq(-6000,0,by=2000)-800,1,line=0,cex={scale_font_size},col="{scale_font_color}")
|
||
text(0,b1,x$des,cex={font_size},font=3,col="{font_color}")
|
||
|
||
mtext("{heading_1}",3,line=1,adj=0.25,cex={heading_font_size},col="{heading_font_color}")
|
||
mtext("{heading_2}",3,line=1,adj=0.75,cex={heading_font_size},col="{heading_font_color}")
|
||
|
||
mtext("{title}",3,line=2,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-0.5,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("...",1,line=2,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
|
||
svg("{out_path}.svg")
|
||
par(mai=c(0.2,0.25,0.8,0.25),omi=c(0.75,0.2,0.85,0.2),cex=0.75,las=1)
|
||
|
||
right<-t(as.matrix(data.frame(800,x$F)))
|
||
left<--t(as.matrix(data.frame(800,x$M)))
|
||
|
||
myColour_right<-c(par("bg"),rgb(255,0,210,150,maxColorValue=255))
|
||
myColour_left<-c(par("bg"),rgb(191,239,255,maxColorValue=255))
|
||
|
||
b1<-barplot(right,axes=F,horiz=T,axis.lty=0,border=NA,col=myColour_right,xlim=c(-8000,8000))
|
||
barplot(left,axes=F,horiz=T,axis.lty=0,border=NA,col=myColour_left,xlim=c(-7500,7500),add=T)
|
||
|
||
abline(v=seq(0,6000,by=2000)+800,col="{line_color}",lty=3)
|
||
abline(v=seq(-6000,0,by=2000)-800,col="{line_color}",lty=3)
|
||
|
||
mtext(format(seq(0,6000,by=2000),big.mark="."),at=seq(0,6000,by=2000)+800,1,line=0,cex={scale_font_size},col="{scale_font_color}")
|
||
mtext(format(abs(seq(-6000,0,by=2000)),big.mark="."),at=seq(-6000,0,by=2000)-800,1,line=0,cex={scale_font_size},col="{scale_font_color}")
|
||
text(0,b1,x$des,cex={font_size},font=3,col="{font_color}")
|
||
|
||
mtext("{heading_1}",3,line=1,adj=0.25,cex={heading_font_size},col="{heading_font_color}")
|
||
mtext("{heading_2}",3,line=1,adj=0.75,cex={heading_font_size},col="{heading_font_color}")
|
||
|
||
mtext("{title}",3,line=2,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-0.5,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("...",1,line=2,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R")
|
||
|
||
|
||
# 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size, event_1, event_2, event_3, font_size
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color, font_color, line_color, color
|
||
# 示例文件 Areas_Under_a_Time_Series.csv
|
||
|
||
def Areas_Under_a_Time_Series(file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name,
|
||
x_font_size,
|
||
x_scale_font_size, y_name, y_font_size, y_scale_font_size, event_1, event_2, event_3,
|
||
font_size,
|
||
title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color,
|
||
font_color, line_color, color):
|
||
|
||
font_color = font_color[4:-1]
|
||
|
||
color = color[4:-1]
|
||
|
||
code = f"""
|
||
library(gdata)
|
||
colour<-rgb({font_color},150,maxColorValue=255)
|
||
myShapeColour1<-rgb({color},50,maxColorValue=255)
|
||
myShapeColour2<-rgb({color},80,maxColorValue=255)
|
||
myData<-read.csv('{file_path}',encoding="latin1")
|
||
names(myData) <- c("x","y")
|
||
attach(myData)
|
||
mySelection<-subset(myData,x >= 1879 & x <= 1884)
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(cex.axis=1.1,mai=c(0.75,1.5,0.25,0.5),omi=c(0.5,0.5,1.1,0.5), mgp=c(6,1,0),las=1)
|
||
|
||
plot(x,y,axes=F,type="n",xlab="",xlim=c(1800,2020),ylim=c(0,14000),xpd=T,ylab="{y_name}",cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
axis(1,at=pretty(x),col="{x_scale_font_color}",cex.axis={x_scale_font_size},col.axis="{x_scale_font_color}")
|
||
axis(2,at=py<-pretty(y),col="{y_scale_font_color}",cex.lab=1.2,labels=format(py,big.mark=","),cex.axis={y_scale_font_size},col.axis="{y_scale_font_color}")
|
||
|
||
y<-ts(y,start=1800,frequency=1)
|
||
points(window(y, end=1869),col="{line_color}")
|
||
lines(window(y, start=1870),col="{line_color}")
|
||
|
||
attach(mySelection)
|
||
polygon(c(min(mySelection$x),mySelection$x,max(mySelection$x)),c(-500,mySelection$y,-500),col=myShapeColour2,border=NA)
|
||
text(1860,2200,adj=0,col=colour,"{event_1}",cex={font_size})
|
||
mySelection<-subset(myData,x >= 1940 & x <= 1973)
|
||
attach(mySelection)
|
||
polygon(c(min(mySelection$x),mySelection$x,max(mySelection$x)),c(-500,mySelection$y,-500),col=myShapeColour1,border=NA)
|
||
text(1930,5000,adj=0,col=colour,"{event_2}",cex={font_size})
|
||
mySelection<-subset(myData,x >= 1973 & x <= 1990)
|
||
attach(mySelection)
|
||
polygon(c(min(mySelection$x),mySelection$x,max(mySelection$x)),c(-500,mySelection$y,-500),col=myShapeColour2,border=NA)
|
||
text(1960,6800,adj=0,col=colour,"{event_3}",cex={font_size})
|
||
|
||
mtext("{title}",3,line=2,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-0.5,adj=0,cex={title_font_size}*0.6,font=3, outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=4.5,adj=1,cex={title_font_size}*0.4,font=3,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1,adj=0.57,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
myData<-read.csv('{file_path}',encoding="latin1")
|
||
names(myData) <- c("x","y")
|
||
attach(myData)
|
||
mySelection<-subset(myData,x >= 1879 & x <= 1884)
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(cex.axis=1.1,mai=c(0.75,1.5,0.25,0.5),omi=c(0.5,0.5,1.1,0.5), mgp=c(6,1,0),las=1)
|
||
|
||
plot(x,y,axes=F,type="n",xlab="",xlim=c(1800,2020),ylim=c(0,14000),xpd=T,ylab="{y_name}",cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
axis(1,at=pretty(x),col="{x_scale_font_color}",cex.axis={x_scale_font_size},col.axis="{x_scale_font_color}")
|
||
axis(2,at=py<-pretty(y),col="{y_scale_font_color}",cex.lab=1.2,labels=format(py,big.mark=","),cex.axis={y_scale_font_size},col.axis="{y_scale_font_color}")
|
||
|
||
y<-ts(y,start=1800,frequency=1)
|
||
points(window(y, end=1869),col="{line_color}")
|
||
lines(window(y, start=1870),col="{line_color}")
|
||
|
||
attach(mySelection)
|
||
polygon(c(min(mySelection$x),mySelection$x,max(mySelection$x)),c(-500,mySelection$y,-500),col=myShapeColour2,border=NA)
|
||
text(1860,2200,adj=0,col=colour,"{event_1}",cex={font_size})
|
||
mySelection<-subset(myData,x >= 1940 & x <= 1973)
|
||
attach(mySelection)
|
||
polygon(c(min(mySelection$x),mySelection$x,max(mySelection$x)),c(-500,mySelection$y,-500),col=myShapeColour1,border=NA)
|
||
text(1930,5000,adj=0,col=colour,"{event_2}",cex={font_size})
|
||
mySelection<-subset(myData,x >= 1973 & x <= 1990)
|
||
attach(mySelection)
|
||
polygon(c(min(mySelection$x),mySelection$x,max(mySelection$x)),c(-500,mySelection$y,-500),col=myShapeColour2,border=NA)
|
||
text(1960,6800,adj=0,col=colour,"{event_3}",cex={font_size})
|
||
|
||
mtext("{title}",3,line=2,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-0.5,adj=0,cex={title_font_size}*0.6,font=3, outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=4.5,adj=1,cex={title_font_size}*0.4,font=3,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1,adj=0.57,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
myData<-read.csv('{file_path}',encoding="latin1")
|
||
names(myData) <- c("x","y")
|
||
attach(myData)
|
||
mySelection<-subset(myData,x >= 1879 & x <= 1884)
|
||
|
||
png("{out_path}.png")
|
||
par(cex.axis=1.1,mai=c(0.75,1.5,0.25,0.5),omi=c(0.5,0.5,1.1,0.5), mgp=c(6,1,0),las=1)
|
||
|
||
plot(x,y,axes=F,type="n",xlab="",xlim=c(1800,2020),ylim=c(0,14000),xpd=T,ylab="{y_name}",cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
axis(1,at=pretty(x),col="{x_scale_font_color}",cex.axis={x_scale_font_size},col.axis="{x_scale_font_color}")
|
||
axis(2,at=py<-pretty(y),col="{y_scale_font_color}",cex.lab=1.2,labels=format(py,big.mark=","),cex.axis={y_scale_font_size},col.axis="{y_scale_font_color}")
|
||
|
||
y<-ts(y,start=1800,frequency=1)
|
||
points(window(y, end=1869),col="{line_color}")
|
||
lines(window(y, start=1870),col="{line_color}")
|
||
|
||
attach(mySelection)
|
||
polygon(c(min(mySelection$x),mySelection$x,max(mySelection$x)),c(-500,mySelection$y,-500),col=myShapeColour2,border=NA)
|
||
text(1860,2200,adj=0,col=colour,"{event_1}",cex={font_size})
|
||
mySelection<-subset(myData,x >= 1940 & x <= 1973)
|
||
attach(mySelection)
|
||
polygon(c(min(mySelection$x),mySelection$x,max(mySelection$x)),c(-500,mySelection$y,-500),col=myShapeColour1,border=NA)
|
||
text(1930,5000,adj=0,col=colour,"{event_2}",cex={font_size})
|
||
mySelection<-subset(myData,x >= 1973 & x <= 1990)
|
||
attach(mySelection)
|
||
polygon(c(min(mySelection$x),mySelection$x,max(mySelection$x)),c(-500,mySelection$y,-500),col=myShapeColour2,border=NA)
|
||
text(1960,6800,adj=0,col=colour,"{event_3}",cex={font_size})
|
||
|
||
mtext("{title}",3,line=2,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-0.5,adj=0,cex={title_font_size}*0.6,font=3, outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=4.5,adj=1,cex={title_font_size}*0.4,font=3,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1,adj=0.57,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R")
|
||
|
||
|
||
# 基础参数 file_path, char_out_path, out_path, title, caption_1, caption_2, subtitle, title_font_size, font_size
|
||
# 高级参数 title_font_color, font_color, color_1, color_2
|
||
# 示例文件 Balloon_Plot.csv
|
||
|
||
def Balloon_Plot(file_path, char_out_path, out_path, title, caption_1, caption_2, subtitle, title_font_size, font_size,
|
||
title_font_color, font_color, color_1, color_2):
|
||
code = f"""
|
||
library(gplots)
|
||
myData <- read.csv("{file_path}")
|
||
attach(myData)
|
||
myColours <- character(length(myData$Survived))
|
||
for(i in 1:length(myData$Survived))
|
||
myColours[i] <- if(myData$Survived[i] == "Yes") "{color_1}" else "{color_2}"
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(omi=c(0.75,0.25,0.5,0.25),mai=c(0.25,0.55,0.25,0),cex={font_size})
|
||
balloonplot(x=list(Age,Sex),main="",
|
||
y=list(Class=Class,
|
||
Survived=gdata::reorder.factor(Survived,new.order=c(2,1))),
|
||
z=Freq,dotsize=18,
|
||
sort=T,
|
||
dotcol=myColours,
|
||
show.zeros=T,
|
||
show.margins=T,
|
||
text.color="{font_color}",
|
||
label.color="{font_color}")
|
||
|
||
mtext("{title}",3,line=0,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-2,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption_1}",1,line=1,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption_2}",1,line=1,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(omi=c(0.75,0.25,0.5,0.25),mai=c(0.25,0.55,0.25,0),cex={font_size})
|
||
balloonplot(x=list(Age,Sex),main="",
|
||
y=list(Class=Class,
|
||
Survived=gdata::reorder.factor(Survived,new.order=c(2,1))),
|
||
z=Freq,dotsize=18,
|
||
sort=T,
|
||
dotcol=myColours,
|
||
show.zeros=T,
|
||
show.margins=T,
|
||
text.color="{font_color}",
|
||
label.color="{font_color}")
|
||
|
||
mtext("{title}",3,line=0,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-2,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption_1}",1,line=1,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption_2}",1,line=1,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(omi=c(0.75,0.25,0.5,0.25),mai=c(0.25,0.55,0.25,0),cex={font_size})
|
||
balloonplot(x=list(Age,Sex),main="",
|
||
y=list(Class=Class,
|
||
Survived=gdata::reorder.factor(Survived,new.order=c(2,1))),
|
||
z=Freq,dotsize=18,
|
||
sort=T,
|
||
dotcol=myColours,
|
||
show.zeros=T,
|
||
show.margins=T,
|
||
text.color="{font_color}",
|
||
label.color="{font_color}")
|
||
|
||
mtext("{title}",3,line=0,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-2,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption_1}",1,line=1,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption_2}",1,line=1,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=408)
|
||
par(omi=c(0.75,0.25,0.5,0.25),mai=c(0.25,0.55,0.25,0),cex={font_size})
|
||
balloonplot(x=list(Age,Sex),main="",
|
||
y=list(Class=Class,
|
||
Survived=gdata::reorder.factor(Survived,new.order=c(2,1))),
|
||
z=Freq,dotsize=18,
|
||
sort=T,
|
||
dotcol=myColours,
|
||
show.zeros=T,
|
||
show.margins=T,
|
||
text.color="{font_color}",
|
||
label.color="{font_color}")
|
||
|
||
mtext("{title}",3,line=0,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-2,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption_1}",1,line=1,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption_2}",1,line=1,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size, image_name_font_size, mark_country_1, mark_country_2, average_font_size
|
||
|
||
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color, image_name_font_color, average_font_color, mark_color, color
|
||
# 示例文件 Bar_Chart_Simple.xlsx
|
||
|
||
def Bar_Chart_Simple(file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name, x_font_size,
|
||
x_scale_font_size, y_name, y_font_size, y_scale_font_size, image_name_font_size, mark_country_1,
|
||
mark_country_2, average_font_size,
|
||
title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color,
|
||
image_name_font_color, background_color,
|
||
average_font_color, mark_color, color):
|
||
y_name = "\n".join(y_name)
|
||
|
||
background_color = background_color[4:-1]
|
||
|
||
mark_color = mark_color[4:-1]
|
||
|
||
code = f"""
|
||
library(gdata)
|
||
ipsos<-read.xls('{file_path}',encoding="latin1")
|
||
names(ipsos) <- c("Country","Percent")
|
||
sort.ipsos<-ipsos[order(ipsos$Percent) ,]
|
||
attach(sort.ipsos)
|
||
percent_max <- max(ipsos[,2])/2
|
||
myValue2 <- numeric(length(Country))
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(omi=c(0.65,0.25,0.75,0.75),mai=c(0.3,2,0.35,0),mgp=c(3,3,0),las=1)
|
||
x<-barplot(Percent,names.arg=F,horiz=T,border=NA,xlim=c(0,100),col="{color}", cex.names=0.85,axes=F)
|
||
|
||
for (i in 1:length(Country))
|
||
{{
|
||
if (Country[i] %in% c("{mark_country_1}","{mark_country_2}"))
|
||
{{
|
||
myValue2[i] <- Percent[i]
|
||
text(-8,x[i],Country[i],xpd=T,adj=1,cex={y_scale_font_size},col="{y_scale_font_color}")
|
||
}}
|
||
text(-3.5,x[i],Percent[i],xpd=T,adj=1,cex={y_scale_font_size},col="{y_scale_font_color}")
|
||
}}
|
||
|
||
rect(0,-0.5,20,28,col=rgb({background_color},80,maxColorValue=255),border=NA)
|
||
rect(20,-0.5,40,28,col=rgb({background_color},120,maxColorValue=255),border=NA)
|
||
rect(40,-0.5,60,28,col=rgb({background_color},80,maxColorValue=255),border=NA)
|
||
rect(60,-0.5,80,28,col=rgb({background_color},120,maxColorValue=255),border=NA)
|
||
rect(80,-0.5,100,28,col=rgb({background_color},80,maxColorValue=255),border=NA)
|
||
|
||
myColour2<-rgb({mark_color},maxColorValue=255)
|
||
x2<-barplot(myValue2,names.arg=F,horiz=T,border=NA,xlim=c(0,100),col=myColour2,cex.names=0.85,axes=F,add=T)
|
||
|
||
arrows(percent_max,-0.5,percent_max,20.5,lwd=1.5,length=0,xpd=T,col="{average_font_color}")
|
||
arrows(percent_max,-0.5,percent_max,-0.75,lwd=3,length=0,xpd=T)
|
||
arrows(percent_max,20.5,percent_max,20.75,lwd=3,length=0,xpd=T)
|
||
text(percent_max-5,20.5,"Average",adj=1,xpd=T,cex={average_font_size},font=3)
|
||
text(percent_max-1,20.5,percent_max,adj=1,xpd=T,cex={average_font_size},font=4,col="{average_font_color}")
|
||
text(100,20.5,"All values in percent",adj=1,xpd=T,cex={image_name_font_size},font=3,col="{image_name_font_color}")
|
||
mtext(c(0,20,40,60,80,100),at=c(0,20,40,60,80,100),1,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
|
||
mtext("{title}",3,line={title_font_size},adj=0,cex=1.2,outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-0.4,adj=0,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=1.0,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-0.2,adj=0.6,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
mtext("{y_name}",2,line=0.25,adj=0.5,cex={y_font_size},font=3,outer=T,col="{y_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(omi=c(0.65,0.25,0.75,0.75),mai=c(0.3,2,0.35,0),mgp=c(3,3,0),las=1)
|
||
x<-barplot(Percent,names.arg=F,horiz=T,border=NA,xlim=c(0,100),col="{color}", cex.names=0.85,axes=F)
|
||
|
||
for (i in 1:length(Country))
|
||
{{
|
||
if (Country[i] %in% c("{mark_country_1}","{mark_country_2}"))
|
||
{{
|
||
myValue2[i] <- Percent[i]
|
||
text(-8,x[i],Country[i],xpd=T,adj=1,cex={y_scale_font_size},col="{y_scale_font_color}")
|
||
}}
|
||
text(-3.5,x[i],Percent[i],xpd=T,adj=1,cex={y_scale_font_size},col="{y_scale_font_color}")
|
||
}}
|
||
|
||
rect(0,-0.5,20,28,col=rgb({background_color},80,maxColorValue=255),border=NA)
|
||
rect(20,-0.5,40,28,col=rgb({background_color},120,maxColorValue=255),border=NA)
|
||
rect(40,-0.5,60,28,col=rgb({background_color},80,maxColorValue=255),border=NA)
|
||
rect(60,-0.5,80,28,col=rgb({background_color},120,maxColorValue=255),border=NA)
|
||
rect(80,-0.5,100,28,col=rgb({background_color},80,maxColorValue=255),border=NA)
|
||
|
||
myColour2<-rgb({mark_color},maxColorValue=255)
|
||
x2<-barplot(myValue2,names.arg=F,horiz=T,border=NA,xlim=c(0,100),col=myColour2,cex.names=0.85,axes=F,add=T)
|
||
|
||
arrows(percent_max,-0.5,percent_max,20.5,lwd=1.5,length=0,xpd=T,col="{average_font_color}")
|
||
arrows(percent_max,-0.5,percent_max,-0.75,lwd=3,length=0,xpd=T)
|
||
arrows(percent_max,20.5,percent_max,20.75,lwd=3,length=0,xpd=T)
|
||
text(percent_max-5,20.5,"Average",adj=1,xpd=T,cex={average_font_size},font=3)
|
||
text(percent_max-1,20.5,percent_max,adj=1,xpd=T,cex={average_font_size},font=4,col="{average_font_color}")
|
||
text(100,20.5,"All values in percent",adj=1,xpd=T,cex={image_name_font_size},font=3,col="{image_name_font_color}")
|
||
mtext(c(0,20,40,60,80,100),at=c(0,20,40,60,80,100),1,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
|
||
mtext("{title}",3,line={title_font_size},adj=0,cex=1.2,outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-0.4,adj=0,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=1.0,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-0.2,adj=0.6,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
mtext("{y_name}",2,line=0.25,adj=0.5,cex={y_font_size},font=3,outer=T,col="{y_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=408)
|
||
par(omi=c(0.65,0.25,0.75,0.75),mai=c(0.3,2,0.35,0),mgp=c(3,3,0),las=1)
|
||
x<-barplot(Percent,names.arg=F,horiz=T,border=NA,xlim=c(0,100),col="{color}", cex.names=0.85,axes=F)
|
||
|
||
for (i in 1:length(Country))
|
||
{{
|
||
if (Country[i] %in% c("{mark_country_1}","{mark_country_2}"))
|
||
{{
|
||
myValue2[i] <- Percent[i]
|
||
text(-8,x[i],Country[i],xpd=T,adj=1,cex={y_scale_font_size},col="{y_scale_font_color}")
|
||
}}
|
||
text(-3.5,x[i],Percent[i],xpd=T,adj=1,cex={y_scale_font_size},col="{y_scale_font_color}")
|
||
}}
|
||
|
||
rect(0,-0.5,20,28,col=rgb({background_color},80,maxColorValue=255),border=NA)
|
||
rect(20,-0.5,40,28,col=rgb({background_color},120,maxColorValue=255),border=NA)
|
||
rect(40,-0.5,60,28,col=rgb({background_color},80,maxColorValue=255),border=NA)
|
||
rect(60,-0.5,80,28,col=rgb({background_color},120,maxColorValue=255),border=NA)
|
||
rect(80,-0.5,100,28,col=rgb({background_color},80,maxColorValue=255),border=NA)
|
||
|
||
myColour2<-rgb({mark_color},maxColorValue=255)
|
||
x2<-barplot(myValue2,names.arg=F,horiz=T,border=NA,xlim=c(0,100),col=myColour2,cex.names=0.85,axes=F,add=T)
|
||
|
||
arrows(percent_max,-0.5,percent_max,20.5,lwd=1.5,length=0,xpd=T,col="{average_font_color}")
|
||
arrows(percent_max,-0.5,percent_max,-0.75,lwd=3,length=0,xpd=T)
|
||
arrows(percent_max,20.5,percent_max,20.75,lwd=3,length=0,xpd=T)
|
||
text(percent_max-5,20.5,"Average",adj=1,xpd=T,cex={average_font_size},font=3)
|
||
text(percent_max-1,20.5,percent_max,adj=1,xpd=T,cex={average_font_size},font=4,col="{average_font_color}")
|
||
text(100,20.5,"All values in percent",adj=1,xpd=T,cex={image_name_font_size},font=3,col="{image_name_font_color}")
|
||
mtext(c(0,20,40,60,80,100),at=c(0,20,40,60,80,100),1,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
|
||
mtext("{title}",3,line={title_font_size},adj=0,cex=1.2,outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-0.4,adj=0,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=1.0,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-0.2,adj=0.6,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
mtext("{y_name}",2,line=0.25,adj=0.5,cex={y_font_size},font=3,outer=T,col="{y_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(omi=c(0.65,0.25,0.75,0.75),mai=c(0.3,2,0.35,0),mgp=c(3,3,0),las=1)
|
||
x<-barplot(Percent,names.arg=F,horiz=T,border=NA,xlim=c(0,100),col="{color}", cex.names=0.85,axes=F)
|
||
|
||
for (i in 1:length(Country))
|
||
{{
|
||
if (Country[i] %in% c("{mark_country_1}","{mark_country_2}"))
|
||
{{
|
||
myValue2[i] <- Percent[i]
|
||
text(-8,x[i],Country[i],xpd=T,adj=1,cex={y_scale_font_size},col="{y_scale_font_color}")
|
||
}}
|
||
text(-3.5,x[i],Percent[i],xpd=T,adj=1,cex={y_scale_font_size},col="{y_scale_font_color}")
|
||
}}
|
||
|
||
rect(0,-0.5,20,28,col=rgb({background_color},80,maxColorValue=255),border=NA)
|
||
rect(20,-0.5,40,28,col=rgb({background_color},120,maxColorValue=255),border=NA)
|
||
rect(40,-0.5,60,28,col=rgb({background_color},80,maxColorValue=255),border=NA)
|
||
rect(60,-0.5,80,28,col=rgb({background_color},120,maxColorValue=255),border=NA)
|
||
rect(80,-0.5,100,28,col=rgb({background_color},80,maxColorValue=255),border=NA)
|
||
|
||
myColour2<-rgb({mark_color},maxColorValue=255)
|
||
x2<-barplot(myValue2,names.arg=F,horiz=T,border=NA,xlim=c(0,100),col=myColour2,cex.names=0.85,axes=F,add=T)
|
||
|
||
arrows(percent_max,-0.5,percent_max,20.5,lwd=1.5,length=0,xpd=T,col="{average_font_color}")
|
||
arrows(percent_max,-0.5,percent_max,-0.75,lwd=3,length=0,xpd=T)
|
||
arrows(percent_max,20.5,percent_max,20.75,lwd=3,length=0,xpd=T)
|
||
text(percent_max-5,20.5,"Average",adj=1,xpd=T,cex={average_font_size},font=3)
|
||
text(percent_max-1,20.5,percent_max,adj=1,xpd=T,cex={average_font_size},font=4,col="{average_font_color}")
|
||
text(100,20.5,"All values in percent",adj=1,xpd=T,cex={image_name_font_size},font=3,col="{image_name_font_color}")
|
||
mtext(c(0,20,40,60,80,100),at=c(0,20,40,60,80,100),1,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
|
||
mtext("{title}",3,line={title_font_size},adj=0,cex=1.2,outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-0.4,adj=0,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=1.0,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-0.2,adj=0.6,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
mtext("{y_name}",2,line=0.25,adj=0.5,cex={y_font_size},font=3,outer=T,col="{y_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R")
|
||
|
||
|
||
# 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, scale_font_size
|
||
# 高级参数 title_font_color, scale_font_color, mark_scale_font_color, font_color
|
||
# 示例文件 Bump_Chart.xlsx
|
||
|
||
def Bump_Chart(file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, scale_font_size,
|
||
title_font_color, scale_font_color, mark_scale_font_color, font_color):
|
||
code = f"""
|
||
library(plotrix)
|
||
library(gdata)
|
||
|
||
z1<-read.xls('{file_path}', row.names = 1, encoding="latin1")
|
||
|
||
myColours<-rep("{scale_font_color}",nrow(z1)); myLineWidth<-rep(1,nrow(z1))
|
||
myColours[5]<-"{mark_scale_font_color}"; myLineWidth[5]<-8
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(omi=c(0.5,0.5,0.9,0.5),mai=c(0,0.75,0.25,0.75),xpd=T,las=1,col="{font_color}")
|
||
|
||
bumpchart(z1,rank=F,pch=18,top.labels=c("",""),col=myColours,lwd=myLineWidth,mar=c(2,12,1,12),cex=1.1)
|
||
|
||
mtext("{title}",3,line=1.5,adj=0,outer=T,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{caption}",1,line=0,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
|
||
axis(2,col=par("bg"),col.ticks="{scale_font_color}",col.axis="{scale_font_color}",lwd.ticks=0.5,tck=-0.025, at=c(min(z1$r2002), max(z1$r2002)),c(round(min(z1$r2002)/1000,digits=1), round(max(z1$r2002)/1000, digits=1)),cex.axis={scale_font_size})
|
||
axis(4,col=par("bg"),col.ticks="{scale_font_color}",col.axis="{scale_font_color}",lwd.ticks=0.5,tck=-0.025, at=c(min(z1$r2011), max(z1$r2011)),c(round(min(z1$r2011)/1000,digits=1), round(max(z1$r2011)/1000, digits=1)),cex.axis={scale_font_size})
|
||
|
||
mtext("{subtitle}",3,font=3,adj=0,cex={title_font_size}*0.6,line=-0.5,outer=T,col="{title_font_color}")
|
||
|
||
axis(2,col=par("bg"),col.ticks="{mark_scale_font_color}",col.axis="{mark_scale_font_color}",lwd.ticks=0.5,tck=-0.025,at=z1[5,1],round(z1[5,1]/1000, digits=1),cex.axis={scale_font_size})
|
||
axis(4,col=par("bg"),col.ticks="{mark_scale_font_color}",col.axis="{mark_scale_font_color}",lwd.ticks=0.5,tck=-0.025,at=z1[5,2],round(z1[5,2]/1000, digits=1),cex.axis={scale_font_size})
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(omi=c(0.5,0.5,0.9,0.5),mai=c(0,0.75,0.25,0.75),xpd=T,las=1,col="{font_color}")
|
||
|
||
bumpchart(z1,rank=F,pch=18,top.labels=c("",""),col=myColours,lwd=myLineWidth,mar=c(2,12,1,12),cex=1.1)
|
||
|
||
mtext("{title}",3,line=1.5,adj=0,outer=T,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{caption}",1,line=0,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
|
||
axis(2,col=par("bg"),col.ticks="{scale_font_color}",col.axis="{scale_font_color}",lwd.ticks=0.5,tck=-0.025, at=c(min(z1$r2002), max(z1$r2002)),c(round(min(z1$r2002)/1000,digits=1), round(max(z1$r2002)/1000, digits=1)),cex.axis={scale_font_size})
|
||
axis(4,col=par("bg"),col.ticks="{scale_font_color}",col.axis="{scale_font_color}",lwd.ticks=0.5,tck=-0.025, at=c(min(z1$r2011), max(z1$r2011)),c(round(min(z1$r2011)/1000,digits=1), round(max(z1$r2011)/1000, digits=1)),cex.axis={scale_font_size})
|
||
|
||
mtext("{subtitle}",3,font=3,adj=0,cex={title_font_size}*0.6,line=-0.5,outer=T,col="{title_font_color}")
|
||
|
||
axis(2,col=par("bg"),col.ticks="{mark_scale_font_color}",col.axis="{mark_scale_font_color}",lwd.ticks=0.5,tck=-0.025,at=z1[5,1],round(z1[5,1]/1000, digits=1),cex.axis={scale_font_size})
|
||
axis(4,col=par("bg"),col.ticks="{mark_scale_font_color}",col.axis="{mark_scale_font_color}",lwd.ticks=0.5,tck=-0.025,at=z1[5,2],round(z1[5,2]/1000, digits=1),cex.axis={scale_font_size})
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(omi=c(0.5,0.5,0.9,0.5),mai=c(0,0.75,0.25,0.75),xpd=T,las=1,col="{font_color}")
|
||
|
||
bumpchart(z1,rank=F,pch=18,top.labels=c("",""),col=myColours,lwd=myLineWidth,mar=c(2,12,1,12),cex=1.1)
|
||
|
||
mtext("{title}",3,line=1.5,adj=0,outer=T,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{caption}",1,line=0,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
|
||
axis(2,col=par("bg"),col.ticks="{scale_font_color}",col.axis="{scale_font_color}",lwd.ticks=0.5,tck=-0.025, at=c(min(z1$r2002), max(z1$r2002)),c(round(min(z1$r2002)/1000,digits=1), round(max(z1$r2002)/1000, digits=1)),cex.axis={scale_font_size})
|
||
axis(4,col=par("bg"),col.ticks="{scale_font_color}",col.axis="{scale_font_color}",lwd.ticks=0.5,tck=-0.025, at=c(min(z1$r2011), max(z1$r2011)),c(round(min(z1$r2011)/1000,digits=1), round(max(z1$r2011)/1000, digits=1)),cex.axis={scale_font_size})
|
||
|
||
mtext("{subtitle}",3,font=3,adj=0,cex={title_font_size}*0.6,line=-0.5,outer=T,col="{title_font_color}")
|
||
|
||
axis(2,col=par("bg"),col.ticks="{mark_scale_font_color}",col.axis="{mark_scale_font_color}",lwd.ticks=0.5,tck=-0.025,at=z1[5,1],round(z1[5,1]/1000, digits=1),cex.axis={scale_font_size})
|
||
axis(4,col=par("bg"),col.ticks="{mark_scale_font_color}",col.axis="{mark_scale_font_color}",lwd.ticks=0.5,tck=-0.025,at=z1[5,2],round(z1[5,2]/1000, digits=1),cex.axis={scale_font_size})
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(omi=c(0.5,0.5,0.9,0.5),mai=c(0,0.75,0.25,0.75),xpd=T,las=1,col="{font_color}")
|
||
|
||
bumpchart(z1,rank=F,pch=18,top.labels=c("",""),col=myColours,lwd=myLineWidth,mar=c(2,12,1,12),cex=1.1)
|
||
|
||
mtext("{title}",3,line=1.5,adj=0,outer=T,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{caption}",1,line=0,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
|
||
axis(2,col=par("bg"),col.ticks="{scale_font_color}",col.axis="{scale_font_color}",lwd.ticks=0.5,tck=-0.025, at=c(min(z1$r2002), max(z1$r2002)),c(round(min(z1$r2002)/1000,digits=1), round(max(z1$r2002)/1000, digits=1)),cex.axis={scale_font_size})
|
||
axis(4,col=par("bg"),col.ticks="{scale_font_color}",col.axis="{scale_font_color}",lwd.ticks=0.5,tck=-0.025, at=c(min(z1$r2011), max(z1$r2011)),c(round(min(z1$r2011)/1000,digits=1), round(max(z1$r2011)/1000, digits=1)),cex.axis={scale_font_size})
|
||
|
||
mtext("{subtitle}",3,font=3,adj=0,cex={title_font_size}*0.6,line=-0.5,outer=T,col="{title_font_color}")
|
||
|
||
axis(2,col=par("bg"),col.ticks="{mark_scale_font_color}",col.axis="{mark_scale_font_color}",lwd.ticks=0.5,tck=-0.025,at=z1[5,1],round(z1[5,1]/1000, digits=1),cex.axis={scale_font_size})
|
||
axis(4,col=par("bg"),col.ticks="{mark_scale_font_color}",col.axis="{mark_scale_font_color}",lwd.ticks=0.5,tck=-0.025,at=z1[5,2],round(z1[5,2]/1000, digits=1),cex.axis={scale_font_size})
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R")
|
||
|
||
|
||
# 基础参数 file_path, char_out_path,out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color, high_color, low_color
|
||
# 示例文件 Column_Chart_with_Percentages_for_Growth_Developments.csv
|
||
|
||
def Column_Chart_for_Developments(file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name,
|
||
x_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size,
|
||
title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color,
|
||
high_color, low_color):
|
||
y_name = "\n".join(y_name)
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
myData <- datas[,2]/10000
|
||
myLabels <- datas[,1]
|
||
myColours<-c(rep("{low_color}",length(myData)-1),"{high_color}")
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(las=1,cex=0.9,omi=c(0.75,0.25,1.25,0.25),mai=c(0.5,0.25,0.5,0.75),las=1)
|
||
barplot(myData,border=NA,col=myColours,names.arg=substr(myLabels,3,4),axes=F,col.axis="{x_scale_font_color}",cex.names={x_scale_font_size})
|
||
abline(h=c(10,20,30,40,50,60,70,80),col=par("bg"),lwd=1.5)
|
||
axis(4,at=c(0,20,40,60),col="{y_scale_font_color}")
|
||
text(11.5,myData[length(myData)]+0.025*myData[length(myData)],format(round(myData[length(myData)]),nsmall=1),adj=0.5,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
|
||
# Titling
|
||
|
||
mtext("{title}",3,line=4,adj=0,outer=T,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=1,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
mtext("{y_name}",4,line=0,adj=0.5,cex={y_font_size},font=3,outer=T,col="{y_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(las=1,cex=0.9,omi=c(0.75,0.25,1.25,0.25),mai=c(0.5,0.25,0.5,0.75),las=1)
|
||
barplot(myData,border=NA,col=myColours,names.arg=substr(myLabels,3,4),axes=F,col.axis="{x_scale_font_color}",cex.names={x_scale_font_size})
|
||
abline(h=c(10,20,30,40,50,60,70,80),col=par("bg"),lwd=1.5)
|
||
axis(4,at=c(0,20,40,60),col="{y_scale_font_color}")
|
||
text(11.5,myData[length(myData)]+0.025*myData[length(myData)],format(round(myData[length(myData)]),nsmall=1),adj=0.5,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
|
||
# Titling
|
||
|
||
mtext("{title}",3,line=4,adj=0,outer=T,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=1,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
mtext("{y_name}",4,line=0,adj=0.5,cex={y_font_size},font=3,outer=T,col="{y_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(las=1,cex=0.9,omi=c(0.75,0.25,1.25,0.25),mai=c(0.5,0.25,0.5,0.75),las=1)
|
||
barplot(myData,border=NA,col=myColours,names.arg=substr(myLabels,3,4),axes=F,col.axis="{x_scale_font_color}",cex.names={x_scale_font_size})
|
||
abline(h=c(10,20,30,40,50,60,70,80),col=par("bg"),lwd=1.5)
|
||
axis(4,at=c(0,20,40,60),col="{y_scale_font_color}")
|
||
text(11.5,myData[length(myData)]+0.025*myData[length(myData)],format(round(myData[length(myData)]),nsmall=1),adj=0.5,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
|
||
# Titling
|
||
|
||
mtext("{title}",3,line=4,adj=0,outer=T,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=1,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
mtext("{y_name}",4,line=0,adj=0.5,cex={y_font_size},font=3,outer=T,col="{y_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(las=1,cex=0.9,omi=c(0.75,0.25,1.25,0.25),mai=c(0.5,0.25,0.5,0.75),las=1)
|
||
barplot(myData,border=NA,col=myColours,names.arg=substr(myLabels,3,4),axes=F,col.axis="{x_scale_font_color}",cex.names={x_scale_font_size})
|
||
abline(h=c(10,20,30,40,50,60,70,80),col=par("bg"),lwd=1.5)
|
||
axis(4,at=c(0,20,40,60),col="{y_scale_font_color}")
|
||
text(11.5,myData[length(myData)]+0.025*myData[length(myData)],format(round(myData[length(myData)]),nsmall=1),adj=0.5,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
|
||
# Titling
|
||
|
||
mtext("{title}",3,line=4,adj=0,outer=T,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=1,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
mtext("{y_name}",4,line=0,adj=0.5,cex={y_font_size},font=3,outer=T,col="{y_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R")
|
||
|
||
|
||
# 基础参数 file_path, char_out_path,out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color, color, high_color, low_color
|
||
# 示例文件 Column_Chart_with_Percentages_for_Growth_Developments.csv
|
||
|
||
def Column_Chart_with_Percentages_for_Growth_Developments(file_path, char_out_path, out_path, title, caption, subtitle,
|
||
title_font_size, x_name, x_font_size, x_scale_font_size,
|
||
y_name, y_font_size,
|
||
y_scale_font_size,
|
||
title_font_color, x_font_color, x_scale_font_color,
|
||
y_font_color, y_scale_font_color,
|
||
color, high_color, low_color):
|
||
code = f"""
|
||
datas <- read.csv("{file_path}")
|
||
|
||
myData <- datas[,2]/10000
|
||
myLabels <- datas[,1]
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(las=1,cex=0.9,omi=c(0.75,0.5,1.25,0.5),mai=c(0.5,1,0,1),las=1)
|
||
|
||
# Define data
|
||
|
||
myGrowth<-0
|
||
for (i in 2:length(myData)) myGrowth<-c(myGrowth,myData[i]-myData[i-1])
|
||
myValueLeft<-myData-myGrowth
|
||
|
||
x<-rbind(t(myData),t(myData))
|
||
y<-rbind(t(myValueLeft),rep(0,length(myData)))
|
||
f1<-"{high_color}"; f2<-"{color}"
|
||
myColours<-c(f1,f2)
|
||
for (i in 1:length(myData)-1) myColours<-c(myColours,f1,f2)
|
||
|
||
for (i in 1:length(myData))
|
||
{{
|
||
if (y[1,i]>x[1,i])
|
||
{{
|
||
tmp<-x[1,i]; x[1,i]<-y[1,i]; y[1,i]<-tmp
|
||
myColours[(2*i)-1]<-"{low_color}"
|
||
}}
|
||
}}
|
||
|
||
# Create chart and other elements
|
||
|
||
barplot(x,beside=T,border=NA,col=myColours,space=c(0,2),axes=T,ylab="{y_name}",cex.lab={y_font_size},col.lab="{y_font_color}",col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size},fg="{y_scale_font_color}")
|
||
barplot(y,beside=T,border=NA,col=rep("{color}",2*length(myData)),add=T,names.arg=myLabels,space=c(0,2),axes=F,col.axis="{x_scale_font_color}",cex.names={x_scale_font_size})
|
||
|
||
hoehe<-0.1*max(myData)
|
||
j<-1
|
||
k<-j
|
||
for (i in 1:length(myData))
|
||
{{
|
||
if (j > 1) k<-k+4
|
||
text(k+1.3,hoehe,format(round(x[2,i]),nsmall=0),cex=1.25,adj=0,xpd=T,col="white")
|
||
j<-j+3
|
||
if (i<length(myData)) text(k+3.1,y[1,i+1]+((x[1,i+1]-y[1,i+1])/2),
|
||
format(round(myGrowth[i+1],1),cex=0.75,nsmall=1),adj=0)
|
||
}}
|
||
|
||
# Titling
|
||
|
||
mtext("{title}",3,line=4,adj=0,outer=T,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=1,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(las=1,cex=0.9,omi=c(0.75,0.5,1.25,0.5),mai=c(0.5,1,0,1),las=1)
|
||
|
||
# Define data
|
||
|
||
myGrowth<-0
|
||
for (i in 2:length(myData)) myGrowth<-c(myGrowth,myData[i]-myData[i-1])
|
||
myValueLeft<-myData-myGrowth
|
||
|
||
x<-rbind(t(myData),t(myData))
|
||
y<-rbind(t(myValueLeft),rep(0,length(myData)))
|
||
f1<-"{high_color}"; f2<-"{color}"
|
||
myColours<-c(f1,f2)
|
||
for (i in 1:length(myData)-1) myColours<-c(myColours,f1,f2)
|
||
|
||
for (i in 1:length(myData))
|
||
{{
|
||
if (y[1,i]>x[1,i])
|
||
{{
|
||
tmp<-x[1,i]; x[1,i]<-y[1,i]; y[1,i]<-tmp
|
||
myColours[(2*i)-1]<-"{low_color}"
|
||
}}
|
||
}}
|
||
|
||
# Create chart and other elements
|
||
|
||
barplot(x,beside=T,border=NA,col=myColours,space=c(0,2),axes=T,ylab="{y_name}",cex.lab={y_font_size},col.lab="{y_font_color}",col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size},fg="{y_scale_font_color}")
|
||
barplot(y,beside=T,border=NA,col=rep("{color}",2*length(myData)),add=T,names.arg=myLabels,space=c(0,2),axes=F,col.axis="{x_scale_font_color}",cex.names={x_scale_font_size})
|
||
|
||
hoehe<-0.1*max(myData)
|
||
j<-1
|
||
k<-j
|
||
for (i in 1:length(myData))
|
||
{{
|
||
if (j > 1) k<-k+4
|
||
text(k+1.3,hoehe,format(round(x[2,i]),nsmall=0),cex=1.25,adj=0,xpd=T,col="white")
|
||
j<-j+3
|
||
if (i<length(myData)) text(k+3.1,y[1,i+1]+((x[1,i+1]-y[1,i+1])/2),
|
||
format(round(myGrowth[i+1],1),cex=0.75,nsmall=1),adj=0)
|
||
}}
|
||
|
||
# Titling
|
||
|
||
mtext("{title}",3,line=4,adj=0,outer=T,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=1,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(las=1,cex=0.9,omi=c(0.75,0.5,1.25,0.5),mai=c(0.5,1,0,1),las=1)
|
||
|
||
# Define data
|
||
|
||
myGrowth<-0
|
||
for (i in 2:length(myData)) myGrowth<-c(myGrowth,myData[i]-myData[i-1])
|
||
myValueLeft<-myData-myGrowth
|
||
|
||
x<-rbind(t(myData),t(myData))
|
||
y<-rbind(t(myValueLeft),rep(0,length(myData)))
|
||
f1<-"{high_color}"; f2<-"{color}"
|
||
myColours<-c(f1,f2)
|
||
for (i in 1:length(myData)-1) myColours<-c(myColours,f1,f2)
|
||
|
||
for (i in 1:length(myData))
|
||
{{
|
||
if (y[1,i]>x[1,i])
|
||
{{
|
||
tmp<-x[1,i]; x[1,i]<-y[1,i]; y[1,i]<-tmp
|
||
myColours[(2*i)-1]<-"{low_color}"
|
||
}}
|
||
}}
|
||
|
||
# Create chart and other elements
|
||
|
||
barplot(x,beside=T,border=NA,col=myColours,space=c(0,2),axes=T,ylab="{y_name}",cex.lab={y_font_size},col.lab="{y_font_color}",col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size},fg="{y_scale_font_color}")
|
||
barplot(y,beside=T,border=NA,col=rep("{color}",2*length(myData)),add=T,names.arg=myLabels,space=c(0,2),axes=F,col.axis="{x_scale_font_color}",cex.names={x_scale_font_size})
|
||
|
||
hoehe<-0.1*max(myData)
|
||
j<-1
|
||
k<-j
|
||
for (i in 1:length(myData))
|
||
{{
|
||
if (j > 1) k<-k+4
|
||
text(k+1.3,hoehe,format(round(x[2,i]),nsmall=0),cex=1.25,adj=0,xpd=T,col="white")
|
||
j<-j+3
|
||
if (i<length(myData)) text(k+3.1,y[1,i+1]+((x[1,i+1]-y[1,i+1])/2),
|
||
format(round(myGrowth[i+1],1),cex=0.75,nsmall=1),adj=0)
|
||
}}
|
||
|
||
# Titling
|
||
|
||
mtext("{title}",3,line=4,adj=0,outer=T,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=1,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(las=1,cex=0.9,omi=c(0.75,0.5,1.25,0.5),mai=c(0.5,1,0,1),las=1)
|
||
|
||
# Define data
|
||
|
||
myGrowth<-0
|
||
for (i in 2:length(myData)) myGrowth<-c(myGrowth,myData[i]-myData[i-1])
|
||
myValueLeft<-myData-myGrowth
|
||
|
||
x<-rbind(t(myData),t(myData))
|
||
y<-rbind(t(myValueLeft),rep(0,length(myData)))
|
||
f1<-"{high_color}"; f2<-"{color}"
|
||
myColours<-c(f1,f2)
|
||
for (i in 1:length(myData)-1) myColours<-c(myColours,f1,f2)
|
||
|
||
for (i in 1:length(myData))
|
||
{{
|
||
if (y[1,i]>x[1,i])
|
||
{{
|
||
tmp<-x[1,i]; x[1,i]<-y[1,i]; y[1,i]<-tmp
|
||
myColours[(2*i)-1]<-"{low_color}"
|
||
}}
|
||
}}
|
||
|
||
# Create chart and other elements
|
||
|
||
barplot(x,beside=T,border=NA,col=myColours,space=c(0,2),axes=T,ylab="{y_name}",cex.lab={y_font_size},col.lab="{y_font_color}",col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size},fg="{y_scale_font_color}")
|
||
barplot(y,beside=T,border=NA,col=rep("{color}",2*length(myData)),add=T,names.arg=myLabels,space=c(0,2),axes=F,col.axis="{x_scale_font_color}",cex.names={x_scale_font_size})
|
||
|
||
hoehe<-0.1*max(myData)
|
||
j<-1
|
||
k<-j
|
||
for (i in 1:length(myData))
|
||
{{
|
||
if (j > 1) k<-k+4
|
||
text(k+1.3,hoehe,format(round(x[2,i]),nsmall=0),cex=1.25,adj=0,xpd=T,col="white")
|
||
j<-j+3
|
||
if (i<length(myData)) text(k+3.1,y[1,i+1]+((x[1,i+1]-y[1,i+1])/2),
|
||
format(round(myGrowth[i+1],1),cex=0.75,nsmall=1),adj=0)
|
||
}}
|
||
|
||
# Titling
|
||
|
||
mtext("{title}",3,line=4,adj=0,outer=T,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=1,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R")
|
||
|
||
|
||
# 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_scale_font_size, ruler_name, ruler_name_font_size, ruler_number_font_size, max_number_size
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, max_number_font_color, y_scale_font_color, color_1, color_2, zero_graduation_color, ruler_color
|
||
# 示例文件 Comparison_with_Bar_Chart_1.xlsx
|
||
|
||
def Comparison_with_Bar_Chart_1(file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name,
|
||
x_font_size, x_scale_font_size, y_scale_font_size, ruler_name, ruler_name_font_size,
|
||
ruler_number_font_size,
|
||
max_number_size,
|
||
title_font_color, x_font_color, x_scale_font_color, max_number_font_color,
|
||
y_scale_font_color, color_1, color_2,
|
||
zero_graduation_color, ruler_color):
|
||
code = f"""
|
||
library(fBasics)
|
||
library(gdata)
|
||
myData<-read.xls("{file_path}",head=T,skip=1,dec=".",encoding="latin1")
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(omi=c(0.5,0.5,1.1,0.5),mai=c(0,2,0,0.5),las=1)
|
||
layout(matrix(c(1,2),ncol=1),heights=c(80,20))
|
||
par(mai=c(0,1.75,1,0))
|
||
bp1<-barplot(as.matrix(myData),ylim=c(0,6),width=c(0.5),axes=F,horiz=T,col=c("{color_1}",seqPalette(5,"{color_2}")[2:5]),border=par("bg"),names.arg=gsub("."," ",names(myData),fixed=T),cex.names={y_scale_font_size},col.axis="{y_scale_font_color}")
|
||
|
||
mtext(seq(0,100,by=20),at=seq(0,100,by=20),1,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
arrows(0,-0.03,0,7.30,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(100-(myData[5,]/2),bp1,cex={max_number_size},labels=paste(round(myData[5,],digits=0),"%",sep=" "),col="{max_number_font_color}",xpd=T)
|
||
|
||
par(mai=c(0.55,1.75,0,0))
|
||
bp2<-barplot(as.matrix(rep(20,5)),ylim=c(0,0.5),width=c(0.20),horiz=T,col=seqPalette(5,"{ruler_color}"),border=par("bg"),names.arg=c("{ruler_name}"),axes=F,cex.names={ruler_name_font_size})
|
||
|
||
arrows(0,-0.01,0,0.35,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(c(10,30,50,70,90),bp2,labels=c("20 %","20 %","20 %","20 %","20 %"),col=c("black","black","white","white","white"),xpd=T,cex={ruler_number_font_size})
|
||
|
||
title(main="{title}",line=3,adj=0,cex.main={title_font_size},outer=T,col.main="{title_font_color}")
|
||
myBreak<-strsplit( strwrap("{subtitle}",width=110),"\n")
|
||
mtext("{caption}",side=1,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",side=1,line=-2.0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
for(i in seq(along=myBreak))
|
||
{{
|
||
mtext(myBreak[[i]],line=(1.8-i)*1.5,adj=0,side=3,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
}}
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(omi=c(0.5,0.5,1.1,0.5),mai=c(0,2,0,0.5),las=1)
|
||
layout(matrix(c(1,2),ncol=1),heights=c(80,20))
|
||
par(mai=c(0,1.75,1,0))
|
||
bp1<-barplot(as.matrix(myData),ylim=c(0,6),width=c(0.5),axes=F,horiz=T,col=c("{color_1}",seqPalette(5,"{color_2}")[2:5]),border=par("bg"),names.arg=gsub("."," ",names(myData),fixed=T),cex.names={y_scale_font_size},col.axis="{y_scale_font_color}")
|
||
|
||
mtext(seq(0,100,by=20),at=seq(0,100,by=20),1,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
arrows(0,-0.03,0,7.30,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(100-(myData[5,]/2),bp1,cex={max_number_size},labels=paste(round(myData[5,],digits=0),"%",sep=" "),col="{max_number_font_color}",xpd=T)
|
||
|
||
par(mai=c(0.55,1.75,0,0))
|
||
bp2<-barplot(as.matrix(rep(20,5)),ylim=c(0,0.5),width=c(0.20),horiz=T,col=seqPalette(5,"{ruler_color}"),border=par("bg"),names.arg=c("{ruler_name}"),axes=F,cex.names={ruler_name_font_size})
|
||
|
||
arrows(0,-0.01,0,0.35,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(c(10,30,50,70,90),bp2,labels=c("20 %","20 %","20 %","20 %","20 %"),col=c("black","black","white","white","white"),xpd=T,cex={ruler_number_font_size})
|
||
|
||
title(main="{title}",line=3,adj=0,cex.main={title_font_size},outer=T,col.main="{title_font_color}")
|
||
myBreak<-strsplit( strwrap("{subtitle}",width=110),"\n")
|
||
mtext("{caption}",side=1,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",side=1,line=-2.0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
for(i in seq(along=myBreak))
|
||
{{
|
||
mtext(myBreak[[i]],line=(1.8-i)*1.5,adj=0,side=3,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
}}
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=408)
|
||
par(omi=c(0.5,0.5,1.1,0.5),mai=c(0,2,0,0.5),las=1)
|
||
layout(matrix(c(1,2),ncol=1),heights=c(80,20))
|
||
par(mai=c(0,1.75,1,0))
|
||
bp1<-barplot(as.matrix(myData),ylim=c(0,6),width=c(0.5),axes=F,horiz=T,col=c("{color_1}",seqPalette(5,"{color_2}")[2:5]),border=par("bg"),names.arg=gsub("."," ",names(myData),fixed=T),cex.names={y_scale_font_size},col.axis="{y_scale_font_color}")
|
||
|
||
mtext(seq(0,100,by=20),at=seq(0,100,by=20),1,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
arrows(0,-0.03,0,7.30,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(100-(myData[5,]/2),bp1,cex={max_number_size},labels=paste(round(myData[5,],digits=0),"%",sep=" "),col="{max_number_font_color}",xpd=T)
|
||
|
||
par(mai=c(0.55,1.75,0,0))
|
||
bp2<-barplot(as.matrix(rep(20,5)),ylim=c(0,0.5),width=c(0.20),horiz=T,col=seqPalette(5,"{ruler_color}"),border=par("bg"),names.arg=c("{ruler_name}"),axes=F,cex.names={ruler_name_font_size})
|
||
|
||
arrows(0,-0.01,0,0.35,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(c(10,30,50,70,90),bp2,labels=c("20 %","20 %","20 %","20 %","20 %"),col=c("black","black","white","white","white"),xpd=T,cex={ruler_number_font_size})
|
||
|
||
title(main="{title}",line=3,adj=0,cex.main={title_font_size},outer=T,col.main="{title_font_color}")
|
||
myBreak<-strsplit( strwrap("{subtitle}",width=110),"\n")
|
||
mtext("{caption}",side=1,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",side=1,line=-2.0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
for(i in seq(along=myBreak))
|
||
{{
|
||
mtext(myBreak[[i]],line=(1.8-i)*1.5,adj=0,side=3,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
}}
|
||
dev.off()
|
||
|
||
|
||
svg("{out_path}.svg")
|
||
par(omi=c(0.5,0.5,1.1,0.5),mai=c(0,2,0,0.5),las=1)
|
||
layout(matrix(c(1,2),ncol=1),heights=c(80,20))
|
||
par(mai=c(0,1.75,1,0))
|
||
bp1<-barplot(as.matrix(myData),ylim=c(0,6),width=c(0.5),axes=F,horiz=T,col=c("{color_1}",seqPalette(5,"{color_2}")[2:5]),border=par("bg"),names.arg=gsub("."," ",names(myData),fixed=T),cex.names={y_scale_font_size},col.axis="{y_scale_font_color}")
|
||
|
||
mtext(seq(0,100,by=20),at=seq(0,100,by=20),1,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
arrows(0,-0.03,0,7.30,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(100-(myData[5,]/2),bp1,cex={max_number_size},labels=paste(round(myData[5,],digits=0),"%",sep=" "),col="{max_number_font_color}",xpd=T)
|
||
|
||
par(mai=c(0.55,1.75,0,0))
|
||
bp2<-barplot(as.matrix(rep(20,5)),ylim=c(0,0.5),width=c(0.20),horiz=T,col=seqPalette(5,"{ruler_color}"),border=par("bg"),names.arg=c("{ruler_name}"),axes=F,cex.names={ruler_name_font_size})
|
||
|
||
arrows(0,-0.01,0,0.35,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(c(10,30,50,70,90),bp2,labels=c("20 %","20 %","20 %","20 %","20 %"),col=c("black","black","white","white","white"),xpd=T,cex={ruler_number_font_size})
|
||
|
||
title(main="{title}",line=3,adj=0,cex.main={title_font_size},outer=T,col.main="{title_font_color}")
|
||
myBreak<-strsplit( strwrap("{subtitle}",width=110),"\n")
|
||
mtext("{caption}",side=1,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",side=1,line=-2.0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
for(i in seq(along=myBreak))
|
||
{{
|
||
mtext(myBreak[[i]],line=(1.8-i)*1.5,adj=0,side=3,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
}}
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_scale_font_size, ruler_name, max_number_size, ruler_name_font_size, ruler_number_font_size, country_1, country_2, country_3, country_1_time_1, country_1_time_2, country_2_time_1, country_2_time_2, country_3_time_1, country_3_time_2
|
||
|
||
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, ruler_color, y_scale_font_color, zero_graduation_color, color_1, color_2, max_number_font_color, ruler_number_font_color
|
||
# 示例文件 Comparison_with_Bar_Chart_2.xlsx
|
||
|
||
def Comparison_with_Bar_Chart_2(file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name,
|
||
x_font_size, x_scale_font_size, y_scale_font_size, ruler_name, max_number_size,
|
||
ruler_name_font_size,
|
||
ruler_number_font_size, country_1, country_2, country_3, country_1_time_1,
|
||
country_1_time_2,
|
||
country_2_time_1, country_2_time_2, country_3_time_1, country_3_time_2,
|
||
title_font_color, x_font_color, x_scale_font_color, ruler_color, y_scale_font_color,
|
||
zero_graduation_color,
|
||
color_1, color_2, max_number_font_color, ruler_number_font_color):
|
||
code = f"""
|
||
library(fBasics)
|
||
library(gdata)
|
||
myData<-read.xls("{file_path}",head=T,skip=1,dec=".",encoding="latin1")
|
||
|
||
pdf("{out_path}.pdf")
|
||
layout(matrix(c(1,2,1,2),2,2),heights=c(6,1))
|
||
par(omi=c(1,0.5,1.25,0.25),mai=c(0,2.65,0.75,0.25),cex=1.5,las=1)
|
||
|
||
bp1<-barplot(as.matrix(myData),ylim=c(0,3),width=c(0.45),axes=F,horiz=T,col=c("{color_1}",seqPalette(10,"{color_2}")[2:10]),border=par("bg"),names.arg=c("{country_1_time_2}","{country_1_time_1}","{country_2_time_2}","{country_2_time_1}","{country_3_time_2}","{country_3_time_1}"),col.axis="{y_scale_font_color}",cex.names={y_scale_font_size})
|
||
arrows(0,-0.01,0,3.25,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(100-(myData[10,]/2),bp1,col="{max_number_font_color}",cex={max_number_size},labels=paste(round(myData[10,],digits=0),"%",sep=" "),xpd=T)
|
||
text(-15,bp1[2],"{country_3}",adj=1,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
text(-15,bp1[4],"{country_2}",adj=1,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
text(-15,bp1[6],"{country_1}",adj=1,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
|
||
par(mai=c(0,2.65,0.1,0.25))
|
||
bp2<-barplot(as.matrix(rep(10,10)),ylim=c(0,0.5),width=c(0.25),axes=F,horiz=T,col=seqPalette(10,"{ruler_color}"),border=par("bg"),names.arg=c("{ruler_name}"),cex.names={ruler_name_font_size})
|
||
arrows(0,-0.01,0,0.35,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(95,bp2,labels="10 %",col="{ruler_number_font_color}",xpd=T,cex={ruler_number_font_size})
|
||
mtext(seq(0,100,by=20),at=seq(0,100,by=20),3,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
|
||
mtext("{title}",line=2,adj=0,cex.main={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{caption}",side=1,line=1.5,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",side=1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
myBreak<-strsplit( strwrap("{subtitle}",width=110),"\n")
|
||
for(i in seq(along=myBreak))
|
||
{{
|
||
mtext(myBreak[[i]],line=(1.8-i)*1.5,adj=0,side=3,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
}}
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
layout(matrix(c(1,2,1,2),2,2),heights=c(6,1))
|
||
par(omi=c(1,0.5,1.25,0.25),mai=c(0,2.65,0.75,0.25),cex=1.5,las=1)
|
||
|
||
bp1<-barplot(as.matrix(myData),ylim=c(0,3),width=c(0.45),axes=F,horiz=T,col=c("{color_1}",seqPalette(10,"{color_2}")[2:10]),border=par("bg"),names.arg=c("{country_1_time_2}","{country_1_time_1}","{country_2_time_2}","{country_2_time_1}","{country_3_time_2}","{country_3_time_1}"),col.axis="{y_scale_font_color}",cex.names={y_scale_font_size})
|
||
arrows(0,-0.01,0,3.25,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(100-(myData[10,]/2),bp1,col="{max_number_font_color}",cex={max_number_size},labels=paste(round(myData[10,],digits=0),"%",sep=" "),xpd=T)
|
||
text(-15,bp1[2],"{country_3}",adj=1,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
text(-15,bp1[4],"{country_2}",adj=1,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
text(-15,bp1[6],"{country_1}",adj=1,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
|
||
par(mai=c(0,2.65,0.1,0.25))
|
||
bp2<-barplot(as.matrix(rep(10,10)),ylim=c(0,0.5),width=c(0.25),axes=F,horiz=T,col=seqPalette(10,"{ruler_color}"),border=par("bg"),names.arg=c("{ruler_name}"),cex.names={ruler_name_font_size})
|
||
arrows(0,-0.01,0,0.35,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(95,bp2,labels="10 %",col="{ruler_number_font_color}",xpd=T,cex={ruler_number_font_size})
|
||
mtext(seq(0,100,by=20),at=seq(0,100,by=20),3,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
|
||
mtext("{title}",line=2,adj=0,cex.main={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{caption}",side=1,line=1.5,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",side=1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
myBreak<-strsplit( strwrap("{subtitle}",width=110),"\n")
|
||
for(i in seq(along=myBreak))
|
||
{{
|
||
mtext(myBreak[[i]],line=(1.8-i)*1.5,adj=0,side=3,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
}}
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
layout(matrix(c(1,2,1,2),2,2),heights=c(6,1))
|
||
par(omi=c(1,0.5,1.25,0.25),mai=c(0,2.65,0.75,0.25),cex=1.5,las=1)
|
||
|
||
bp1<-barplot(as.matrix(myData),ylim=c(0,3),width=c(0.45),axes=F,horiz=T,col=c("{color_1}",seqPalette(10,"{color_2}")[2:10]),border=par("bg"),names.arg=c("{country_1_time_2}","{country_1_time_1}","{country_2_time_2}","{country_2_time_1}","{country_3_time_2}","{country_3_time_1}"),col.axis="{y_scale_font_color}",cex.names={y_scale_font_size})
|
||
arrows(0,-0.01,0,3.25,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(100-(myData[10,]/2),bp1,col="{max_number_font_color}",cex={max_number_size},labels=paste(round(myData[10,],digits=0),"%",sep=" "),xpd=T)
|
||
text(-15,bp1[2],"{country_3}",adj=1,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
text(-15,bp1[4],"{country_2}",adj=1,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
text(-15,bp1[6],"{country_1}",adj=1,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
|
||
par(mai=c(0,2.65,0.1,0.25))
|
||
bp2<-barplot(as.matrix(rep(10,10)),ylim=c(0,0.5),width=c(0.25),axes=F,horiz=T,col=seqPalette(10,"{ruler_color}"),border=par("bg"),names.arg=c("{ruler_name}"),cex.names={ruler_name_font_size})
|
||
arrows(0,-0.01,0,0.35,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(95,bp2,labels="10 %",col="{ruler_number_font_color}",xpd=T,cex={ruler_number_font_size})
|
||
mtext(seq(0,100,by=20),at=seq(0,100,by=20),3,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
|
||
mtext("{title}",line=2,adj=0,cex.main={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{caption}",side=1,line=1.5,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",side=1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
myBreak<-strsplit( strwrap("{subtitle}",width=110),"\n")
|
||
for(i in seq(along=myBreak))
|
||
{{
|
||
mtext(myBreak[[i]],line=(1.8-i)*1.5,adj=0,side=3,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
}}
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
layout(matrix(c(1,2,1,2),2,2),heights=c(6,1))
|
||
par(omi=c(1,0.5,1.25,0.25),mai=c(0,2.65,0.75,0.25),cex=1.5,las=1)
|
||
|
||
bp1<-barplot(as.matrix(myData),ylim=c(0,3),width=c(0.45),axes=F,horiz=T,col=c("{color_1}",seqPalette(10,"{color_2}")[2:10]),border=par("bg"),names.arg=c("{country_1_time_2}","{country_1_time_1}","{country_2_time_2}","{country_2_time_1}","{country_3_time_2}","{country_3_time_1}"),col.axis="{y_scale_font_color}",cex.names={y_scale_font_size})
|
||
arrows(0,-0.01,0,3.25,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(100-(myData[10,]/2),bp1,col="{max_number_font_color}",cex={max_number_size},labels=paste(round(myData[10,],digits=0),"%",sep=" "),xpd=T)
|
||
text(-15,bp1[2],"{country_3}",adj=1,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
text(-15,bp1[4],"{country_2}",adj=1,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
text(-15,bp1[6],"{country_1}",adj=1,xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})
|
||
|
||
par(mai=c(0,2.65,0.1,0.25))
|
||
bp2<-barplot(as.matrix(rep(10,10)),ylim=c(0,0.5),width=c(0.25),axes=F,horiz=T,col=seqPalette(10,"{ruler_color}"),border=par("bg"),names.arg=c("{ruler_name}"),cex.names={ruler_name_font_size})
|
||
arrows(0,-0.01,0,0.35,lwd=1.5,length=0,xpd=T,col="{zero_graduation_color}")
|
||
text(95,bp2,labels="10 %",col="{ruler_number_font_color}",xpd=T,cex={ruler_number_font_size})
|
||
mtext(seq(0,100,by=20),at=seq(0,100,by=20),3,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
|
||
mtext("{title}",line=2,adj=0,cex.main={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{caption}",side=1,line=1.5,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",side=1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
myBreak<-strsplit( strwrap("{subtitle}",width=110),"\n")
|
||
for(i in seq(along=myBreak))
|
||
{{
|
||
mtext(myBreak[[i]],line=(1.8-i)*1.5,adj=0,side=3,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
}}
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R")
|
||
|
||
|
||
# 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_scale_font_size, y_scale_font_size, numbers_font_size
|
||
# 高级参数 title_font_color, x_scale_font_color, y_scale_font_color, max_number_font_color, line_color, color, number_font_color
|
||
# 示例文件 Comparison_with_Bar_Chart_3.xlsx
|
||
|
||
def Comparison_with_Bar_Chart_3(file_path, char_out_path, out_path, title, caption, subtitle, title_font_size,
|
||
x_scale_font_size,
|
||
y_scale_font_size, numbers_font_size,
|
||
title_font_color, x_scale_font_color, y_scale_font_color, max_number_font_color,
|
||
line_color, color,
|
||
number_font_color):
|
||
color = color[4:-1]
|
||
|
||
code = f"""
|
||
library(gdata)
|
||
myData<-read.xls("{file_path}",skip=1,dec=".", encoding="latin1")
|
||
tmyData<-t(myData)
|
||
transparency<-c(0,50,100,150,200)
|
||
number_colour<-c("{number_font_color}","{number_font_color}","{number_font_color}","{number_font_color}","{max_number_font_color}")
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(omi=c(0.5,0.5,1.1,0.5),las=1)
|
||
layout(matrix(data=c(1,2,3,4,5),nrow=1,ncol=5),widths=c(2.0,1,1,1,1),heights=c(1,1))
|
||
pos<-c(45,45,45,45,35)
|
||
par(cex=1.05)
|
||
|
||
for (i in 1:5) {{
|
||
if (i == 1)
|
||
{{
|
||
par(mai=c(0.25,1.75,0.25,0.15))
|
||
bp1<-barplot(tmyData[ ,i],horiz=T,cex.names={y_scale_font_size},col.axis="{y_scale_font_color}",axes=F,names.arg=gsub("."," ",names(myData),fixed=T),xlim=c(0,60),col=rgb(43,15,52,0,maxColorValue=255))
|
||
}} else
|
||
{{
|
||
par(mai=c(0.25,0.1,0.25,0.15))
|
||
bp2<-barplot(tmyData[ ,i],horiz=T,axisnames=F,axes=F,xlim=c(0,60),col=rgb({color},transparency[i],maxColorValue=255),border=par("bg"))
|
||
}}
|
||
text(pos[i],bp1,adj=1,labels=paste(round(myData[i ,],digits=0),"%",sep=" "),col=number_colour[i],xpd=T,cex={numbers_font_size})
|
||
mtext(seq(0,60,by=15),at=seq(0,60,by=15),1,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
arrows(0,-0.1,0,14.6,lwd=2.5,length=0,xpd=T,col="{line_color}")
|
||
}}
|
||
title(main="{title}",line=3,adj=0,cex.main={title_font_size},outer=T,col.main="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1,font=3,cex={title_font_size}*0.4,col="{title_font_color}")
|
||
myBreak<-strsplit( strwrap("{subtitle}",width=110),"\n")
|
||
for(i in seq(along=myBreak))
|
||
{{
|
||
mtext(myBreak[[i]],line=(1.8-i)*1.7,adj=0,side=3,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
}}
|
||
dev.off()
|
||
|
||
png("{out_path}.png",width=900,height=540)
|
||
par(omi=c(0.5,0.5,1.1,0.5),las=1)
|
||
layout(matrix(data=c(1,2,3,4,5),nrow=1,ncol=5),widths=c(2.0,1,1,1,1),heights=c(1,1))
|
||
pos<-c(45,45,45,45,35)
|
||
par(cex=1.05)
|
||
|
||
for (i in 1:5) {{
|
||
if (i == 1)
|
||
{{
|
||
par(mai=c(0.25,1.75,0.25,0.15))
|
||
bp1<-barplot(tmyData[ ,i],horiz=T,cex.names={y_scale_font_size},col.axis="{y_scale_font_color}",axes=F,names.arg=gsub("."," ",names(myData),fixed=T),xlim=c(0,60),col=rgb(43,15,52,0,maxColorValue=255))
|
||
}} else
|
||
{{
|
||
par(mai=c(0.25,0.1,0.25,0.15))
|
||
bp2<-barplot(tmyData[ ,i],horiz=T,axisnames=F,axes=F,xlim=c(0,60),col=rgb({color},transparency[i],maxColorValue=255),border=par("bg"))
|
||
}}
|
||
text(pos[i],bp1,adj=1,labels=paste(round(myData[i ,],digits=0),"%",sep=" "),col=number_colour[i],xpd=T,cex={numbers_font_size})
|
||
mtext(seq(0,60,by=15),at=seq(0,60,by=15),1,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
arrows(0,-0.1,0,14.6,lwd=2.5,length=0,xpd=T,col="{line_color}")
|
||
}}
|
||
title(main="{title}",line=3,adj=0,cex.main={title_font_size},outer=T,col.main="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1,font=3,cex={title_font_size}*0.4,col="{title_font_color}")
|
||
myBreak<-strsplit( strwrap("{subtitle}",width=110),"\n")
|
||
for(i in seq(along=myBreak))
|
||
{{
|
||
mtext(myBreak[[i]],line=(1.8-i)*1.7,adj=0,side=3,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
}}
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(omi=c(0.5,0.5,1.1,0.5),las=1)
|
||
layout(matrix(data=c(1,2,3,4,5),nrow=1,ncol=5),widths=c(2.0,1,1,1,1),heights=c(1,1))
|
||
pos<-c(45,45,45,45,35)
|
||
par(cex=1.05)
|
||
|
||
for (i in 1:5) {{
|
||
if (i == 1)
|
||
{{
|
||
par(mai=c(0.25,1.75,0.25,0.15))
|
||
bp1<-barplot(tmyData[ ,i],horiz=T,cex.names={y_scale_font_size},col.axis="{y_scale_font_color}",axes=F,names.arg=gsub("."," ",names(myData),fixed=T),xlim=c(0,60),col=rgb(43,15,52,0,maxColorValue=255))
|
||
}} else
|
||
{{
|
||
par(mai=c(0.25,0.1,0.25,0.15))
|
||
bp2<-barplot(tmyData[ ,i],horiz=T,axisnames=F,axes=F,xlim=c(0,60),col=rgb({color},transparency[i],maxColorValue=255),border=par("bg"))
|
||
}}
|
||
text(pos[i],bp1,adj=1,labels=paste(round(myData[i ,],digits=0),"%",sep=" "),col=number_colour[i],xpd=T,cex={numbers_font_size})
|
||
mtext(seq(0,60,by=15),at=seq(0,60,by=15),1,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
arrows(0,-0.1,0,14.6,lwd=2.5,length=0,xpd=T,col="{line_color}")
|
||
}}
|
||
title(main="{title}",line=3,adj=0,cex.main={title_font_size},outer=T,col.main="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1,font=3,cex={title_font_size}*0.4,col="{title_font_color}")
|
||
myBreak<-strsplit( strwrap("{subtitle}",width=110),"\n")
|
||
for(i in seq(along=myBreak))
|
||
{{
|
||
mtext(myBreak[[i]],line=(1.8-i)*1.7,adj=0,side=3,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
}}
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(omi=c(0.5,0.5,1.1,0.5),las=1)
|
||
layout(matrix(data=c(1,2,3,4,5),nrow=1,ncol=5),widths=c(2.0,1,1,1,1),heights=c(1,1))
|
||
pos<-c(45,45,45,45,35)
|
||
par(cex=1.05)
|
||
|
||
for (i in 1:5) {{
|
||
if (i == 1)
|
||
{{
|
||
par(mai=c(0.25,1.75,0.25,0.15))
|
||
bp1<-barplot(tmyData[ ,i],horiz=T,cex.names={y_scale_font_size},col.axis="{y_scale_font_color}",axes=F,names.arg=gsub("."," ",names(myData),fixed=T),xlim=c(0,60),col=rgb(43,15,52,0,maxColorValue=255))
|
||
}} else
|
||
{{
|
||
par(mai=c(0.25,0.1,0.25,0.15))
|
||
bp2<-barplot(tmyData[ ,i],horiz=T,axisnames=F,axes=F,xlim=c(0,60),col=rgb({color},transparency[i],maxColorValue=255),border=par("bg"))
|
||
}}
|
||
text(pos[i],bp1,adj=1,labels=paste(round(myData[i ,],digits=0),"%",sep=" "),col=number_colour[i],xpd=T,cex={numbers_font_size})
|
||
mtext(seq(0,60,by=15),at=seq(0,60,by=15),1,line=0,cex={x_scale_font_size},col="{x_scale_font_color}")
|
||
arrows(0,-0.1,0,14.6,lwd=2.5,length=0,xpd=T,col="{line_color}")
|
||
}}
|
||
title(main="{title}",line=3,adj=0,cex.main={title_font_size},outer=T,col.main="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1,font=3,cex={title_font_size}*0.4,col="{title_font_color}")
|
||
myBreak<-strsplit( strwrap("{subtitle}",width=110),"\n")
|
||
for(i in seq(along=myBreak))
|
||
{{
|
||
mtext(myBreak[[i]],line=(1.8-i)*1.7,adj=0,side=3,cex={title_font_size}*0.6,outer=T,col="{title_font_color}")
|
||
}}
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R")
|
||
|
||
|
||
# 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size, time_1, time_1_font_size, time_2, time_2_font_size
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color, time_color, line_color, color
|
||
# 示例文件 Daily_Values_with_Labels.a
|
||
|
||
def Daily_Values_with_Labels(file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name,
|
||
x_font_size,
|
||
x_scale_font_size, y_name, y_font_size, y_scale_font_size, time_1, time_1_font_size,
|
||
time_2, time_2_font_size,
|
||
title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color,
|
||
time_color, line_color, color):
|
||
color = color[4:-1]
|
||
|
||
time_color = time_color[4:-1]
|
||
|
||
code = f"""
|
||
christmas <- read.csv(file="{file_path}",head=F,sep="\t",dec=".")
|
||
|
||
attach(christmas)
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(cex.axis=1.1,omi=c(1,0.5,0.95,0.5),mai=c(0.1,1.25,0.1,0.2),mgp=c(5,1,0),las=1)
|
||
|
||
plot(axes=F,type="n",xlab="",ylab="{y_name}",V1,V2,col.lab="{y_font_color}",cex.lab={y_font_size})
|
||
|
||
axis(1,tck=-0.01,col="{x_scale_font_color}",cex.axis={x_scale_font_size},at=V1[c(1,length(V1))],labels=c("1 July","30 June"),col.axis="{x_scale_font_color}")
|
||
axis(2,at=py<-pretty(V2),labels=format(py,big.mark=","),cex.axis={y_scale_font_size},col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}")
|
||
points(V1,V2,type="l",col="{line_color}")
|
||
points(lowess(V2,f=1/5),type="l",lwd=25,col=rgb({color},70,maxColorValue=255))
|
||
text(123,V2[179],"{time_1}",cex={time_1_font_size},col=rgb({time_color},100,maxColorValue=255))
|
||
arrows(157,V2[179],172,V2[179],length=0.10,angle=10,code=0,lwd=2,col=rgb({time_color},100,maxColorValue=255))
|
||
arrows(192,V2[185],220,V2[185],length=0.10,angle=10,code=0,lwd=2,col=rgb({time_color},100,maxColorValue=255))
|
||
text(240,V2[185],"{time_2}",cex={time_2_font_size},col=rgb({time_color},100,maxColorValue=255))
|
||
|
||
mtext("{title}",3,line=1.5,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-0.2,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=3,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(cex.axis=1.1,omi=c(1,0.5,0.95,0.5),mai=c(0.1,1.25,0.1,0.2),mgp=c(5,1,0),las=1)
|
||
|
||
plot(axes=F,type="n",xlab="",ylab="{y_name}",V1,V2,col.lab="{y_font_color}",cex.lab={y_font_size})
|
||
|
||
axis(1,tck=-0.01,col="{x_scale_font_color}",cex.axis={x_scale_font_size},at=V1[c(1,length(V1))],labels=c("1 July","30 June"),col.axis="{x_scale_font_color}")
|
||
axis(2,at=py<-pretty(V2),labels=format(py,big.mark=","),cex.axis={y_scale_font_size},col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}")
|
||
points(V1,V2,type="l",col="{line_color}")
|
||
points(lowess(V2,f=1/5),type="l",lwd=25,col=rgb({color},70,maxColorValue=255))
|
||
text(123,V2[179],"{time_1}",cex={time_1_font_size},col=rgb({time_color},100,maxColorValue=255))
|
||
arrows(157,V2[179],172,V2[179],length=0.10,angle=10,code=0,lwd=2,col=rgb({time_color},100,maxColorValue=255))
|
||
arrows(192,V2[185],220,V2[185],length=0.10,angle=10,code=0,lwd=2,col=rgb({time_color},100,maxColorValue=255))
|
||
text(240,V2[185],"{time_2}",cex={time_2_font_size},col=rgb({time_color},100,maxColorValue=255))
|
||
|
||
mtext("{title}",3,line=1.5,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-0.2,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=3,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(cex.axis=1.1,omi=c(1,0.5,0.95,0.5),mai=c(0.1,1.25,0.1,0.2),mgp=c(5,1,0),las=1)
|
||
|
||
plot(axes=F,type="n",xlab="",ylab="{y_name}",V1,V2,col.lab="{y_font_color}",cex.lab={y_font_size})
|
||
|
||
axis(1,tck=-0.01,col="{x_scale_font_color}",cex.axis={x_scale_font_size},at=V1[c(1,length(V1))],labels=c("1 July","30 June"),col.axis="{x_scale_font_color}")
|
||
axis(2,at=py<-pretty(V2),labels=format(py,big.mark=","),cex.axis={y_scale_font_size},col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}")
|
||
points(V1,V2,type="l",col="{line_color}")
|
||
points(lowess(V2,f=1/5),type="l",lwd=25,col=rgb({color},70,maxColorValue=255))
|
||
text(123,V2[179],"{time_1}",cex={time_1_font_size},col=rgb({time_color},100,maxColorValue=255))
|
||
arrows(157,V2[179],172,V2[179],length=0.10,angle=10,code=0,lwd=2,col=rgb({time_color},100,maxColorValue=255))
|
||
arrows(192,V2[185],220,V2[185],length=0.10,angle=10,code=0,lwd=2,col=rgb({time_color},100,maxColorValue=255))
|
||
text(240,V2[185],"{time_2}",cex={time_2_font_size},col=rgb({time_color},100,maxColorValue=255))
|
||
|
||
mtext("{title}",3,line=1.5,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-0.2,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=3,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(cex.axis=1.1,omi=c(1,0.5,0.95,0.5),mai=c(0.1,1.25,0.1,0.2),mgp=c(5,1,0),las=1)
|
||
|
||
plot(axes=F,type="n",xlab="",ylab="{y_name}",V1,V2,col.lab="{y_font_color}",cex.lab={y_font_size})
|
||
|
||
axis(1,tck=-0.01,col="{x_scale_font_color}",cex.axis={x_scale_font_size},at=V1[c(1,length(V1))],labels=c("1 July","30 June"),col.axis="{x_scale_font_color}")
|
||
axis(2,at=py<-pretty(V2),labels=format(py,big.mark=","),cex.axis={y_scale_font_size},col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}")
|
||
points(V1,V2,type="l",col="{line_color}")
|
||
points(lowess(V2,f=1/5),type="l",lwd=25,col=rgb({color},70,maxColorValue=255))
|
||
text(123,V2[179],"{time_1}",cex={time_1_font_size},col=rgb({time_color},100,maxColorValue=255))
|
||
arrows(157,V2[179],172,V2[179],length=0.10,angle=10,code=0,lwd=2,col=rgb({time_color},100,maxColorValue=255))
|
||
arrows(192,V2[185],220,V2[185],length=0.10,angle=10,code=0,lwd=2,col=rgb({time_color},100,maxColorValue=255))
|
||
text(240,V2[185],"{time_2}",cex={time_2_font_size},col=rgb({time_color},100,maxColorValue=255))
|
||
|
||
mtext("{title}",3,line=1.5,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-0.2,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=3,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=0,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R")
|
||
|
||
|
||
# 基础参数 file_path, char_out_path, out_path, title, caption, title_font_size, cluster_rows, cluster_cols, legend, scale_font_size, width, height
|
||
# 高级参数 title_font_color, border_color, color
|
||
# 示例文件 Heat_Map.xlsx
|
||
|
||
def Heat_Map(file_path, char_out_path, out_path, title, caption, title_font_size, cluster_rows, cluster_cols, legend,
|
||
scale_font_size, width, height,
|
||
title_font_color, border_color, color):
|
||
code = f"""
|
||
library(RColorBrewer)
|
||
library(pheatmap)
|
||
library(gdata)
|
||
myGrades<-read.xls('{file_path}',encoding="latin1")
|
||
names(myGrades) <- c("names","Maths","Physics","Biology","Chemistry","German","History","English","Latin","Art","Sports","Music","Geography","Sum")
|
||
x<-as.matrix(myGrades[,2:13])
|
||
rownames(x)<-myGrades$names
|
||
x<-x[order(rowSums(x)), ]
|
||
x<-x[,order(colSums(x))]
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(mai=c(0.25,0.25,0.25,1.75),omi=c(0.25,0.25,0.75,0.85),las=1)
|
||
|
||
plot.new()
|
||
pheatmap(x,col=brewer.pal(6,"{color}"),cluster_rows={cluster_rows},cluster_cols={cluster_cols},cellwidth={width},cellheight={height},border_color="{border_color}",fontsize={scale_font_size},legend={legend},annotation_colors="res")
|
||
|
||
mtext("{title}",3,line=2,adj=0.2,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=-1,adj=1,cex={title_font_size}*0.5,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(mai=c(0.25,0.25,0.25,1.75),omi=c(0.25,0.25,0.75,0.85),las=1)
|
||
|
||
plot.new()
|
||
pheatmap(x,col=brewer.pal(6,"{color}"),cluster_rows={cluster_rows},cluster_cols={cluster_cols},cellwidth={width},cellheight={height},border_color="{border_color}",fontsize={scale_font_size},legend={legend},annotation_colors="res")
|
||
|
||
mtext("{title}",3,line=2,adj=0.2,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=-1,adj=1,cex={title_font_size}*0.5,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(mai=c(0.25,0.25,0.25,1.75),omi=c(0.25,0.25,0.75,0.85),las=1)
|
||
|
||
plot.new()
|
||
pheatmap(x,col=brewer.pal(6,"{color}"),cluster_rows={cluster_rows},cluster_cols={cluster_cols},cellwidth={width},cellheight={height},border_color="{border_color}",fontsize={scale_font_size},legend={legend},annotation_colors="res")
|
||
|
||
mtext("{title}",3,line=2,adj=0.2,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=-1,adj=1,cex={title_font_size}*0.5,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(mai=c(0.25,0.25,0.25,1.75),omi=c(0.25,0.25,0.75,0.85),las=1)
|
||
|
||
plot.new()
|
||
pheatmap(x,col=brewer.pal(6,"{color}"),cluster_rows={cluster_rows},cluster_cols={cluster_cols},cellwidth={width},cellheight={height},border_color="{border_color}",fontsize={scale_font_size},legend={legend},annotation_colors="res")
|
||
|
||
mtext("{title}",3,line=2,adj=0.2,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=-1,adj=1,cex={title_font_size}*0.5,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R")
|
||
|
||
|
||
# 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size, time_1, time_2, chart_bar_y_name, chart_bar_y_font_size, chart_bar_y_scale_font_size, chart_bar_x_scale_font_size
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color, line_color, color, chart_bar_line_color, chart_bar_color, chart_bar_y_font_color, chart_bar_y_scale_font_color, chart_bar_x_scale_font_color
|
||
# 示例文件 Monthly_Values_with_Monthly_Labels_Layout.xlsx
|
||
|
||
def Monthly_Values_with_Monthly_Labels_Layout(file_path, char_out_path, out_path, title, caption, subtitle,
|
||
title_font_size,
|
||
x_name, x_font_size, x_scale_font_size, y_name, y_font_size,
|
||
y_scale_font_size, time_1, time_2,
|
||
chart_bar_y_name, chart_bar_y_font_size, chart_bar_y_scale_font_size,
|
||
chart_bar_x_scale_font_size,
|
||
title_font_color, x_font_color, x_scale_font_color, y_font_color,
|
||
y_scale_font_color, line_color,
|
||
color, chart_bar_line_color, chart_bar_color, chart_bar_y_font_color,
|
||
chart_bar_y_scale_font_color,
|
||
chart_bar_x_scale_font_color, background_color):
|
||
color = color[4:-1]
|
||
|
||
background_color = background_color[4:-1]
|
||
|
||
chart_bar_color = chart_bar_color[4:-1]
|
||
|
||
code = f"""
|
||
library(gdata)
|
||
library(gdata)
|
||
myData<-read.xls('{file_path}',encoding="latin1")
|
||
names(myData) <- c("Monat", "Wert")
|
||
attach(myData)
|
||
|
||
myColour1<-rgb({color},150,maxColorValue=255)
|
||
myColour2<-rgb({chart_bar_color},150,maxColorValue=255)
|
||
|
||
monthbegin<-seq(as.Date("{time_1}"),as.Date("{time_2}"),by="1 months")
|
||
yearbegin<-seq(as.Date("{time_1}"),as.Date("{time_2}"),by="1 years")
|
||
|
||
pdf("{out_path}.pdf")
|
||
layout(matrix(c(1,2),ncol=1),heights=c(80,20))
|
||
par(cex=0.75,bg=rgb({background_color},maxColorValue=255),omi=c(0.75,0.25,0.5,0.25),mai=c(0.25,0.75,0.25,0),mgp=c(2,1,0),las=1)
|
||
plot(type="n",axes=F,xlab="",ylab="{y_name}",as.Date(paste(Monat,"01",sep="-")),Wert,cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
abline(v=yearbegin,col="{line_color}")
|
||
points(as.Date(paste(Monat,"01",sep="-")),Wert,col=myColour1,lwd=5,type="l")
|
||
axis(1,col="{line_color}",at=monthbegin,labels=format(monthbegin,"%b\n%Y"),cex.axis={x_scale_font_size},lwd.ticks=0.1,tck=-0.005,col.axis="{x_scale_font_color}")
|
||
axis(2,col="{line_color}",col.ticks="{line_color}",lwd.ticks=0.5,cex.axis={y_scale_font_size},tck=-0.01,pos=as.Date("{time_1}"),col.axis="{y_scale_font_color}")
|
||
myRate<-rep(0,nrow(myData))
|
||
for (i in 2:nrow(myData)) myRate[i]<-(Wert[i]-Wert[i-1])/Wert[i-1]
|
||
plot(type="h",axes=F,xlab="",ylab="{chart_bar_y_name}",as.Date(paste(Monat,"01",sep="-")),myRate,col=myColour2,lwd=3,cex.lab={chart_bar_y_font_size},col.lab="{chart_bar_y_font_color}")
|
||
axis(1,col="{chart_bar_line_color}",at=monthbegin,labels=format(monthbegin,"%b\n%Y"),cex.axis={chart_bar_x_scale_font_size},lwd.ticks=0.1,tck=-0.02,col.axis="{chart_bar_x_scale_font_color}")
|
||
axis(2,col=rgb({background_color},maxColorValue=255),col.ticks="{chart_bar_line_color}",lwd.ticks=0.5,cex.axis={chart_bar_y_scale_font_size},tck=-0.025,pos=as.Date("{time_1}"),col.axis="{chart_bar_y_scale_font_color}")
|
||
|
||
mtext("{title}",3,line=1,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-1,adj=0,cex={title_font_size}*0.7,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=1,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
layout(matrix(c(1,2),ncol=1),heights=c(80,20))
|
||
par(cex=0.75,bg=rgb({background_color},maxColorValue=255),omi=c(0.75,0.25,0.5,0.25),mai=c(0.25,0.75,0.25,0),mgp=c(2,1,0),las=1)
|
||
plot(type="n",axes=F,xlab="",ylab="{y_name}",as.Date(paste(Monat,"01",sep="-")),Wert,cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
abline(v=yearbegin,col="{line_color}")
|
||
points(as.Date(paste(Monat,"01",sep="-")),Wert,col=myColour1,lwd=5,type="l")
|
||
axis(1,col="{line_color}",at=monthbegin,labels=format(monthbegin,"%b\n%Y"),cex.axis={x_scale_font_size},lwd.ticks=0.1,tck=-0.005,col.axis="{x_scale_font_color}")
|
||
axis(2,col="{line_color}",col.ticks="{line_color}",lwd.ticks=0.5,cex.axis={y_scale_font_size},tck=-0.01,pos=as.Date("{time_1}"),col.axis="{y_scale_font_color}")
|
||
myRate<-rep(0,nrow(myData))
|
||
for (i in 2:nrow(myData)) myRate[i]<-(Wert[i]-Wert[i-1])/Wert[i-1]
|
||
plot(type="h",axes=F,xlab="",ylab="{chart_bar_y_name}",as.Date(paste(Monat,"01",sep="-")),myRate,col=myColour2,lwd=3,cex.lab={chart_bar_y_font_size},col.lab="{chart_bar_y_font_color}")
|
||
axis(1,col="{chart_bar_line_color}",at=monthbegin,labels=format(monthbegin,"%b\n%Y"),cex.axis={chart_bar_x_scale_font_size},lwd.ticks=0.1,tck=-0.02,col.axis="{chart_bar_x_scale_font_color}")
|
||
axis(2,col=rgb({background_color},maxColorValue=255),col.ticks="{chart_bar_line_color}",lwd.ticks=0.5,cex.axis={chart_bar_y_scale_font_size},tck=-0.025,pos=as.Date("{time_1}"),col.axis="{chart_bar_y_scale_font_color}")
|
||
|
||
mtext("{title}",3,line=1,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-1,adj=0,cex={title_font_size}*0.7,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=1,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
layout(matrix(c(1,2),ncol=1),heights=c(80,20))
|
||
par(cex=0.75,bg=rgb({background_color},maxColorValue=255),omi=c(0.75,0.25,0.5,0.25),mai=c(0.25,0.75,0.25,0),mgp=c(2,1,0),las=1)
|
||
plot(type="n",axes=F,xlab="",ylab="{y_name}",as.Date(paste(Monat,"01",sep="-")),Wert,cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
abline(v=yearbegin,col="{line_color}")
|
||
points(as.Date(paste(Monat,"01",sep="-")),Wert,col=myColour1,lwd=5,type="l")
|
||
axis(1,col="{line_color}",at=monthbegin,labels=format(monthbegin,"%b\n%Y"),cex.axis={x_scale_font_size},lwd.ticks=0.1,tck=-0.005,col.axis="{x_scale_font_color}")
|
||
axis(2,col="{line_color}",col.ticks="{line_color}",lwd.ticks=0.5,cex.axis={y_scale_font_size},tck=-0.01,pos=as.Date("{time_1}"),col.axis="{y_scale_font_color}")
|
||
myRate<-rep(0,nrow(myData))
|
||
for (i in 2:nrow(myData)) myRate[i]<-(Wert[i]-Wert[i-1])/Wert[i-1]
|
||
plot(type="h",axes=F,xlab="",ylab="{chart_bar_y_name}",as.Date(paste(Monat,"01",sep="-")),myRate,col=myColour2,lwd=3,cex.lab={chart_bar_y_font_size},col.lab="{chart_bar_y_font_color}")
|
||
axis(1,col="{chart_bar_line_color}",at=monthbegin,labels=format(monthbegin,"%b\n%Y"),cex.axis={chart_bar_x_scale_font_size},lwd.ticks=0.1,tck=-0.02,col.axis="{chart_bar_x_scale_font_color}")
|
||
axis(2,col=rgb({background_color},maxColorValue=255),col.ticks="{chart_bar_line_color}",lwd.ticks=0.5,cex.axis={chart_bar_y_scale_font_size},tck=-0.025,pos=as.Date("{time_1}"),col.axis="{chart_bar_y_scale_font_color}")
|
||
|
||
mtext("{title}",3,line=1,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-1,adj=0,cex={title_font_size}*0.7,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=1,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
layout(matrix(c(1,2),ncol=1),heights=c(80,20))
|
||
par(cex=0.75,bg=rgb({background_color},maxColorValue=255),omi=c(0.75,0.25,0.5,0.25),mai=c(0.25,0.75,0.25,0),mgp=c(2,1,0),las=1)
|
||
plot(type="n",axes=F,xlab="",ylab="{y_name}",as.Date(paste(Monat,"01",sep="-")),Wert,cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
abline(v=yearbegin,col="{line_color}")
|
||
points(as.Date(paste(Monat,"01",sep="-")),Wert,col=myColour1,lwd=5,type="l")
|
||
axis(1,col="{line_color}",at=monthbegin,labels=format(monthbegin,"%b\n%Y"),cex.axis={x_scale_font_size},lwd.ticks=0.1,tck=-0.005,col.axis="{x_scale_font_color}")
|
||
axis(2,col="{line_color}",col.ticks="{line_color}",lwd.ticks=0.5,cex.axis={y_scale_font_size},tck=-0.01,pos=as.Date("{time_1}"),col.axis="{y_scale_font_color}")
|
||
myRate<-rep(0,nrow(myData))
|
||
for (i in 2:nrow(myData)) myRate[i]<-(Wert[i]-Wert[i-1])/Wert[i-1]
|
||
plot(type="h",axes=F,xlab="",ylab="{chart_bar_y_name}",as.Date(paste(Monat,"01",sep="-")),myRate,col=myColour2,lwd=3,cex.lab={chart_bar_y_font_size},col.lab="{chart_bar_y_font_color}")
|
||
axis(1,col="{chart_bar_line_color}",at=monthbegin,labels=format(monthbegin,"%b\n%Y"),cex.axis={chart_bar_x_scale_font_size},lwd.ticks=0.1,tck=-0.02,col.axis="{chart_bar_x_scale_font_color}")
|
||
axis(2,col=rgb({background_color},maxColorValue=255),col.ticks="{chart_bar_line_color}",lwd.ticks=0.5,cex.axis={chart_bar_y_scale_font_size},tck=-0.025,pos=as.Date("{time_1}"),col.axis="{chart_bar_y_scale_font_color}")
|
||
|
||
mtext("{title}",3,line=1,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-1,adj=0,cex={title_font_size}*0.7,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=1,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_scale_font_size
|
||
|
||
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, y_scale_font_color, bar_color, color_1, color_2
|
||
# 示例文件 Quarterly_Values_as_Columns.xlsx
|
||
|
||
def Quarterly_Values_as_Columns(file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name,
|
||
x_font_size, x_scale_font_size, y_scale_font_size,
|
||
title_font_color, x_font_color, x_scale_font_color, y_scale_font_color, bar_color,
|
||
color_1, color_2,
|
||
background_color):
|
||
code = f"""
|
||
library(gplots)
|
||
library(gdata)
|
||
gdp<-read.xls('{file_path}',encoding="latin1")
|
||
names(gdp) <- c("year","quarter","respective","priceadjusted")
|
||
x<-rev(gdp$priceadjusted)
|
||
t<-unique(gdp$year)
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(omi=c(0.65,0.75,0.95,0.75),mai=c(0.9,0,0.25,0.02),fg="{background_color}",bg="{background_color}",las=1)
|
||
|
||
par(mfcol=c(1,length(t)))
|
||
for (i in length(t):1)
|
||
{{
|
||
subset(gdp$priceadjusted,gdp$year == t[i])
|
||
xt<-subset(gdp$priceadjusted,gdp$year == t[i])
|
||
myColours<-rep("{color_1}",length(xt))
|
||
for (j in 1:length(xt)) if(xt[j]<0) myColours[j]<-"{color_2}"
|
||
barplot2(rev(xt),border=NA,bty="n",col=rev(myColours),ylim=c(-4,2),axes=F,prcol="{bar_color}")
|
||
if (i==length(t)) axis(2,col="{background_color}",cex.axis=1.25,at=c(-4:2),labels=c("-4%","-3%","-2%","-1%","0%","1%","2%"),col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
mtext(t[i],1,line=2,col="{x_scale_font_color}",cex={x_scale_font_size})
|
||
}}
|
||
|
||
mtext("{title}",3,line=2.5,adj=0,cex={title_font_size},col="{title_font_color}",outer=T)
|
||
mtext("{subtitle}",3,line=-0.5,adj=0,cex={title_font_size}*0.6,font=3,col="{title_font_color}",outer=T)
|
||
mtext("{caption}",1,line=1,adj=1,cex={title_font_size}*0.6,font=3,col="{title_font_color}",outer=T)
|
||
mtext("{x_name}",1,line=-1,adj=0.47,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(omi=c(0.65,0.75,0.95,0.75),mai=c(0.9,0,0.25,0.02),fg="{background_color}",bg="{background_color}",las=1)
|
||
|
||
par(mfcol=c(1,length(t)))
|
||
for (i in length(t):1)
|
||
{{
|
||
subset(gdp$priceadjusted,gdp$year == t[i])
|
||
xt<-subset(gdp$priceadjusted,gdp$year == t[i])
|
||
myColours<-rep("{color_1}",length(xt))
|
||
for (j in 1:length(xt)) if(xt[j]<0) myColours[j]<-"{color_2}"
|
||
barplot2(rev(xt),border=NA,bty="n",col=rev(myColours),ylim=c(-4,2),axes=F,prcol="{bar_color}")
|
||
if (i==length(t)) axis(2,col="{background_color}",cex.axis=1.25,at=c(-4:2),labels=c("-4%","-3%","-2%","-1%","0%","1%","2%"),col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
mtext(t[i],1,line=2,col="{x_scale_font_color}",cex={x_scale_font_size})
|
||
}}
|
||
|
||
mtext("{title}",3,line=2.5,adj=0,cex={title_font_size},col="{title_font_color}",outer=T)
|
||
mtext("{subtitle}",3,line=-0.5,adj=0,cex={title_font_size}*0.6,font=3,col="{title_font_color}",outer=T)
|
||
mtext("{caption}",1,line=1,adj=1,cex={title_font_size}*0.6,font=3,col="{title_font_color}",outer=T)
|
||
mtext("{x_name}",1,line=-1,adj=0.47,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(omi=c(0.65,0.75,0.95,0.75),mai=c(0.9,0,0.25,0.02),fg="{background_color}",bg="{background_color}",las=1)
|
||
|
||
par(mfcol=c(1,length(t)))
|
||
for (i in length(t):1)
|
||
{{
|
||
subset(gdp$priceadjusted,gdp$year == t[i])
|
||
xt<-subset(gdp$priceadjusted,gdp$year == t[i])
|
||
myColours<-rep("{color_1}",length(xt))
|
||
for (j in 1:length(xt)) if(xt[j]<0) myColours[j]<-"{color_2}"
|
||
barplot2(rev(xt),border=NA,bty="n",col=rev(myColours),ylim=c(-4,2),axes=F,prcol="{bar_color}")
|
||
if (i==length(t)) axis(2,col="{background_color}",cex.axis=1.25,at=c(-4:2),labels=c("-4%","-3%","-2%","-1%","0%","1%","2%"),col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
mtext(t[i],1,line=2,col="{x_scale_font_color}",cex={x_scale_font_size})
|
||
}}
|
||
|
||
mtext("{title}",3,line=2.5,adj=0,cex={title_font_size},col="{title_font_color}",outer=T)
|
||
mtext("{subtitle}",3,line=-0.5,adj=0,cex={title_font_size}*0.6,font=3,col="{title_font_color}",outer=T)
|
||
mtext("{caption}",1,line=1,adj=1,cex={title_font_size}*0.6,font=3,col="{title_font_color}",outer=T)
|
||
mtext("{x_name}",1,line=-1,adj=0.47,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(omi=c(0.65,0.75,0.95,0.75),mai=c(0.9,0,0.25,0.02),fg="{background_color}",bg="{background_color}",las=1)
|
||
|
||
par(mfcol=c(1,length(t)))
|
||
for (i in length(t):1)
|
||
{{
|
||
subset(gdp$priceadjusted,gdp$year == t[i])
|
||
xt<-subset(gdp$priceadjusted,gdp$year == t[i])
|
||
myColours<-rep("{color_1}",length(xt))
|
||
for (j in 1:length(xt)) if(xt[j]<0) myColours[j]<-"{color_2}"
|
||
barplot2(rev(xt),border=NA,bty="n",col=rev(myColours),ylim=c(-4,2),axes=F,prcol="{bar_color}")
|
||
if (i==length(t)) axis(2,col="{background_color}",cex.axis=1.25,at=c(-4:2),labels=c("-4%","-3%","-2%","-1%","0%","1%","2%"),col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
mtext(t[i],1,line=2,col="{x_scale_font_color}",cex={x_scale_font_size})
|
||
}}
|
||
|
||
mtext("{title}",3,line=2.5,adj=0,cex={title_font_size},col="{title_font_color}",outer=T)
|
||
mtext("{subtitle}",3,line=-0.5,adj=0,cex={title_font_size}*0.6,font=3,col="{title_font_color}",outer=T)
|
||
mtext("{caption}",1,line=1,adj=1,cex={title_font_size}*0.6,font=3,col="{title_font_color}",outer=T)
|
||
mtext("{x_name}",1,line=-1,adj=0.47,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size, font_size
|
||
|
||
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color, font_color, line_color, color
|
||
# 示例文件 Quarterly_Values_as_Lines_with_Value_Labels.xlsx
|
||
|
||
def Quarterly_Values_as_Lines_with_Value_Labels(file_path, char_out_path, out_path, title, caption, subtitle,
|
||
title_font_size, x_name, x_font_size, x_scale_font_size,
|
||
y_scale_font_size, font_size,
|
||
title_font_color, x_font_color, x_scale_font_color, y_scale_font_color,
|
||
font_color,
|
||
line_color, scale_line_color, point_color, background_color):
|
||
line_color = line_color[4:-1]
|
||
|
||
point_color = point_color[4:-1]
|
||
|
||
code = f"""
|
||
library(gdata)
|
||
gdp<-read.xls('{file_path}',encoding="latin1")
|
||
names <- c("year","quarter","jeworiginal","jewkalber","preisboriginal","preisbkalber")
|
||
gdp<-subset(gdp,gdp$year > 2007)
|
||
x<-ts(rev(gdp$jeworiginal),start=2008,frequency=4)
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(omi=c(0.65,0.75,0.95,0.75),mai=c(0.9,0,0.25,0.02),fg="{x_scale_font_color}",bg="{background_color}",las=1)
|
||
|
||
plot(x,type="n",axes=F,xlim=c(2008,2012),ylim=c(560,670),xlab="",ylab="")
|
||
abline(v=c(2008:2012),col="{scale_line_color}",lty=1,lwd=1)
|
||
lines(x,lwd=8,type="b",col=rgb({line_color},80,maxColorValue=255))
|
||
points(x,pch=19,cex=3,col=rgb({point_color},maxColorValue=255))
|
||
faktor<-rep(0.985,length(x))
|
||
for (i in 1:length(x))
|
||
{{
|
||
if (i>1 & i<length(x)) {{ if (x[i]>x[i-1] & x[i]>x[i+1]) {{ faktor[i]<-1.015 }} }}
|
||
text((2008+i*0.25)-0.25,faktor[i]*x[i],x[i],col="{font_color}",cex={font_size})
|
||
}}
|
||
axis(1,at=c(2008:2012),tck=0,cex.axis={x_scale_font_size},col.axis="{x_scale_font_color}")
|
||
axis(2,col=NA,col.ticks="{y_scale_font_color}",lwd.ticks=0.5,cex.axis={y_scale_font_size},tck=-0.025,col.axis="{y_scale_font_color}")
|
||
|
||
mtext("{title}",3,line=2.3,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=0,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=1,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(omi=c(0.65,0.75,0.95,0.75),mai=c(0.9,0,0.25,0.02),fg="{x_scale_font_color}",bg="{background_color}",las=1)
|
||
|
||
plot(x,type="n",axes=F,xlim=c(2008,2012),ylim=c(560,670),xlab="",ylab="")
|
||
abline(v=c(2008:2012),col="{scale_line_color}",lty=1,lwd=1)
|
||
lines(x,lwd=8,type="b",col=rgb({line_color},80,maxColorValue=255))
|
||
points(x,pch=19,cex=3,col=rgb({point_color},maxColorValue=255))
|
||
faktor<-rep(0.985,length(x))
|
||
for (i in 1:length(x))
|
||
{{
|
||
if (i>1 & i<length(x)) {{ if (x[i]>x[i-1] & x[i]>x[i+1]) {{ faktor[i]<-1.015 }} }}
|
||
text((2008+i*0.25)-0.25,faktor[i]*x[i],x[i],col="{font_color}",cex={font_size})
|
||
}}
|
||
axis(1,at=c(2008:2012),tck=0,cex.axis={x_scale_font_size},col.axis="{x_scale_font_color}")
|
||
axis(2,col=NA,col.ticks="{y_scale_font_color}",lwd.ticks=0.5,cex.axis={y_scale_font_size},tck=-0.025,col.axis="{y_scale_font_color}")
|
||
|
||
mtext("{title}",3,line=2.3,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=0,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=1,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(omi=c(0.65,0.75,0.95,0.75),mai=c(0.9,0,0.25,0.02),fg="{x_scale_font_color}",bg="{background_color}",las=1)
|
||
|
||
plot(x,type="n",axes=F,xlim=c(2008,2012),ylim=c(560,670),xlab="",ylab="")
|
||
abline(v=c(2008:2012),col="{scale_line_color}",lty=1,lwd=1)
|
||
lines(x,lwd=8,type="b",col=rgb({line_color},80,maxColorValue=255))
|
||
points(x,pch=19,cex=3,col=rgb({point_color},maxColorValue=255))
|
||
faktor<-rep(0.985,length(x))
|
||
for (i in 1:length(x))
|
||
{{
|
||
if (i>1 & i<length(x)) {{ if (x[i]>x[i-1] & x[i]>x[i+1]) {{ faktor[i]<-1.015 }} }}
|
||
text((2008+i*0.25)-0.25,faktor[i]*x[i],x[i],col="{font_color}",cex={font_size})
|
||
}}
|
||
axis(1,at=c(2008:2012),tck=0,cex.axis={x_scale_font_size},col.axis="{x_scale_font_color}")
|
||
axis(2,col=NA,col.ticks="{y_scale_font_color}",lwd.ticks=0.5,cex.axis={y_scale_font_size},tck=-0.025,col.axis="{y_scale_font_color}")
|
||
|
||
mtext("{title}",3,line=2.3,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=0,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=1,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(omi=c(0.65,0.75,0.95,0.75),mai=c(0.9,0,0.25,0.02),fg="{x_scale_font_color}",bg="{background_color}",las=1)
|
||
|
||
plot(x,type="n",axes=F,xlim=c(2008,2012),ylim=c(560,670),xlab="",ylab="")
|
||
abline(v=c(2008:2012),col="{scale_line_color}",lty=1,lwd=1)
|
||
lines(x,lwd=8,type="b",col=rgb({line_color},80,maxColorValue=255))
|
||
points(x,pch=19,cex=3,col=rgb({point_color},maxColorValue=255))
|
||
faktor<-rep(0.985,length(x))
|
||
for (i in 1:length(x))
|
||
{{
|
||
if (i>1 & i<length(x)) {{ if (x[i]>x[i-1] & x[i]>x[i+1]) {{ faktor[i]<-1.015 }} }}
|
||
text((2008+i*0.25)-0.25,faktor[i]*x[i],x[i],col="{font_color}",cex={font_size})
|
||
}}
|
||
axis(1,at=c(2008:2012),tck=0,cex.axis={x_scale_font_size},col.axis="{x_scale_font_color}")
|
||
axis(2,col=NA,col.ticks="{y_scale_font_color}",lwd.ticks=0.5,cex.axis={y_scale_font_size},tck=-0.025,col.axis="{y_scale_font_color}")
|
||
|
||
mtext("{title}",3,line=2.3,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=0,adj=0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=1,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1,adj=0.5,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, legend_1, legend_2, legend_size
|
||
|
||
|
||
# 高级参数 legend_position, title_font_color, color_1, color_2, line_color
|
||
# 示例文件 Radial_Polygons_Overlay.xlsx
|
||
|
||
def Radial_Polygons_Overlay(file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, legend_1,
|
||
legend_2, legend_size,
|
||
legend_position, title_font_color, color_1, color_2, line_color):
|
||
|
||
color_1 = color_1[4:-1]
|
||
|
||
color_2 = color_2[4:-1]
|
||
|
||
code = f"""
|
||
library(plotrix)
|
||
library(gdata)
|
||
|
||
myRegions <- read.xls("{file_path}", encoding="latin1")
|
||
|
||
myRegions[,1]<-NULL
|
||
|
||
myLabelling <- names(myRegions)
|
||
|
||
myC1<-rgb({color_1},155,maxColorValue=255)
|
||
myC2<-rgb({color_2},155,maxColorValue=255)
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(omi=c(1,0.25,1,1),mai=c(0,2,0,0.5),cex.axis=1.5,cex.lab=1,xpd=T,las=1)
|
||
|
||
# Create chart
|
||
|
||
radial.plot(myRegions[2:3,],start=1,grid.left=T,labels=myLabelling,rp.type="p",main="",line.col=c(myC1,myC2),poly.col=c(myC1,myC2),show.grid=T,radial.lim=c(0,55),lwd=8,rad.col="{line_color}",grid.col="{line_color}")
|
||
legend("{legend_position}",c("{legend_1}","{legend_2}"),pch=15,col=c(myC1,myC2),bty="n",cex={legend_size})
|
||
|
||
# Titling
|
||
|
||
mtext(line=3,"{title}",cex={title_font_size},adj=0,col="{title_font_color}")
|
||
mtext(line=1,"{subtitle}",cex={title_font_size}*0.6,adj=0,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=2,"{caption}",cex={title_font_size}*0.4,adj=1,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(omi=c(1,0.25,1,1),mai=c(0,2,0,0.5),cex.axis=1.5,cex.lab=1,xpd=T,las=1)
|
||
|
||
# Create chart
|
||
|
||
radial.plot(myRegions[2:3,],start=1,grid.left=T,labels=myLabelling,rp.type="p",main="",line.col=c(myC1,myC2),poly.col=c(myC1,myC2),show.grid=T,radial.lim=c(0,55),lwd=8,rad.col="{line_color}",grid.col="{line_color}")
|
||
legend("{legend_position}",c("{legend_1}","{legend_2}"),pch=15,col=c(myC1,myC2),bty="n",cex={legend_size})
|
||
|
||
# Titling
|
||
|
||
mtext(line=3,"{title}",cex={title_font_size},adj=0,col="{title_font_color}")
|
||
mtext(line=1,"{subtitle}",cex={title_font_size}*0.6,adj=0,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=2,"{caption}",cex={title_font_size}*0.4,adj=1,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(omi=c(1,0.25,1,1),mai=c(0,2,0,0.5),cex.axis=1.5,cex.lab=1,xpd=T,las=1)
|
||
|
||
# Create chart
|
||
|
||
radial.plot(myRegions[2:3,],start=1,grid.left=T,labels=myLabelling,rp.type="p",main="",line.col=c(myC1,myC2),poly.col=c(myC1,myC2),show.grid=T,radial.lim=c(0,55),lwd=8,rad.col="{line_color}",grid.col="{line_color}")
|
||
legend("{legend_position}",c("{legend_1}","{legend_2}"),pch=15,col=c(myC1,myC2),bty="n",cex={legend_size})
|
||
|
||
# Titling
|
||
|
||
mtext(line=3,"{title}",cex={title_font_size},adj=0,col="{title_font_color}")
|
||
mtext(line=1,"{subtitle}",cex={title_font_size}*0.6,adj=0,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=2,"{caption}",cex={title_font_size}*0.4,adj=1,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(omi=c(1,0.25,1,1),mai=c(0,2,0,0.5),cex.axis=1.5,cex.lab=1,xpd=T,las=1)
|
||
|
||
# Create chart
|
||
|
||
radial.plot(myRegions[2:3,],start=1,grid.left=T,labels=myLabelling,rp.type="p",main="",line.col=c(myC1,myC2),poly.col=c(myC1,myC2),show.grid=T,radial.lim=c(0,55),lwd=8,rad.col="{line_color}",grid.col="{line_color}")
|
||
legend("{legend_position}",c("{legend_1}","{legend_2}"),pch=15,col=c(myC1,myC2),bty="n",cex={legend_size})
|
||
|
||
# Titling
|
||
|
||
mtext(line=3,"{title}",cex={title_font_size},adj=0,col="{title_font_color}")
|
||
mtext(line=1,"{subtitle}",cex={title_font_size}*0.6,adj=0,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=2,"{caption}",cex={title_font_size}*0.4,adj=1,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size, annotation, annotation_size, deviations_size
|
||
|
||
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color, annotation_color, color_1, color_2, deviations_color, mean_line_color
|
||
# 示例文件 Scatter_Plot_Variant_2_Outliers_Highlighted.csv
|
||
|
||
def Scatter_Plot_Variant_2_Outliers_Highlighted(file_path, char_out_path, out_path, title, caption, subtitle,
|
||
title_font_size, x_name, x_font_size, x_scale_font_size, y_name,
|
||
y_font_size, y_scale_font_size,
|
||
annotation, annotation_size, deviations_size,
|
||
title_font_color, x_font_color, x_scale_font_color, y_font_color,
|
||
y_scale_font_color,
|
||
annotation_color, color_1, color_2, deviations_color, mean_line_color):
|
||
color_1 = color_1[4:-1]
|
||
|
||
color_2 = color_2[4:-1]
|
||
|
||
code = f"""
|
||
library(gdata)
|
||
myStructuralData<-read.csv(file="{file_path}",head=F,sep=";",dec=".")
|
||
myData<-subset(myStructuralData,V2 > 0 & V34 > 10)
|
||
attach(myData)
|
||
myXDes<-""
|
||
myYDes<-"{y_name}"
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(mar=c(4,4,0.5,2),omi=c(0.5,0.5,1,0),las=1)
|
||
|
||
plot(type="n",xlab=myXDes,ylab=myYDes,V34,V21,xlim=c(10,26),ylim=c(-20,35),axes=F,cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
axis(1,lwd.ticks=0.5,cex.axis={x_scale_font_size},tck=-0.015,col.axis="{x_scale_font_color}",col="{x_scale_font_color}")
|
||
axis(2,lwd.ticks=0.5,cex.axis={y_scale_font_size},tck=-0.015,col.axis="{y_scale_font_color}",col="{y_scale_font_color}")
|
||
|
||
myC1<-rgb({color_1},200,maxColorValue=255)
|
||
myC2<-rgb({color_2},150,maxColorValue=255)
|
||
|
||
fit<-lm(V21 ~ V34)
|
||
myData$fit<-fitted(fit)
|
||
points(V34,myData$fit,col=myC2,type="l",lwd=8)
|
||
|
||
myData$resid<-residuals(fit)
|
||
myData.sort<-myData[order(-abs(myData$resid)) ,]
|
||
myData.sort_begin<-myData.sort[1:5,]
|
||
|
||
myP1<-myData.sort[5+1:length(myData$fit),c("V34","V21")]
|
||
myP2<-myData.sort_begin[c("V34","V21")]
|
||
|
||
myR1<-sqrt(myData.sort$V6)/10
|
||
myR2<-sqrt(myData.sort_begin$V6)/10
|
||
|
||
symbols(myP1,circles=myR1,inches=0.3,bg=myC1,fg="white",add=T)
|
||
symbols(myP2,circles=myR2,inches=0.3,bg=myC2,fg="white",add=T)
|
||
|
||
text(myP2,iconv(as.matrix(myData.sort_begin["V3"]),"LATIN1","UTF-8"),cex={deviations_size},pos=3,offset=1.1,col="{deviations_color}")
|
||
|
||
abline(v=mean(V34,na.rm=T),col="{mean_line_color}",lty=3)
|
||
abline(h=mean(V21,na.rm=T),col="{mean_line_color}",lty=3)
|
||
|
||
text(20,20, "{annotation}", adj=0,cex={annotation_size},col="{annotation_color}")
|
||
mtext("{title}",3,adj=0,line=2,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0,line=0,cex={title_font_size}*0.6,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=4,adj=1,cex={title_font_size}*0.4,font=3,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1,adj=0.52,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(mar=c(4,4,0.5,2),omi=c(0.5,0.5,1,0),las=1)
|
||
|
||
plot(type="n",xlab=myXDes,ylab=myYDes,V34,V21,xlim=c(10,26),ylim=c(-20,35),axes=F,cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
axis(1,lwd.ticks=0.5,cex.axis={x_scale_font_size},tck=-0.015,col.axis="{x_scale_font_color}",col="{x_scale_font_color}")
|
||
axis(2,lwd.ticks=0.5,cex.axis={y_scale_font_size},tck=-0.015,col.axis="{y_scale_font_color}",col="{y_scale_font_color}")
|
||
|
||
myC1<-rgb({color_1},200,maxColorValue=255)
|
||
myC2<-rgb({color_2},150,maxColorValue=255)
|
||
|
||
fit<-lm(V21 ~ V34)
|
||
myData$fit<-fitted(fit)
|
||
points(V34,myData$fit,col=myC2,type="l",lwd=8)
|
||
|
||
myData$resid<-residuals(fit)
|
||
myData.sort<-myData[order(-abs(myData$resid)) ,]
|
||
myData.sort_begin<-myData.sort[1:5,]
|
||
|
||
myP1<-myData.sort[5+1:length(myData$fit),c("V34","V21")]
|
||
myP2<-myData.sort_begin[c("V34","V21")]
|
||
|
||
myR1<-sqrt(myData.sort$V6)/10
|
||
myR2<-sqrt(myData.sort_begin$V6)/10
|
||
|
||
symbols(myP1,circles=myR1,inches=0.3,bg=myC1,fg="white",add=T)
|
||
symbols(myP2,circles=myR2,inches=0.3,bg=myC2,fg="white",add=T)
|
||
|
||
text(myP2,iconv(as.matrix(myData.sort_begin["V3"]),"LATIN1","UTF-8"),cex={deviations_size},pos=3,offset=1.1,col="{deviations_color}")
|
||
|
||
abline(v=mean(V34,na.rm=T),col="{mean_line_color}",lty=3)
|
||
abline(h=mean(V21,na.rm=T),col="{mean_line_color}",lty=3)
|
||
|
||
text(20,20, "{annotation}", adj=0,cex={annotation_size},col="{annotation_color}")
|
||
mtext("{title}",3,adj=0,line=2,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0,line=0,cex={title_font_size}*0.6,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=4,adj=1,cex={title_font_size}*0.4,font=3,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1,adj=0.52,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(mar=c(4,4,0.5,2),omi=c(0.5,0.5,1,0),las=1)
|
||
|
||
plot(type="n",xlab=myXDes,ylab=myYDes,V34,V21,xlim=c(10,26),ylim=c(-20,35),axes=F,cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
axis(1,lwd.ticks=0.5,cex.axis={x_scale_font_size},tck=-0.015,col.axis="{x_scale_font_color}",col="{x_scale_font_color}")
|
||
axis(2,lwd.ticks=0.5,cex.axis={y_scale_font_size},tck=-0.015,col.axis="{y_scale_font_color}",col="{y_scale_font_color}")
|
||
|
||
myC1<-rgb({color_1},200,maxColorValue=255)
|
||
myC2<-rgb({color_2},150,maxColorValue=255)
|
||
|
||
fit<-lm(V21 ~ V34)
|
||
myData$fit<-fitted(fit)
|
||
points(V34,myData$fit,col=myC2,type="l",lwd=8)
|
||
|
||
myData$resid<-residuals(fit)
|
||
myData.sort<-myData[order(-abs(myData$resid)) ,]
|
||
myData.sort_begin<-myData.sort[1:5,]
|
||
|
||
myP1<-myData.sort[5+1:length(myData$fit),c("V34","V21")]
|
||
myP2<-myData.sort_begin[c("V34","V21")]
|
||
|
||
myR1<-sqrt(myData.sort$V6)/10
|
||
myR2<-sqrt(myData.sort_begin$V6)/10
|
||
|
||
symbols(myP1,circles=myR1,inches=0.3,bg=myC1,fg="white",add=T)
|
||
symbols(myP2,circles=myR2,inches=0.3,bg=myC2,fg="white",add=T)
|
||
|
||
text(myP2,iconv(as.matrix(myData.sort_begin["V3"]),"LATIN1","UTF-8"),cex={deviations_size},pos=3,offset=1.1,col="{deviations_color}")
|
||
|
||
abline(v=mean(V34,na.rm=T),col="{mean_line_color}",lty=3)
|
||
abline(h=mean(V21,na.rm=T),col="{mean_line_color}",lty=3)
|
||
|
||
text(20,20, "{annotation}", adj=0,cex={annotation_size},col="{annotation_color}")
|
||
mtext("{title}",3,adj=0,line=2,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0,line=0,cex={title_font_size}*0.6,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=4,adj=1,cex={title_font_size}*0.4,font=3,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1,adj=0.52,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(mar=c(4,4,0.5,2),omi=c(0.5,0.5,1,0),las=1)
|
||
|
||
plot(type="n",xlab=myXDes,ylab=myYDes,V34,V21,xlim=c(10,26),ylim=c(-20,35),axes=F,cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
axis(1,lwd.ticks=0.5,cex.axis={x_scale_font_size},tck=-0.015,col.axis="{x_scale_font_color}",col="{x_scale_font_color}")
|
||
axis(2,lwd.ticks=0.5,cex.axis={y_scale_font_size},tck=-0.015,col.axis="{y_scale_font_color}",col="{y_scale_font_color}")
|
||
|
||
myC1<-rgb({color_1},200,maxColorValue=255)
|
||
myC2<-rgb({color_2},150,maxColorValue=255)
|
||
|
||
fit<-lm(V21 ~ V34)
|
||
myData$fit<-fitted(fit)
|
||
points(V34,myData$fit,col=myC2,type="l",lwd=8)
|
||
|
||
myData$resid<-residuals(fit)
|
||
myData.sort<-myData[order(-abs(myData$resid)) ,]
|
||
myData.sort_begin<-myData.sort[1:5,]
|
||
|
||
myP1<-myData.sort[5+1:length(myData$fit),c("V34","V21")]
|
||
myP2<-myData.sort_begin[c("V34","V21")]
|
||
|
||
myR1<-sqrt(myData.sort$V6)/10
|
||
myR2<-sqrt(myData.sort_begin$V6)/10
|
||
|
||
symbols(myP1,circles=myR1,inches=0.3,bg=myC1,fg="white",add=T)
|
||
symbols(myP2,circles=myR2,inches=0.3,bg=myC2,fg="white",add=T)
|
||
|
||
text(myP2,iconv(as.matrix(myData.sort_begin["V3"]),"LATIN1","UTF-8"),cex={deviations_size},pos=3,offset=1.1,col="{deviations_color}")
|
||
|
||
abline(v=mean(V34,na.rm=T),col="{mean_line_color}",lty=3)
|
||
abline(h=mean(V21,na.rm=T),col="{mean_line_color}",lty=3)
|
||
|
||
text(20,20, "{annotation}", adj=0,cex={annotation_size},col="{annotation_color}")
|
||
mtext("{title}",3,adj=0,line=2,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0,line=0,cex={title_font_size}*0.6,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=4,adj=1,cex={title_font_size}*0.4,font=3,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1,adj=0.52,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size, average, average_font_size, celebrities_size
|
||
|
||
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color, average_line_color, celebrities_color, color_1, color_2, average_font_color
|
||
# 示例文件 Scatter_Plot_Variant_3_Areas_Highlighted.xlsx
|
||
|
||
def Scatter_Plot_Variant_3_Areas_Highlighted(file_path, char_out_path, out_path, title, caption, subtitle,
|
||
title_font_size,
|
||
x_name, x_font_size, x_scale_font_size, y_name, y_font_size,
|
||
y_scale_font_size, average,
|
||
average_font_size, celebrities_size,
|
||
title_font_color, x_font_color, x_scale_font_color, y_font_color,
|
||
y_scale_font_color,
|
||
average_line_color, celebrities_color, color_1, color_2,
|
||
average_font_color):
|
||
code = f"""
|
||
library(gdata)
|
||
library(gdata)
|
||
myPersons<-read.xls('{file_path}',encoding="latin1")
|
||
names(myPersons) <- c("markings","name","s","group1","group2","group3","h","w","comment")
|
||
attach(myPersons)
|
||
myData<-subset(myPersons,w>0 & s=="m" & name!="Max Schmeling")
|
||
attach(myData)
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(mai=c(0.85,1,0.25,0.25),omi=c(1,0.5,1,0.5),las=1)
|
||
|
||
plot(type="n",xlab="",ylab="{y_name}",h,w,xlim=c(160,220),ylim=c(50,125),axes=F,cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
axis(1,col=par("bg"),col.ticks="{x_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{x_scale_font_color}",cex.axis={x_scale_font_size})
|
||
axis(2,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
|
||
myC1<-rgb(255,0,210,maxColorValue=255)
|
||
myC2<-rgb(0,208,226,100,maxColorValue=255)
|
||
|
||
myP1<-subset(myData[c("h","w")],w>20*(h/100*h/100) & w<25*(h/100*h/100))
|
||
myP2<-subset(myData[c("h","w")],w<20*(h/100*h/100))
|
||
myP3<-subset(myData[c("h","w")],w>25*(h/100*h/100))
|
||
|
||
myDes2<-as.matrix(subset(name,w<20*(h/100*h/100)))
|
||
myDes3<-as.matrix(subset(name,w>25*(h/100*h/100)))
|
||
|
||
symbols(myP1,bg='{color_1}',fg="white",circles=rep(1,nrow(myP1)),inches=0.25,add=T)
|
||
symbols(myP2,bg='{color_2}',fg="white",circles=rep(1,nrow(myP2)),inches=0.25,add=T)
|
||
symbols(myP3,bg='{color_2}',fg="white",circles=rep(1,nrow(myP3)),inches=0.25,add=T)
|
||
|
||
text(myP2,myDes2,cex={celebrities_size},pos=1,offset=1.1,col="{celebrities_color}")
|
||
text(myP3,myDes3,cex={celebrities_size},pos=3,offset=1.1,col="{celebrities_color}")
|
||
|
||
curve(20*(x/100*x/100),xlim=c(160,220),add=T)
|
||
curve(25*(x/100*x/100),xlim=c(160,220),add=T)
|
||
|
||
abline(v=mean(h,na.rm=T),lty=3,col="{average_line_color}")
|
||
abline(h=mean(w,na.rm=T),lty=3,col="{average_line_color}")
|
||
text(182.5,52,"{average}",adj=0,font=3,cex={average_font_size},col="{average_font_color}")
|
||
|
||
mtext("{title}",3,adj=0,line=2,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0,line=0,cex={title_font_size}*0.6,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=1,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1.2,adj=0.535,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png",width=900,height=408)
|
||
par(mai=c(0.85,1,0.25,0.25),omi=c(1,0.5,1,0.5),las=1)
|
||
|
||
plot(type="n",xlab="",ylab="{y_name}",h,w,xlim=c(160,220),ylim=c(50,125),axes=F,cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
axis(1,col=par("bg"),col.ticks="{x_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{x_scale_font_color}",cex.axis={x_scale_font_size})
|
||
axis(2,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
|
||
myC1<-rgb(255,0,210,maxColorValue=255)
|
||
myC2<-rgb(0,208,226,100,maxColorValue=255)
|
||
|
||
myP1<-subset(myData[c("h","w")],w>20*(h/100*h/100) & w<25*(h/100*h/100))
|
||
myP2<-subset(myData[c("h","w")],w<20*(h/100*h/100))
|
||
myP3<-subset(myData[c("h","w")],w>25*(h/100*h/100))
|
||
|
||
myDes2<-as.matrix(subset(name,w<20*(h/100*h/100)))
|
||
myDes3<-as.matrix(subset(name,w>25*(h/100*h/100)))
|
||
|
||
symbols(myP1,bg='{color_1}',fg="white",circles=rep(1,nrow(myP1)),inches=0.25,add=T)
|
||
symbols(myP2,bg='{color_2}',fg="white",circles=rep(1,nrow(myP2)),inches=0.25,add=T)
|
||
symbols(myP3,bg='{color_2}',fg="white",circles=rep(1,nrow(myP3)),inches=0.25,add=T)
|
||
|
||
text(myP2,myDes2,cex={celebrities_size},pos=1,offset=1.1,col="{celebrities_color}")
|
||
text(myP3,myDes3,cex={celebrities_size},pos=3,offset=1.1,col="{celebrities_color}")
|
||
|
||
curve(20*(x/100*x/100),xlim=c(160,220),add=T)
|
||
curve(25*(x/100*x/100),xlim=c(160,220),add=T)
|
||
|
||
abline(v=mean(h,na.rm=T),lty=3,col="{average_line_color}")
|
||
abline(h=mean(w,na.rm=T),lty=3,col="{average_line_color}")
|
||
text(182.5,52,"{average}",adj=0,font=3,cex={average_font_size},col="{average_font_color}")
|
||
|
||
mtext("{title}",3,adj=0,line=2,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0,line=0,cex={title_font_size}*0.6,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=1,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1.2,adj=0.535,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(mai=c(0.85,1,0.25,0.25),omi=c(1,0.5,1,0.5),las=1)
|
||
|
||
plot(type="n",xlab="",ylab="{y_name}",h,w,xlim=c(160,220),ylim=c(50,125),axes=F,cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
axis(1,col=par("bg"),col.ticks="{x_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{x_scale_font_color}",cex.axis={x_scale_font_size})
|
||
axis(2,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
|
||
myC1<-rgb(255,0,210,maxColorValue=255)
|
||
myC2<-rgb(0,208,226,100,maxColorValue=255)
|
||
|
||
myP1<-subset(myData[c("h","w")],w>20*(h/100*h/100) & w<25*(h/100*h/100))
|
||
myP2<-subset(myData[c("h","w")],w<20*(h/100*h/100))
|
||
myP3<-subset(myData[c("h","w")],w>25*(h/100*h/100))
|
||
|
||
myDes2<-as.matrix(subset(name,w<20*(h/100*h/100)))
|
||
myDes3<-as.matrix(subset(name,w>25*(h/100*h/100)))
|
||
|
||
symbols(myP1,bg='{color_1}',fg="white",circles=rep(1,nrow(myP1)),inches=0.25,add=T)
|
||
symbols(myP2,bg='{color_2}',fg="white",circles=rep(1,nrow(myP2)),inches=0.25,add=T)
|
||
symbols(myP3,bg='{color_2}',fg="white",circles=rep(1,nrow(myP3)),inches=0.25,add=T)
|
||
|
||
text(myP2,myDes2,cex={celebrities_size},pos=1,offset=1.1,col="{celebrities_color}")
|
||
text(myP3,myDes3,cex={celebrities_size},pos=3,offset=1.1,col="{celebrities_color}")
|
||
|
||
curve(20*(x/100*x/100),xlim=c(160,220),add=T)
|
||
curve(25*(x/100*x/100),xlim=c(160,220),add=T)
|
||
|
||
abline(v=mean(h,na.rm=T),lty=3,col="{average_line_color}")
|
||
abline(h=mean(w,na.rm=T),lty=3,col="{average_line_color}")
|
||
text(182.5,52,"{average}",adj=0,font=3,cex={average_font_size},col="{average_font_color}")
|
||
|
||
mtext("{title}",3,adj=0,line=2,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0,line=0,cex={title_font_size}*0.6,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=1,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1.2,adj=0.535,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(mai=c(0.85,1,0.25,0.25),omi=c(1,0.5,1,0.5),las=1)
|
||
|
||
plot(type="n",xlab="",ylab="{y_name}",h,w,xlim=c(160,220),ylim=c(50,125),axes=F,cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
axis(1,col=par("bg"),col.ticks="{x_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{x_scale_font_color}",cex.axis={x_scale_font_size})
|
||
axis(2,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
|
||
myC1<-rgb(255,0,210,maxColorValue=255)
|
||
myC2<-rgb(0,208,226,100,maxColorValue=255)
|
||
|
||
myP1<-subset(myData[c("h","w")],w>20*(h/100*h/100) & w<25*(h/100*h/100))
|
||
myP2<-subset(myData[c("h","w")],w<20*(h/100*h/100))
|
||
myP3<-subset(myData[c("h","w")],w>25*(h/100*h/100))
|
||
|
||
myDes2<-as.matrix(subset(name,w<20*(h/100*h/100)))
|
||
myDes3<-as.matrix(subset(name,w>25*(h/100*h/100)))
|
||
|
||
symbols(myP1,bg='{color_1}',fg="white",circles=rep(1,nrow(myP1)),inches=0.25,add=T)
|
||
symbols(myP2,bg='{color_2}',fg="white",circles=rep(1,nrow(myP2)),inches=0.25,add=T)
|
||
symbols(myP3,bg='{color_2}',fg="white",circles=rep(1,nrow(myP3)),inches=0.25,add=T)
|
||
|
||
text(myP2,myDes2,cex={celebrities_size},pos=1,offset=1.1,col="{celebrities_color}")
|
||
text(myP3,myDes3,cex={celebrities_size},pos=3,offset=1.1,col="{celebrities_color}")
|
||
|
||
curve(20*(x/100*x/100),xlim=c(160,220),add=T)
|
||
curve(25*(x/100*x/100),xlim=c(160,220),add=T)
|
||
|
||
abline(v=mean(h,na.rm=T),lty=3,col="{average_line_color}")
|
||
abline(h=mean(w,na.rm=T),lty=3,col="{average_line_color}")
|
||
text(182.5,52,"{average}",adj=0,font=3,cex={average_font_size},col="{average_font_color}")
|
||
|
||
mtext("{title}",3,adj=0,line=2,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0,line=0,cex={title_font_size}*0.6,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=1,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1.2,adj=0.535,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size, font_size
|
||
|
||
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color, font_color, line_color, color
|
||
# 示例文件 Scatter_Plot_Variant_5_Connected_Points.xlsx
|
||
|
||
def Scatter_Plot_Variant_5_Connected_Points(file_path, char_out_path, out_path, title, caption, subtitle,
|
||
title_font_size,
|
||
x_name, x_font_size, x_scale_font_size, y_name, y_font_size,
|
||
y_scale_font_size, font_size,
|
||
title_font_color, x_font_color, x_scale_font_color, y_font_color,
|
||
y_scale_font_color, font_color,
|
||
line_color, color):
|
||
code = f"""
|
||
library(gdata)
|
||
myData<-read.xls('{file_path}',encoding="latin1")
|
||
names(myData) <- c("Year","GDP","LEXP")
|
||
myData<-myData[myData$Year>=1985, ]
|
||
attach(myData)
|
||
n<-nrow(myData)
|
||
grGDP<-vector()
|
||
grLEXP<-vector()
|
||
|
||
for (i in 2:n)
|
||
{{
|
||
grGDP[i]<-(GDP[i]-GDP[i-1])/GDP[i-1]
|
||
grLEXP[i]<-(LEXP[i]-LEXP[i-1])/LEXP[i-1]
|
||
}}
|
||
myData$grGDP<-grGDP*100
|
||
myData$grLEXP<-grLEXP*100
|
||
myData<-myData[2:n, ]
|
||
|
||
n<-nrow(myData)
|
||
|
||
t <- 1:n
|
||
ts <- seq(1, n, by = 1/10)
|
||
xs <- splinefun(t, myData$grGDP)(ts)
|
||
ys <- splinefun(t, myData$grLEXP)(ts)
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(mai=c(1.1,1.25,0.15,0),omi=c(1,0.5,1,0.5), mgp=c(4.5,1,0),las=1)
|
||
|
||
plot(myData$grGDP, myData$grLEXP, type="n", xlab="", ylab="{y_name}", cex.lab={y_font_size}, axes=F, col.lab="{y_font_color}")
|
||
axis(1,col=par("bg"),col.ticks="{x_scale_font_color}",lwd.ticks=0.5,tck=-0.025,cex.axis={x_scale_font_size},col.axis="{x_scale_font_color}")
|
||
axis(2,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,cex.axis={y_scale_font_size},col.axis="{y_scale_font_color}")
|
||
|
||
lines(xs, ys,lwd=7,col="{line_color}")
|
||
for (i in 1:n)
|
||
{{
|
||
symbols(myData$grGDP[i],myData$grLEXP[i],bg="{color}",fg="{font_color}",circles=1,inches=0.25,add=T)
|
||
text(myData$grGDP[i],myData$grLEXP[i], myData$Year[i],col="{font_color}",cex={font_size})
|
||
}}
|
||
|
||
mtext("{title}",3,adj=0,line=1.5,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0,line=-0.25,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1.05,adj=0.57,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(mai=c(1.1,1.25,0.15,0),omi=c(1,0.5,1,0.5), mgp=c(4.5,1,0),las=1)
|
||
|
||
plot(myData$grGDP, myData$grLEXP, type="n", xlab="", ylab="{y_name}", cex.lab={y_font_size}, axes=F, col.lab="{y_font_color}")
|
||
axis(1,col=par("bg"),col.ticks="{x_scale_font_color}",lwd.ticks=0.5,tck=-0.025,cex.axis={x_scale_font_size},col.axis="{x_scale_font_color}")
|
||
axis(2,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,cex.axis={y_scale_font_size},col.axis="{y_scale_font_color}")
|
||
|
||
lines(xs, ys,lwd=7,col="{line_color}")
|
||
for (i in 1:n)
|
||
{{
|
||
symbols(myData$grGDP[i],myData$grLEXP[i],bg="{color}",fg="{font_color}",circles=1,inches=0.25,add=T)
|
||
text(myData$grGDP[i],myData$grLEXP[i], myData$Year[i],col="{font_color}",cex={font_size})
|
||
}}
|
||
|
||
mtext("{title}",3,adj=0,line=1.5,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0,line=-0.25,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1.05,adj=0.57,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(mai=c(1.1,1.25,0.15,0),omi=c(1,0.5,1,0.5), mgp=c(4.5,1,0),las=1)
|
||
|
||
plot(myData$grGDP, myData$grLEXP, type="n", xlab="", ylab="{y_name}", cex.lab={y_font_size}, axes=F, col.lab="{y_font_color}")
|
||
axis(1,col=par("bg"),col.ticks="{x_scale_font_color}",lwd.ticks=0.5,tck=-0.025,cex.axis={x_scale_font_size},col.axis="{x_scale_font_color}")
|
||
axis(2,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,cex.axis={y_scale_font_size},col.axis="{y_scale_font_color}")
|
||
|
||
lines(xs, ys,lwd=7,col="{line_color}")
|
||
for (i in 1:n)
|
||
{{
|
||
symbols(myData$grGDP[i],myData$grLEXP[i],bg="{color}",fg="{font_color}",circles=1,inches=0.25,add=T)
|
||
text(myData$grGDP[i],myData$grLEXP[i], myData$Year[i],col="{font_color}",cex={font_size})
|
||
}}
|
||
|
||
mtext("{title}",3,adj=0,line=1.5,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0,line=-0.25,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1.05,adj=0.57,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(mai=c(1.1,1.25,0.15,0),omi=c(1,0.5,1,0.5), mgp=c(4.5,1,0),las=1)
|
||
|
||
plot(myData$grGDP, myData$grLEXP, type="n", xlab="", ylab="{y_name}", cex.lab={y_font_size}, axes=F, col.lab="{y_font_color}")
|
||
axis(1,col=par("bg"),col.ticks="{x_scale_font_color}",lwd.ticks=0.5,tck=-0.025,cex.axis={x_scale_font_size},col.axis="{x_scale_font_color}")
|
||
axis(2,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,cex.axis={y_scale_font_size},col.axis="{y_scale_font_color}")
|
||
|
||
lines(xs, ys,lwd=7,col="{line_color}")
|
||
for (i in 1:n)
|
||
{{
|
||
symbols(myData$grGDP[i],myData$grLEXP[i],bg="{color}",fg="{font_color}",circles=1,inches=0.25,add=T)
|
||
text(myData$grGDP[i],myData$grLEXP[i], myData$Year[i],col="{font_color}",cex={font_size})
|
||
}}
|
||
|
||
mtext("{title}",3,adj=0,line=1.5,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0,line=-0.25,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1,cex={title_font_size}*0.4,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-1.05,adj=0.57,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size, font_size, character, time_size
|
||
|
||
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color, font_color, time_color
|
||
# 示例文件 Scatter_Plot_With_User_Defined_Symbols.xlsx
|
||
|
||
def Scatter_Plot_With_User_Defined_Symbols(file_path, char_out_path, out_path, title, caption, subtitle,
|
||
title_font_size,
|
||
x_name, x_font_size, x_scale_font_size, y_name, y_font_size,
|
||
y_scale_font_size, font_size,
|
||
character, time_size,
|
||
title_font_color, x_font_color, x_scale_font_color, y_font_color,
|
||
y_scale_font_color, font_color,
|
||
time_color):
|
||
code = f"""
|
||
library(maptools)
|
||
library(gdata)
|
||
myData<-read.xls("{file_path}", encoding="latin1")
|
||
names(myData) <- c("WarNum","WarName","WarType","CcodeA","SideA","CcodeB","SideB","Intnl","StartMonth1","StartDay1","StartYear1","EndMonth1","EndDay1","EndYear1","StartMonth2","StartDay2","StartYear2","EndMonth2","EndDay2","EndYear2","TransFrom","WhereFought","Initiator","Outcome","TransTo","SideADeaths","SideBDeaths","Version")
|
||
mySelection<-subset(myData, myData$StartYear1>=1995 & myData$SideADeaths > 0 & myData$SideADeaths < 2000 & myData$SideBDeaths > 0 & myData$SideBDeaths < 4000)
|
||
attach(mySelection)
|
||
myColour<-"{font_color}"
|
||
myN<-nrow(mySelection)
|
||
h<-rep(0, myN)
|
||
v<-rep(0, myN)
|
||
myOffset<-cbind(h, v)
|
||
mySelection[, c("WarName", "StartYear1", "SideADeaths", "SideBDeaths")]
|
||
myOffset[1, "h"]<--400
|
||
myOffset[5, "h"]<-232
|
||
myOffset[4, "h"]<--275
|
||
myOffset[2, "h"]<-270; myOffset[2, "v"]<-100;
|
||
myOffset[13, "h"]<--275
|
||
myOffset[12, "h"]<--300
|
||
myX<-as.numeric(SideADeaths)
|
||
myY<-as.numeric(SideBDeaths)
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(omi=c(0.5,0.5,0,0),mai=c(0.5,1.25,0,0.25),las=1)
|
||
plot(myX, myY, typ="n", xlab="", ylab="", axes=F, xlim=c(0, 2000), ylim=c(0, 4000))
|
||
axis(1,col=par("bg"),col.ticks="{x_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{x_scale_font_color}",cex.axis="{x_scale_font_size}")
|
||
axis(2,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis="{y_scale_font_size}")
|
||
text(myX+130+myOffset[, "h"], myY-180+myOffset[, "v"], paste(WarName, StartYear1, sep=" "), cex={time_size}, xpd=T, col="{time_color}")
|
||
|
||
mtext(side=1, "{x_name}", adj=0.5, line=3, cex={x_font_size}, col="{x_font_color}")
|
||
mtext(side=2, "{y_name}", las=0, adj=0.5, line=4, cex={y_font_size}, col="{y_font_color}")
|
||
|
||
mtext("{title}",3,adj=1,line=-3,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=1,line=-5,cex={title_font_size}*0.6,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=0,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
text(myX, myY, "{character}", col=myColour, cex={font_size}, xpd=T)
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(omi=c(0.5,0.5,0,0),mai=c(0.5,1.25,0,0.25),las=1)
|
||
plot(myX, myY, typ="n", xlab="", ylab="", axes=F, xlim=c(0, 2000), ylim=c(0, 4000))
|
||
axis(1,col=par("bg"),col.ticks="{x_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{x_scale_font_color}",cex.axis="{x_scale_font_size}")
|
||
axis(2,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis="{y_scale_font_size}")
|
||
text(myX+130+myOffset[, "h"], myY-180+myOffset[, "v"], paste(WarName, StartYear1, sep=" "), cex={time_size}, xpd=T, col="{time_color}")
|
||
|
||
mtext(side=1, "{x_name}", adj=0.5, line=3, cex={x_font_size}, col="{x_font_color}")
|
||
mtext(side=2, "{y_name}", las=0, adj=0.5, line=4, cex={y_font_size}, col="{y_font_color}")
|
||
|
||
mtext("{title}",3,adj=1,line=-3,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=1,line=-5,cex={title_font_size}*0.6,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=0,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
text(myX, myY, "{character}", col=myColour, cex={font_size}, xpd=T)
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(omi=c(0.5,0.5,0,0),mai=c(0.5,1.25,0,0.25),las=1)
|
||
plot(myX, myY, typ="n", xlab="", ylab="", axes=F, xlim=c(0, 2000), ylim=c(0, 4000))
|
||
axis(1,col=par("bg"),col.ticks="{x_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{x_scale_font_color}",cex.axis="{x_scale_font_size}")
|
||
axis(2,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis="{y_scale_font_size}")
|
||
text(myX+130+myOffset[, "h"], myY-180+myOffset[, "v"], paste(WarName, StartYear1, sep=" "), cex={time_size}, xpd=T, col="{time_color}")
|
||
|
||
mtext(side=1, "{x_name}", adj=0.5, line=3, cex={x_font_size}, col="{x_font_color}")
|
||
mtext(side=2, "{y_name}", las=0, adj=0.5, line=4, cex={y_font_size}, col="{y_font_color}")
|
||
|
||
mtext("{title}",3,adj=1,line=-3,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=1,line=-5,cex={title_font_size}*0.6,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=0,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
text(myX, myY, "{character}", col=myColour, cex={font_size}, xpd=T)
|
||
dev.off()
|
||
|
||
|
||
svg("{out_path}.svg")
|
||
par(omi=c(0.5,0.5,0,0),mai=c(0.5,1.25,0,0.25),las=1)
|
||
plot(myX, myY, typ="n", xlab="", ylab="", axes=F, xlim=c(0, 2000), ylim=c(0, 4000))
|
||
axis(1,col=par("bg"),col.ticks="{x_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{x_scale_font_color}",cex.axis="{x_scale_font_size}")
|
||
axis(2,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis="{y_scale_font_size}")
|
||
text(myX+130+myOffset[, "h"], myY-180+myOffset[, "v"], paste(WarName, StartYear1, sep=" "), cex={time_size}, xpd=T, col="{time_color}")
|
||
|
||
mtext(side=1, "{x_name}", adj=0.5, line=3, cex={x_font_size}, col="{x_font_color}")
|
||
mtext(side=2, "{y_name}", las=0, adj=0.5, line=4, cex={y_font_size}, col="{y_font_color}")
|
||
|
||
mtext("{title}",3,adj=1,line=-3,cex={title_font_size},col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=1,line=-5,cex={title_font_size}*0.6,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=1,adj=0,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
text(myX, myY, "{character}", col=myColour, cex={font_size}, xpd=T)
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size, annotation, number_size
|
||
|
||
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color, number_font_color, color_1, color_2, color_3, line_color
|
||
# 示例文件 Scatter_Plot_with_Few_Points.csv
|
||
|
||
def Scatter_Plot_with_Few_Points(file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name,
|
||
x_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size, annotation,
|
||
number_size,
|
||
title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color,
|
||
number_font_color, color_1,
|
||
color_2, color_3, line_color):
|
||
color_1 = color_1[4:-1]
|
||
|
||
color_2 = color_2[4:-1]
|
||
|
||
color_3 = color_3[4:-1]
|
||
|
||
code = f"""
|
||
datas <- read.csv("{file_path}", header = FALSE)
|
||
|
||
myValue <- datas[,1]
|
||
myRevenue <- datas[,2]
|
||
myProfit <- datas[,3]
|
||
|
||
myC1<-rgb({color_2},maxColorValue=255)
|
||
myC2<-rgb({color_3},maxColorValue=255)
|
||
myC3<-"grey"
|
||
myC4<-rgb({color_1},maxColorValue=255)
|
||
|
||
names<-c("BMW:\n44,6 Bn.","Daimler:\n45,5 Bn.","","Facebook:\n75-100 Bn.")
|
||
|
||
pdf("{out_path}.pdf")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(mai=c(2,1,1,1),omi=c(0,0,0,0),xpd=T,las=1)
|
||
|
||
plot(myRevenue,myProfit,axes=F,type="n",xlab="",ylab="{y_name}",xlim=c(-20,100),ylim=c(-1,6),cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
for (i in 1:3)
|
||
{{
|
||
arrows(myRevenue[i],-1,myRevenue[i],myProfit[i],length=0.10,lty="dotted",angle=10,code=0,lwd=1,col="{line_color}")
|
||
arrows(-20,myProfit[i],myRevenue[i],myProfit[i],length=0.10,lty="dotted",angle=10,code=0,lwd=1,col="{line_color}")
|
||
}}
|
||
points(myRevenue,myProfit,pch=19,cex=myValue/2.6,col=c(myC1,myC2,myC3,myC4))
|
||
text(myRevenue,myProfit,names,col="{number_font_color}",cex={number_size})
|
||
axis(1,at=c(2.5,60.5,97.8),labels=c("2.5*","60.5","97.8"),cex.axis={x_scale_font_size},col.axis="{x_scale_font_color}",col="{x_scale_font_color}")
|
||
axis(2,at=c(1,4.8),labels=c("1.0","4.8\n4.7"),cex.axis={y_scale_font_size},col.axis="{y_scale_font_color}",col="{y_scale_font_color}")
|
||
text(-25.5,5.08,"**",col="{y_scale_font_color}")
|
||
text(-26.5,1.08,"*",col="{y_scale_font_color}")
|
||
|
||
mtext(line=1,"{title}",cex={title_font_size},adj=0,col="{title_font_color}")
|
||
mtext(line=-2.5,"{subtitle}",cex={title_font_size}*0.6,adj=0,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=6.5,"{caption}",cex={title_font_size}*0.6,adj=1,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=4.5,"{annotation}",cex={title_font_size}*0.6,adj=0,col="{title_font_color}")
|
||
mtext("{x_name}",1,3,adj=0.5,cex={x_font_size},col="{x_font_color}")
|
||
par(mai=c(2,1,1,1),omi=c(0,0,0,0),xpd=T,las=1)
|
||
|
||
plot(myRevenue,myProfit,axes=F,type="n",xlab="",ylab="{y_name}",xlim=c(-20,100),ylim=c(-1,6),cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
for (i in 1:3)
|
||
{{
|
||
arrows(myRevenue[i],-1,myRevenue[i],myProfit[i],length=0.10,lty="dotted",angle=10,code=0,lwd=1,col="{line_color}")
|
||
arrows(-20,myProfit[i],myRevenue[i],myProfit[i],length=0.10,lty="dotted",angle=10,code=0,lwd=1,col="{line_color}")
|
||
}}
|
||
points(myRevenue,myProfit,pch=19,cex=myValue/2.6,col=c(myC1,myC2,myC3,myC4))
|
||
text(myRevenue,myProfit,names,col="{number_font_color}",cex={number_size})
|
||
axis(1,at=c(2.5,60.5,97.8),labels=c("2.5*","60.5","97.8"),cex.axis={x_scale_font_size},col.axis="{x_scale_font_color}",col="{x_scale_font_color}")
|
||
axis(2,at=c(1,4.8),labels=c("1.0","4.8\n4.7"),cex.axis={y_scale_font_size},col.axis="{y_scale_font_color}",col="{y_scale_font_color}")
|
||
text(-25.5,5.08,"**",col="{y_scale_font_color}")
|
||
text(-26.5,1.08,"*",col="{y_scale_font_color}")
|
||
|
||
mtext(line=1,"{title}",cex={title_font_size},adj=0,col="{title_font_color}")
|
||
mtext(line=-2.5,"{subtitle}",cex={title_font_size}*0.6,adj=0,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=6.5,"{caption}",cex={title_font_size}*0.6,adj=1,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=4.5,"{annotation}",cex={title_font_size}*0.6,adj=0,col="{title_font_color}")
|
||
mtext("{x_name}",1,3,adj=0.5,cex={x_font_size},col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(mai=c(2,1,1,1),omi=c(0,0,0,0),xpd=T,las=1)
|
||
|
||
plot(myRevenue,myProfit,axes=F,type="n",xlab="",ylab="{y_name}",xlim=c(-20,100),ylim=c(-1,6),cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
for (i in 1:3)
|
||
{{
|
||
arrows(myRevenue[i],-1,myRevenue[i],myProfit[i],length=0.10,lty="dotted",angle=10,code=0,lwd=1,col="{line_color}")
|
||
arrows(-20,myProfit[i],myRevenue[i],myProfit[i],length=0.10,lty="dotted",angle=10,code=0,lwd=1,col="{line_color}")
|
||
}}
|
||
points(myRevenue,myProfit,pch=19,cex=myValue/2.6,col=c(myC1,myC2,myC3,myC4))
|
||
text(myRevenue,myProfit,names,col="{number_font_color}",cex={number_size})
|
||
axis(1,at=c(2.5,60.5,97.8),labels=c("2.5*","60.5","97.8"),cex.axis={x_scale_font_size},col.axis="{x_scale_font_color}",col="{x_scale_font_color}")
|
||
axis(2,at=c(1,4.8),labels=c("1.0","4.8\n4.7"),cex.axis={y_scale_font_size},col.axis="{y_scale_font_color}",col="{y_scale_font_color}")
|
||
text(-25.5,5.08,"**",col="{y_scale_font_color}")
|
||
text(-26.5,1.08,"*",col="{y_scale_font_color}")
|
||
|
||
mtext(line=1,"{title}",cex={title_font_size},adj=0,col="{title_font_color}")
|
||
mtext(line=-2.5,"{subtitle}",cex={title_font_size}*0.6,adj=0,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=6.5,"{caption}",cex={title_font_size}*0.6,adj=1,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=4.5,"{annotation}",cex={title_font_size}*0.6,adj=0,col="{title_font_color}")
|
||
mtext("{x_name}",1,3,adj=0.5,cex={x_font_size},col="{x_font_color}")
|
||
dev.off()
|
||
|
||
|
||
svg("{out_path}.svg")
|
||
par(mai=c(2,1,1,1),omi=c(0,0,0,0),xpd=T,las=1)
|
||
|
||
plot(myRevenue,myProfit,axes=F,type="n",xlab="",ylab="{y_name}",xlim=c(-20,100),ylim=c(-1,6),cex.lab={y_font_size},col.lab="{y_font_color}")
|
||
for (i in 1:3)
|
||
{{
|
||
arrows(myRevenue[i],-1,myRevenue[i],myProfit[i],length=0.10,lty="dotted",angle=10,code=0,lwd=1,col="{line_color}")
|
||
arrows(-20,myProfit[i],myRevenue[i],myProfit[i],length=0.10,lty="dotted",angle=10,code=0,lwd=1,col="{line_color}")
|
||
}}
|
||
points(myRevenue,myProfit,pch=19,cex=myValue/2.6,col=c(myC1,myC2,myC3,myC4))
|
||
text(myRevenue,myProfit,names,col="{number_font_color}",cex={number_size})
|
||
axis(1,at=c(2.5,60.5,97.8),labels=c("2.5*","60.5","97.8"),cex.axis={x_scale_font_size},col.axis="{x_scale_font_color}",col="{x_scale_font_color}")
|
||
axis(2,at=c(1,4.8),labels=c("1.0","4.8\n4.7"),cex.axis={y_scale_font_size},col.axis="{y_scale_font_color}",col="{y_scale_font_color}")
|
||
text(-25.5,5.08,"**",col="{y_scale_font_color}")
|
||
text(-26.5,1.08,"*",col="{y_scale_font_color}")
|
||
|
||
mtext(line=1,"{title}",cex={title_font_size},adj=0,col="{title_font_color}")
|
||
mtext(line=-2.5,"{subtitle}",cex={title_font_size}*0.6,adj=0,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=6.5,"{caption}",cex={title_font_size}*0.6,adj=1,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=4.5,"{annotation}",cex={title_font_size}*0.6,adj=0,col="{title_font_color}")
|
||
mtext("{x_name}",1,3,adj=0.5,cex={x_font_size},col="{x_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path, char_out_path, out_path, title, caption, title_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size, name_1, name_2
|
||
|
||
|
||
# 高级参数 title_font_color, x_scale_font_color, y_font_color, y_scale_font_color, color_1, color_2, name_color
|
||
# 示例文件 Seasonal_Ranges_Panel.xlsx
|
||
|
||
def Seasonal_Ranges_Panel(file_path, char_out_path, out_path, title, caption, title_font_size, x_scale_font_size,
|
||
y_name, y_font_size,
|
||
y_scale_font_size, name_1, name_2,
|
||
title_font_color, x_scale_font_color, y_font_color, y_scale_font_color, color_1, color_2,
|
||
name_font_size, name_color):
|
||
code = f"""
|
||
library(gplots)
|
||
library(gdata)
|
||
myData<-read.xls('{file_path}', encoding="latin1")
|
||
attach(myData)
|
||
myLines<-c(-5,0,5,10,15,20,25,30)
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(omi=c(0.25,0.25,0.5,0.25),mai=c(0.45,0.35,0.5,0),mfcol=c(1,2),las=1)
|
||
myT1<-barplot2(t(cbind(NY_min,NY_max-NY_min)),col=c(NA,"{color_1}"),border=NA,names.arg=Month,ylim=c(-5,35),panel.first=abline(h=myLines,col="grey",lwd=1,lty="dotted"),axes=F,cex.names="{x_scale_font_size}",col.axis="{x_scale_font_color}")
|
||
for (i in 1:length(myLines)) {{text(-0.8,myLines[i]+1.1,myLines[i],xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})}}
|
||
text(0.25,33,"{y_name}",xpd=T,cex={y_font_size},col="{y_font_color}")
|
||
mtext(side=3,"{name_1}",cex={name_font_size},col="{name_color}")
|
||
myT2<-barplot2(t(cbind(MAJ_min,MAJ_max-MAJ_min)),col=c(NA,"{color_2}"),border=NA,names.arg=Month,ylim=c(-5,35),panel.first=abline(h=myLines,col="grey",lwd=1,lty="dotted"),axes=F,cex.names="{x_scale_font_size}",col.axis="{x_scale_font_color}")
|
||
|
||
mtext(side=3,"{name_2}",cex={name_font_size},col="{name_color}")
|
||
mtext(side=3,"{title}",cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext(side=1,"{caption}",line=-0.4,cex={title_font_size}*0.6,adj=1,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(omi=c(0.25,0.25,0.5,0.25),mai=c(0.45,0.35,0.5,0),mfcol=c(1,2),las=1)
|
||
myT1<-barplot2(t(cbind(NY_min,NY_max-NY_min)),col=c(NA,"{color_1}"),border=NA,names.arg=Month,ylim=c(-5,35),panel.first=abline(h=myLines,col="grey",lwd=1,lty="dotted"),axes=F,cex.names="{x_scale_font_size}",col.axis="{x_scale_font_color}")
|
||
for (i in 1:length(myLines)) {{text(-0.8,myLines[i]+1.1,myLines[i],xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})}}
|
||
text(0.25,33,"{y_name}",xpd=T,cex={y_font_size},col="{y_font_color}")
|
||
mtext(side=3,"{name_1}",cex={name_font_size},col="{name_color}")
|
||
myT2<-barplot2(t(cbind(MAJ_min,MAJ_max-MAJ_min)),col=c(NA,"{color_2}"),border=NA,names.arg=Month,ylim=c(-5,35),panel.first=abline(h=myLines,col="grey",lwd=1,lty="dotted"),axes=F,cex.names="{x_scale_font_size}",col.axis="{x_scale_font_color}")
|
||
|
||
mtext(side=3,"{name_2}",cex={name_font_size},col="{name_color}")
|
||
mtext(side=3,"{title}",cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext(side=1,"{caption}",line=-0.4,cex={title_font_size}*0.6,adj=1,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=408)
|
||
par(omi=c(0.25,0.25,0.5,0.25),mai=c(0.45,0.35,0.5,0),mfcol=c(1,2),las=1)
|
||
myT1<-barplot2(t(cbind(NY_min,NY_max-NY_min)),col=c(NA,"{color_1}"),border=NA,names.arg=Month,ylim=c(-5,35),panel.first=abline(h=myLines,col="grey",lwd=1,lty="dotted"),axes=F,cex.names="{x_scale_font_size}",col.axis="{x_scale_font_color}")
|
||
for (i in 1:length(myLines)) {{text(-0.8,myLines[i]+1.1,myLines[i],xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})}}
|
||
text(0.25,33,"{y_name}",xpd=T,cex={y_font_size},col="{y_font_color}")
|
||
mtext(side=3,"{name_1}",cex={name_font_size},col="{name_color}")
|
||
myT2<-barplot2(t(cbind(MAJ_min,MAJ_max-MAJ_min)),col=c(NA,"{color_2}"),border=NA,names.arg=Month,ylim=c(-5,35),panel.first=abline(h=myLines,col="grey",lwd=1,lty="dotted"),axes=F,cex.names="{x_scale_font_size}",col.axis="{x_scale_font_color}")
|
||
|
||
mtext(side=3,"{name_2}",cex={name_font_size},col="{name_color}")
|
||
mtext(side=3,"{title}",cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext(side=1,"{caption}",line=-0.4,cex={title_font_size}*0.6,adj=1,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(omi=c(0.25,0.25,0.5,0.25),mai=c(0.45,0.35,0.5,0),mfcol=c(1,2),las=1)
|
||
myT1<-barplot2(t(cbind(NY_min,NY_max-NY_min)),col=c(NA,"{color_1}"),border=NA,names.arg=Month,ylim=c(-5,35),panel.first=abline(h=myLines,col="grey",lwd=1,lty="dotted"),axes=F,cex.names="{x_scale_font_size}",col.axis="{x_scale_font_color}")
|
||
for (i in 1:length(myLines)) {{text(-0.8,myLines[i]+1.1,myLines[i],xpd=T,col="{y_scale_font_color}",cex={y_scale_font_size})}}
|
||
text(0.25,33,"{y_name}",xpd=T,cex={y_font_size},col="{y_font_color}")
|
||
mtext(side=3,"{name_1}",cex={name_font_size},col="{name_color}")
|
||
myT2<-barplot2(t(cbind(MAJ_min,MAJ_max-MAJ_min)),col=c(NA,"{color_2}"),border=NA,names.arg=Month,ylim=c(-5,35),panel.first=abline(h=myLines,col="grey",lwd=1,lty="dotted"),axes=F,cex.names="{x_scale_font_size}",col.axis="{x_scale_font_color}")
|
||
|
||
mtext(side=3,"{name_2}",cex={name_font_size},col="{name_color}")
|
||
mtext(side=3,"{title}",cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext(side=1,"{caption}",line=-0.4,cex={title_font_size}*0.6,adj=1,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_scale_font_size, y_name, y_font_size, y_scale_font_size, legend_1, legend_2, legend_size
|
||
|
||
|
||
# 高级参数 title_font_color, x_scale_font_color, y_font_color, y_scale_font_color, color_1, color_2, line_color, legend_color
|
||
# 示例文件 Seasonal_Ranges_Stacked.xlsx
|
||
|
||
def Seasonal_Ranges_Stacked(file_path, char_out_path, out_path, title, caption, subtitle, title_font_size,
|
||
x_scale_font_size,
|
||
y_name, y_font_size, y_scale_font_size, legend_1, legend_2, legend_size,
|
||
title_font_color, x_scale_font_color, y_font_color, y_scale_font_color, color_1, color_2,
|
||
line_color,
|
||
legend_color):
|
||
code = f"""
|
||
library(gdata)
|
||
myData<-read.xls('{file_path}',encoding="latin1")
|
||
names(myData) <- c("Month","NY_min","NY_max","MAJ_min","MAJ_max")
|
||
myLines<-c(-5,0,5,10,15,20,25,30)
|
||
attach(myData)
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(omi=c(0.25,0,0.75,0.25),mai=c(0.5,2,0.5,2),las=1)
|
||
|
||
myT1<-barplot(t(cbind(NY_min,NY_max-NY_min)),col=c("white","{color_2}"),border=NA,ylim=c(-5,35),axes=F,axisnames=F)
|
||
myT2<-barplot(t(cbind(MAJ_min,MAJ_max-MAJ_min)),col=c("white","{color_1}"),border=NA,add=T,axes=F,names.arg=Month,col.axis="{x_scale_font_color}",cex.names={x_scale_font_size})
|
||
axis(2,at=myLines,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
abline(h=myLines,col="white",lwd=2)
|
||
abline(v=seq(2.5,28.8,by=2.4),col="{line_color}")
|
||
text(-0.95,34,"{y_name}",xpd=T,cex={y_font_size},col="{y_font_color}")
|
||
legend(18,18,c("{legend_2}","{legend_1}"),col=c("{color_2}","{color_1}"),pch=15,bty="n",xjust=1,cex={legend_size},pt.cex={legend_size},xpd=T,text.col="{legend_color}")
|
||
|
||
mtext(side=3,"{title}",cex={title_font_size},adj=0.1,outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0,line=-0.25,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext(side=1,line=-1,"{caption}",cex={title_font_size}*0.4,adj=1,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(omi=c(0.25,0,0.75,0.25),mai=c(0.5,2,0.5,2),las=1)
|
||
|
||
myT1<-barplot(t(cbind(NY_min,NY_max-NY_min)),col=c("white","{color_2}"),border=NA,ylim=c(-5,35),axes=F,axisnames=F)
|
||
myT2<-barplot(t(cbind(MAJ_min,MAJ_max-MAJ_min)),col=c("white","{color_1}"),border=NA,add=T,axes=F,names.arg=Month,col.axis="{x_scale_font_color}",cex.names={x_scale_font_size})
|
||
axis(2,at=myLines,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
abline(h=myLines,col="white",lwd=2)
|
||
abline(v=seq(2.5,28.8,by=2.4),col="{line_color}")
|
||
text(-0.95,34,"{y_name}",xpd=T,cex={y_font_size},col="{y_font_color}")
|
||
legend(18,18,c("{legend_2}","{legend_1}"),col=c("{color_2}","{color_1}"),pch=15,bty="n",xjust=1,cex={legend_size},pt.cex={legend_size},xpd=T,text.col="{legend_color}")
|
||
|
||
mtext(side=3,"{title}",cex={title_font_size},adj=0.1,outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0,line=-0.25,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext(side=1,line=-1,"{caption}",cex={title_font_size}*0.4,adj=1,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=408)
|
||
par(omi=c(0.25,0,0.75,0.25),mai=c(0.5,2,0.5,2),las=1)
|
||
|
||
myT1<-barplot(t(cbind(NY_min,NY_max-NY_min)),col=c("white","{color_2}"),border=NA,ylim=c(-5,35),axes=F,axisnames=F)
|
||
myT2<-barplot(t(cbind(MAJ_min,MAJ_max-MAJ_min)),col=c("white","{color_1}"),border=NA,add=T,axes=F,names.arg=Month,col.axis="{x_scale_font_color}",cex.names={x_scale_font_size})
|
||
axis(2,at=myLines,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
abline(h=myLines,col="white",lwd=2)
|
||
abline(v=seq(2.5,28.8,by=2.4),col="{line_color}")
|
||
text(-0.95,34,"{y_name}",xpd=T,cex={y_font_size},col="{y_font_color}")
|
||
legend(18,18,c("{legend_2}","{legend_1}"),col=c("{color_2}","{color_1}"),pch=15,bty="n",xjust=1,cex={legend_size},pt.cex={legend_size},xpd=T,text.col="{legend_color}")
|
||
|
||
mtext(side=3,"{title}",cex={title_font_size},adj=0.1,outer=T,col="{title_font_color}")
|
||
mtext(side=1,line=-1,"{caption}",cex={title_font_size}*0.4,adj=1,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(omi=c(0.25,0,0.75,0.25),mai=c(0.5,2,0.5,2),las=1)
|
||
|
||
myT1<-barplot(t(cbind(NY_min,NY_max-NY_min)),col=c("white","{color_2}"),border=NA,ylim=c(-5,35),axes=F,axisnames=F)
|
||
myT2<-barplot(t(cbind(MAJ_min,MAJ_max-MAJ_min)),col=c("white","{color_1}"),border=NA,add=T,axes=F,names.arg=Month,col.axis="{x_scale_font_color}",cex.names={x_scale_font_size})
|
||
axis(2,at=myLines,col=par("bg"),col.ticks="{y_scale_font_color}",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
abline(h=myLines,col="white",lwd=2)
|
||
abline(v=seq(2.5,28.8,by=2.4),col="{line_color}")
|
||
text(-0.95,34,"{y_name}",xpd=T,cex={y_font_size},col="{y_font_color}")
|
||
legend(18,18,c("{legend_2}","{legend_1}"),col=c("{color_2}","{color_1}"),pch=15,bty="n",xjust=1,cex={legend_size},pt.cex={legend_size},xpd=T,text.col="{legend_color}")
|
||
|
||
mtext(side=3,"{title}",cex={title_font_size},adj=0.1,outer=T,col="{title_font_color}")
|
||
mtext(side=1,line=-1,"{caption}",cex={title_font_size}*0.4,adj=1,font=3,outer=T,col="{title_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path, char_out_path, out_path, title, caption, title_font_size, x_name, x_font_size, x_scale_font_size, y_name, y_font_size, heading_font_size, date_size, percent_size, main_size, font_size
|
||
|
||
|
||
# 高级参数 font_color, x_font_color, x_scale_font_color, y_font_color, heading_font_color, line_color, title_font_color, date_color, percent_color, main_color
|
||
# 示例文件 Simplified_Gantt_Chart.xlsx
|
||
|
||
def Simplified_Gantt_Chart(file_path, char_out_path, out_path, title, caption, title_font_size, x_name, x_font_size,
|
||
x_scale_font_size,
|
||
y_name, y_font_size, heading_font_size, date_size, percent_size, main_size, font_size,
|
||
font_color, x_font_color, x_scale_font_color, y_font_color, heading_font_color, line_color,
|
||
title_font_color,
|
||
date_color, percent_color, main_color):
|
||
y_name = "\n".join(y_name)
|
||
|
||
code = f"""
|
||
library(gdata)
|
||
mySchedule<-read.xls('{file_path}',encoding="latin1")
|
||
names(mySchedule) <- c("Milestone","when","Group","what","from","to","Durance","who","done","PAN","PAG","AG_from","AG_to","Persons")
|
||
c0<-"black"; c1<-"green"; c2<-"red"; c3<-"blue"; c4<-"orange"; c5<-"brown"
|
||
myColour_done<-"grey"
|
||
myColour<-c(c0,c1,c1,c1,c0,c0,c2,c2,c2,c2,c0,c0,c3,c3,c3,c0,c0,c4,c4,c4,c0,c0,c5)
|
||
|
||
n<-nrow(mySchedule)
|
||
myScheduleData<-subset(mySchedule,nchar(as.character(mySchedule$from))>0)
|
||
myBegin<-min(as.Date(as.matrix(myScheduleData[,c('from','to')])))
|
||
myEnd<-max(as.Date(as.matrix(myScheduleData[,c('from','to')])))
|
||
attach(mySchedule)
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(lend=1,omi=c(0.25,1,1,0.25),mai=c(1,1.85,0.25,2.75),las=1)
|
||
|
||
plot(from,1:n,type="n",xlab="{x_name}",ylab="",axes=F,xlim=c(myBegin,myEnd),ylim=c(n,1), col.lab="{x_font_color}",cex.lab={x_font_size})
|
||
for (i in 1:n)
|
||
{{
|
||
if (nchar(as.character(Group[i]))>0)
|
||
{{
|
||
text(myBegin-2,i,Group[i],adj=1,xpd=T,cex={heading_font_size},col="{heading_font_color}")
|
||
}}
|
||
else if (nchar(as.character(what[i]))>0)
|
||
{{
|
||
x1<-as.Date(mySchedule[i,'from'])
|
||
x2<-as.Date(mySchedule[i,'to'])
|
||
x3<-x1+((x2-x1)*mySchedule[i,'done']/100)
|
||
x<-c(x1,x2)
|
||
x_done<-c(x1,x3)
|
||
y<-c(i,i)
|
||
segments(myBegin, i, myEnd, i, col="{line_color}")
|
||
lines(x,y,lwd=20,col=myColour[i])
|
||
points(myEnd+90,i,cex=(mySchedule[i,'Persons']*mySchedule[i,'Durance'])**0.5,pch=19,col=rgb(110,110,110,50,maxColorValue=255),xpd=T)
|
||
if (x3-x1>1) lines(x_done,y,lwd=20,col=myColour_done)
|
||
if (mySchedule[i,'PAG'] > 0)
|
||
{{
|
||
x4<-as.Date(mySchedule[i,'AG_from'])
|
||
x5<-as.Date(mySchedule[i,'AG_to'])
|
||
x_ag<-c(x4,x5)
|
||
rect(x4,i-0.75,x5,i+0.75,lwd=2)
|
||
}}
|
||
text(myBegin-2,i,what[i],adj=1,xpd=T,cex={main_size},col="{main_color}")
|
||
text(myEnd+25,i,paste(done[i],"%",sep=" "),adj=1,xpd=T,cex={percent_size},col="{percent_color}")
|
||
text(myEnd+35,i,paste(format(x1,format="%d/%m/%y"),"-",format(x2,format="%d/%m/%y"),sep=" "),adj=0,xpd=T,cex={date_size},col="{date_color}")
|
||
}}
|
||
else # Milestone
|
||
{{
|
||
x3<-as.Date(mySchedule[i,'when'])
|
||
myHalf<-(myEnd-myBegin)/2
|
||
if (x3-x1<myHalf)
|
||
{{
|
||
points(as.Date(mySchedule[i,'when']),i,pch=18,cex={font_size}*1.2,col="{font_color}")
|
||
text(as.Date(mySchedule[i,'when'])+5,i,Milestone[i],adj=0,xpd=T,cex={font_size},col="{font_color}")
|
||
}} else
|
||
{{
|
||
points(as.Date(mySchedule[i,'when']),i,pch=18,cex=1.25,col="red")
|
||
text(as.Date(mySchedule[i,'when'])-5,i,Milestone[i],adj=1,xpd=T,cex=0.75)
|
||
}}
|
||
}}
|
||
}}
|
||
axis(3,at=c(myBegin,myEnd),labels=c(format(myBegin,format="%d/%m/%Y"),format(myEnd,format="%d/%m/%Y")),col="{x_scale_font_color}",cex.axis={x_scale_font_size})
|
||
myToday<-Sys.Date()
|
||
abline(v=myToday)
|
||
mtext("today",1,line=0,at=myToday)
|
||
|
||
mtext("{title}",3,line=2,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext(paste("{caption}",format(myToday,format="%d/%m/%y"),sep=""),1,line=4,at=myEnd+20,cex={title_font_size}*0.6,font=3,col="{title_font_color}")
|
||
mtext("{y_name}",2,line=2.5,cex={y_font_size},font=3,outer=T,col="{y_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(lend=1,omi=c(0.25,1,1,0.25),mai=c(1,1.85,0.25,2.75),las=1)
|
||
|
||
plot(from,1:n,type="n",xlab="{x_name}",ylab="",axes=F,xlim=c(myBegin,myEnd),ylim=c(n,1), col.lab="{x_font_color}",cex.lab={x_font_size})
|
||
for (i in 1:n)
|
||
{{
|
||
if (nchar(as.character(Group[i]))>0)
|
||
{{
|
||
text(myBegin-2,i,Group[i],adj=1,xpd=T,cex={heading_font_size},col="{heading_font_color}")
|
||
}}
|
||
else if (nchar(as.character(what[i]))>0)
|
||
{{
|
||
x1<-as.Date(mySchedule[i,'from'])
|
||
x2<-as.Date(mySchedule[i,'to'])
|
||
x3<-x1+((x2-x1)*mySchedule[i,'done']/100)
|
||
x<-c(x1,x2)
|
||
x_done<-c(x1,x3)
|
||
y<-c(i,i)
|
||
segments(myBegin, i, myEnd, i, col="{line_color}")
|
||
lines(x,y,lwd=20,col=myColour[i])
|
||
points(myEnd+90,i,cex=(mySchedule[i,'Persons']*mySchedule[i,'Durance'])**0.5,pch=19,col=rgb(110,110,110,50,maxColorValue=255),xpd=T)
|
||
if (x3-x1>1) lines(x_done,y,lwd=20,col=myColour_done)
|
||
if (mySchedule[i,'PAG'] > 0)
|
||
{{
|
||
x4<-as.Date(mySchedule[i,'AG_from'])
|
||
x5<-as.Date(mySchedule[i,'AG_to'])
|
||
x_ag<-c(x4,x5)
|
||
rect(x4,i-0.75,x5,i+0.75,lwd=2)
|
||
}}
|
||
text(myBegin-2,i,what[i],adj=1,xpd=T,cex={main_size},col="{main_color}")
|
||
text(myEnd+25,i,paste(done[i],"%",sep=" "),adj=1,xpd=T,cex={percent_size},col="{percent_color}")
|
||
text(myEnd+35,i,paste(format(x1,format="%d/%m/%y"),"-",format(x2,format="%d/%m/%y"),sep=" "),adj=0,xpd=T,cex={date_size},col="{date_color}")
|
||
}}
|
||
else # Milestone
|
||
{{
|
||
x3<-as.Date(mySchedule[i,'when'])
|
||
myHalf<-(myEnd-myBegin)/2
|
||
if (x3-x1<myHalf)
|
||
{{
|
||
points(as.Date(mySchedule[i,'when']),i,pch=18,cex={font_size}*1.2,col="{font_color}")
|
||
text(as.Date(mySchedule[i,'when'])+5,i,Milestone[i],adj=0,xpd=T,cex={font_size},col="{font_color}")
|
||
}} else
|
||
{{
|
||
points(as.Date(mySchedule[i,'when']),i,pch=18,cex=1.25,col="red")
|
||
text(as.Date(mySchedule[i,'when'])-5,i,Milestone[i],adj=1,xpd=T,cex=0.75)
|
||
}}
|
||
}}
|
||
}}
|
||
axis(3,at=c(myBegin,myEnd),labels=c(format(myBegin,format="%d/%m/%Y"),format(myEnd,format="%d/%m/%Y")),col="{x_scale_font_color}",cex.axis={x_scale_font_size})
|
||
myToday<-Sys.Date()
|
||
abline(v=myToday)
|
||
mtext("today",1,line=0,at=myToday)
|
||
|
||
mtext("{title}",3,line=2,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext(paste("{caption}",format(myToday,format="%d/%m/%y"),sep=""),1,line=4,at=myEnd+20,cex={title_font_size}*0.6,font=3,col="{title_font_color}")
|
||
mtext("{y_name}",2,line=2.5,cex={y_font_size},font=3,outer=T,col="{y_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=408)
|
||
par(lend=1,omi=c(0.25,1,1,0.25),mai=c(1,1.85,0.25,2.75),las=1)
|
||
|
||
plot(from,1:n,type="n",xlab="{x_name}",ylab="",axes=F,xlim=c(myBegin,myEnd),ylim=c(n,1), col.lab="{x_font_color}",cex.lab={x_font_size})
|
||
for (i in 1:n)
|
||
{{
|
||
if (nchar(as.character(Group[i]))>0)
|
||
{{
|
||
text(myBegin-2,i,Group[i],adj=1,xpd=T,cex={heading_font_size},col="{heading_font_color}")
|
||
}}
|
||
else if (nchar(as.character(what[i]))>0)
|
||
{{
|
||
x1<-as.Date(mySchedule[i,'from'])
|
||
x2<-as.Date(mySchedule[i,'to'])
|
||
x3<-x1+((x2-x1)*mySchedule[i,'done']/100)
|
||
x<-c(x1,x2)
|
||
x_done<-c(x1,x3)
|
||
y<-c(i,i)
|
||
segments(myBegin, i, myEnd, i, col="{line_color}")
|
||
lines(x,y,lwd=20,col=myColour[i])
|
||
points(myEnd+90,i,cex=(mySchedule[i,'Persons']*mySchedule[i,'Durance'])**0.5,pch=19,col=rgb(110,110,110,50,maxColorValue=255),xpd=T)
|
||
if (x3-x1>1) lines(x_done,y,lwd=20,col=myColour_done)
|
||
if (mySchedule[i,'PAG'] > 0)
|
||
{{
|
||
x4<-as.Date(mySchedule[i,'AG_from'])
|
||
x5<-as.Date(mySchedule[i,'AG_to'])
|
||
x_ag<-c(x4,x5)
|
||
rect(x4,i-0.75,x5,i+0.75,lwd=2)
|
||
}}
|
||
text(myBegin-2,i,what[i],adj=1,xpd=T,cex={main_size},col="{main_color}")
|
||
text(myEnd+25,i,paste(done[i],"%",sep=" "),adj=1,xpd=T,cex={percent_size},col="{percent_color}")
|
||
text(myEnd+35,i,paste(format(x1,format="%d/%m/%y"),"-",format(x2,format="%d/%m/%y"),sep=" "),adj=0,xpd=T,cex={date_size},col="{date_color}")
|
||
}}
|
||
else # Milestone
|
||
{{
|
||
x3<-as.Date(mySchedule[i,'when'])
|
||
myHalf<-(myEnd-myBegin)/2
|
||
if (x3-x1<myHalf)
|
||
{{
|
||
points(as.Date(mySchedule[i,'when']),i,pch=18,cex={font_size}*1.2,col="{font_color}")
|
||
text(as.Date(mySchedule[i,'when'])+5,i,Milestone[i],adj=0,xpd=T,cex={font_size},col="{font_color}")
|
||
}} else
|
||
{{
|
||
points(as.Date(mySchedule[i,'when']),i,pch=18,cex=1.25,col="red")
|
||
text(as.Date(mySchedule[i,'when'])-5,i,Milestone[i],adj=1,xpd=T,cex=0.75)
|
||
}}
|
||
}}
|
||
}}
|
||
axis(3,at=c(myBegin,myEnd),labels=c(format(myBegin,format="%d/%m/%Y"),format(myEnd,format="%d/%m/%Y")),col="{x_scale_font_color}",cex.axis={x_scale_font_size})
|
||
myToday<-Sys.Date()
|
||
abline(v=myToday)
|
||
mtext("today",1,line=0,at=myToday)
|
||
|
||
mtext("{title}",3,line=2,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext(paste("{caption}",format(myToday,format="%d/%m/%y"),sep=""),1,line=4,at=myEnd+20,cex={title_font_size}*0.6,font=3,col="{title_font_color}")
|
||
mtext("{y_name}",2,line=2.5,cex={y_font_size},font=3,outer=T,col="{y_font_color}")
|
||
dev.off()
|
||
|
||
|
||
svg("{out_path}.svg")
|
||
par(lend=1,omi=c(0.25,1,1,0.25),mai=c(1,1.85,0.25,2.75),las=1)
|
||
|
||
plot(from,1:n,type="n",xlab="{x_name}",ylab="",axes=F,xlim=c(myBegin,myEnd),ylim=c(n,1), col.lab="{x_font_color}",cex.lab={x_font_size})
|
||
for (i in 1:n)
|
||
{{
|
||
if (nchar(as.character(Group[i]))>0)
|
||
{{
|
||
text(myBegin-2,i,Group[i],adj=1,xpd=T,cex={heading_font_size},col="{heading_font_color}")
|
||
}}
|
||
else if (nchar(as.character(what[i]))>0)
|
||
{{
|
||
x1<-as.Date(mySchedule[i,'from'])
|
||
x2<-as.Date(mySchedule[i,'to'])
|
||
x3<-x1+((x2-x1)*mySchedule[i,'done']/100)
|
||
x<-c(x1,x2)
|
||
x_done<-c(x1,x3)
|
||
y<-c(i,i)
|
||
segments(myBegin, i, myEnd, i, col="{line_color}")
|
||
lines(x,y,lwd=20,col=myColour[i])
|
||
points(myEnd+90,i,cex=(mySchedule[i,'Persons']*mySchedule[i,'Durance'])**0.5,pch=19,col=rgb(110,110,110,50,maxColorValue=255),xpd=T)
|
||
if (x3-x1>1) lines(x_done,y,lwd=20,col=myColour_done)
|
||
if (mySchedule[i,'PAG'] > 0)
|
||
{{
|
||
x4<-as.Date(mySchedule[i,'AG_from'])
|
||
x5<-as.Date(mySchedule[i,'AG_to'])
|
||
x_ag<-c(x4,x5)
|
||
rect(x4,i-0.75,x5,i+0.75,lwd=2)
|
||
}}
|
||
text(myBegin-2,i,what[i],adj=1,xpd=T,cex={main_size},col="{main_color}")
|
||
text(myEnd+25,i,paste(done[i],"%",sep=" "),adj=1,xpd=T,cex={percent_size},col="{percent_color}")
|
||
text(myEnd+35,i,paste(format(x1,format="%d/%m/%y"),"-",format(x2,format="%d/%m/%y"),sep=" "),adj=0,xpd=T,cex={date_size},col="{date_color}")
|
||
}}
|
||
else # Milestone
|
||
{{
|
||
x3<-as.Date(mySchedule[i,'when'])
|
||
myHalf<-(myEnd-myBegin)/2
|
||
if (x3-x1<myHalf)
|
||
{{
|
||
points(as.Date(mySchedule[i,'when']),i,pch=18,cex={font_size}*1.2,col="{font_color}")
|
||
text(as.Date(mySchedule[i,'when'])+5,i,Milestone[i],adj=0,xpd=T,cex={font_size},col="{font_color}")
|
||
}} else
|
||
{{
|
||
points(as.Date(mySchedule[i,'when']),i,pch=18,cex=1.25,col="red")
|
||
text(as.Date(mySchedule[i,'when'])-5,i,Milestone[i],adj=1,xpd=T,cex=0.75)
|
||
}}
|
||
}}
|
||
}}
|
||
axis(3,at=c(myBegin,myEnd),labels=c(format(myBegin,format="%d/%m/%Y"),format(myEnd,format="%d/%m/%Y")),col="{x_scale_font_color}",cex.axis={x_scale_font_size})
|
||
myToday<-Sys.Date()
|
||
abline(v=myToday)
|
||
mtext("today",1,line=0,at=myToday)
|
||
|
||
mtext("{title}",3,line=2,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext(paste("{caption}",format(myToday,format="%d/%m/%y"),sep=""),1,line=4,at=myEnd+20,cex={title_font_size}*0.6,font=3,col="{title_font_color}")
|
||
mtext("{y_name}",2,line=2.5,cex={y_font_size},font=3,outer=T,col="{y_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, font_size, unit
|
||
|
||
|
||
# 高级参数 title_font_color
|
||
# 示例文件 Spie_chart.xlsx
|
||
|
||
def Spie_chart(file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, font_size, unit,
|
||
title_font_color):
|
||
code = f"""
|
||
library(RColorBrewer)
|
||
library(gdata)
|
||
x<-read.xls("{file_path}",encoding="latin1")
|
||
attach(x)
|
||
n<-nrow(x)
|
||
myFactor<-max(sqrt(Acosts60))/0.8
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(omi=c(0.5,0.5,0.75,0.5),mai=c(0.1,0.1,0.1,0.1),las=1)
|
||
plot.new()
|
||
myC0<-rep(NA,n)
|
||
myColours<-brewer.pal(n,"Set3")
|
||
for (i in 1:n)
|
||
{{
|
||
par(new=T)
|
||
r<-col2rgb(myColours[i])[1]
|
||
g<-col2rgb(myColours[i])[2]
|
||
b<-col2rgb(myColours[i])[3]
|
||
myC0[i]<-rgb(r,g,b,190,maxColorValue=255)
|
||
myValue<-format(Total60/1000000,digits=1)
|
||
myTotal<-paste(Disease,": ",myValue," {unit}",sep="")
|
||
if (Acosts60[i] == max(Acosts60)) {{myDes<-myTotal}} else {{myDes<-NA}}
|
||
pie(Patients60,border=NA,radius=sqrt(Acosts60[i])/myFactor,col=myC0,labels=myDes,cex={font_size})
|
||
par(new=T)
|
||
r<-col2rgb(myColours[i])[1]
|
||
g<-col2rgb(myColours[i])[2]
|
||
b<-col2rgb(myColours[i])[3]
|
||
myC0[i]<-rgb(r,g,b,maxColorValue=255)
|
||
pie(Patients60,border=NA,radius=sqrt(Pcosts60[i])/myFactor,col=myC0,labels=NA)
|
||
myC0<-rep(NA,n)
|
||
}}
|
||
|
||
mtext("{title}",3,line=-1,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-3,adj=0,cex={title_font_size}*0.5,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=0,adj=0,cex={title_font_size}*0.5,outer=T,font=3,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(omi=c(0.5,0.5,0.75,0.5),mai=c(0.1,0.1,0.1,0.1),las=1)
|
||
plot.new()
|
||
myC0<-rep(NA,n)
|
||
myColours<-brewer.pal(n,"Set3")
|
||
for (i in 1:n)
|
||
{{
|
||
par(new=T)
|
||
r<-col2rgb(myColours[i])[1]
|
||
g<-col2rgb(myColours[i])[2]
|
||
b<-col2rgb(myColours[i])[3]
|
||
myC0[i]<-rgb(r,g,b,190,maxColorValue=255)
|
||
myValue<-format(Total60/1000000,digits=1)
|
||
myTotal<-paste(Disease,": ",myValue," {unit}",sep="")
|
||
if (Acosts60[i] == max(Acosts60)) {{myDes<-myTotal}} else {{myDes<-NA}}
|
||
pie(Patients60,border=NA,radius=sqrt(Acosts60[i])/myFactor,col=myC0,labels=myDes,cex={font_size})
|
||
par(new=T)
|
||
r<-col2rgb(myColours[i])[1]
|
||
g<-col2rgb(myColours[i])[2]
|
||
b<-col2rgb(myColours[i])[3]
|
||
myC0[i]<-rgb(r,g,b,maxColorValue=255)
|
||
pie(Patients60,border=NA,radius=sqrt(Pcosts60[i])/myFactor,col=myC0,labels=NA)
|
||
myC0<-rep(NA,n)
|
||
}}
|
||
|
||
mtext("{title}",3,line=-1,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-3,adj=0,cex={title_font_size}*0.5,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=0,adj=0,cex={title_font_size}*0.5,outer=T,font=3,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=408)
|
||
par(omi=c(0.5,0.5,0.75,0.5),mai=c(0.1,0.1,0.1,0.1),las=1)
|
||
plot.new()
|
||
myC0<-rep(NA,n)
|
||
myColours<-brewer.pal(n,"Set3")
|
||
for (i in 1:n)
|
||
{{
|
||
par(new=T)
|
||
r<-col2rgb(myColours[i])[1]
|
||
g<-col2rgb(myColours[i])[2]
|
||
b<-col2rgb(myColours[i])[3]
|
||
myC0[i]<-rgb(r,g,b,190,maxColorValue=255)
|
||
myValue<-format(Total60/1000000,digits=1)
|
||
myTotal<-paste(Disease,": ",myValue," {unit}",sep="")
|
||
if (Acosts60[i] == max(Acosts60)) {{myDes<-myTotal}} else {{myDes<-NA}}
|
||
pie(Patients60,border=NA,radius=sqrt(Acosts60[i])/myFactor,col=myC0,labels=myDes,cex={font_size})
|
||
par(new=T)
|
||
r<-col2rgb(myColours[i])[1]
|
||
g<-col2rgb(myColours[i])[2]
|
||
b<-col2rgb(myColours[i])[3]
|
||
myC0[i]<-rgb(r,g,b,maxColorValue=255)
|
||
pie(Patients60,border=NA,radius=sqrt(Pcosts60[i])/myFactor,col=myC0,labels=NA)
|
||
myC0<-rep(NA,n)
|
||
}}
|
||
|
||
mtext("{title}",3,line=-1,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-3,adj=0,cex={title_font_size}*0.5,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=0,adj=0,cex={title_font_size}*0.5,outer=T,font=3,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(omi=c(0.5,0.5,0.75,0.5),mai=c(0.1,0.1,0.1,0.1),las=1)
|
||
plot.new()
|
||
myC0<-rep(NA,n)
|
||
myColours<-brewer.pal(n,"Set3")
|
||
for (i in 1:n)
|
||
{{
|
||
par(new=T)
|
||
r<-col2rgb(myColours[i])[1]
|
||
g<-col2rgb(myColours[i])[2]
|
||
b<-col2rgb(myColours[i])[3]
|
||
myC0[i]<-rgb(r,g,b,190,maxColorValue=255)
|
||
myValue<-format(Total60/1000000,digits=1)
|
||
myTotal<-paste(Disease,": ",myValue," {unit}",sep="")
|
||
if (Acosts60[i] == max(Acosts60)) {{myDes<-myTotal}} else {{myDes<-NA}}
|
||
pie(Patients60,border=NA,radius=sqrt(Acosts60[i])/myFactor,col=myC0,labels=myDes,cex={font_size})
|
||
par(new=T)
|
||
r<-col2rgb(myColours[i])[1]
|
||
g<-col2rgb(myColours[i])[2]
|
||
b<-col2rgb(myColours[i])[3]
|
||
myC0[i]<-rgb(r,g,b,maxColorValue=255)
|
||
pie(Patients60,border=NA,radius=sqrt(Pcosts60[i])/myFactor,col=myC0,labels=NA)
|
||
myC0<-rep(NA,n)
|
||
}}
|
||
|
||
mtext("{title}",3,line=-1,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=-3,adj=0,cex={title_font_size}*0.5,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=0,adj=0,cex={title_font_size}*0.5,outer=T,font=3,col="{title_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, letter_1, letter_2, letter_size_1, letter_size_2
|
||
|
||
|
||
# 高级参数 font_color, title_font_color
|
||
# 示例文件 Table_with_Symbols_of_the_Symbol_Signs_Type_Face.xlsx
|
||
|
||
def Table_with_Symbols_of_the_Symbol_Signs_Type_Face(file_path, char_out_path, out_path, title, caption, subtitle,
|
||
title_font_size, letter_1, letter_2, letter_size_1, letter_size_2,
|
||
letter_color_2, letter_color_1, font_color, title_font_color,
|
||
background_color):
|
||
code = f"""
|
||
library(gdata)
|
||
myData<-read.xls('{file_path}', encoding="latin1")
|
||
attach(myData)
|
||
|
||
x <- myData[,2]
|
||
|
||
y <- myData[,3]
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(omi=c(0.5,0.25,0.5,0.25),mai=c(0,0,0,0),cex=1.2,col="{font_color}")
|
||
|
||
b1<-barplot(y+75,horiz=T,xlim=c(-175,175),border=NA,col="{background_color}",axes=F)
|
||
barplot(-x-75,horiz=T,border=NA,add=T,col="{background_color}",axes=F)
|
||
barplot(rep(75,5),horiz=T,border=par("bg"),add=T,col=par("bg"),axes=F)
|
||
barplot(rep(-75,5),horiz=T,border=par("bg"),add=T,col=par("bg"),axes=F)
|
||
abline(v=seq(-175,195,by=10),col=par("bg"))
|
||
text(0,b1,Level)
|
||
|
||
# Titling
|
||
|
||
mtext("{title}",3,line=0.7,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=0,adj=0,cex={title_font_size}*0.5,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=0.25,adj=1.0,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
|
||
# Symbols
|
||
|
||
for (i in 1:5)
|
||
{{
|
||
MyMen_Number<-y[i]
|
||
text(seq(10,10*round(MyMen_Number/10),by=10)+73.5,rep(b1[i],5),rep("{letter_2}",MyMen_Number),
|
||
cex={letter_size_2},col="{letter_color_2}")
|
||
MyWomen_Number<-x[i]
|
||
text(-seq(10,10*round(MyWomen_Number/10),by=10)-68,rep(b1[i],5),rep("{letter_1}",MyWomen_Number),
|
||
cex={letter_size_1},col="{letter_color_1}")
|
||
}}
|
||
|
||
text(55,b1,paste(y, "%", sep=" "),col="{font_color}")
|
||
text(-55,b1,paste(x, "%", sep=" "),col="{font_color}")
|
||
dev.off()
|
||
par(omi=c(0.5,0.25,0.5,0.25),mai=c(0,0,0,0),cex=1.2,col="{font_color}")
|
||
|
||
b1<-barplot(y+75,horiz=T,xlim=c(-175,175),border=NA,col="{background_color}",axes=F)
|
||
barplot(-x-75,horiz=T,border=NA,add=T,col="{background_color}",axes=F)
|
||
barplot(rep(75,5),horiz=T,border=par("bg"),add=T,col=par("bg"),axes=F)
|
||
barplot(rep(-75,5),horiz=T,border=par("bg"),add=T,col=par("bg"),axes=F)
|
||
abline(v=seq(-175,195,by=10),col=par("bg"))
|
||
text(0,b1,Level)
|
||
|
||
# Titling
|
||
|
||
mtext("{title}",3,line=0.7,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=0,adj=0,cex={title_font_size}*0.5,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=0.25,adj=1.0,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
|
||
# Symbols
|
||
|
||
for (i in 1:5)
|
||
{{
|
||
MyMen_Number<-y[i]
|
||
text(seq(10,10*round(MyMen_Number/10),by=10)+73.5,rep(b1[i],5),rep("{letter_2}",MyMen_Number),
|
||
cex={letter_size_2},col="{letter_color_2}")
|
||
MyWomen_Number<-x[i]
|
||
text(-seq(10,10*round(MyWomen_Number/10),by=10)-68,rep(b1[i],5),rep("{letter_1}",MyWomen_Number),
|
||
cex={letter_size_1},col="{letter_color_1}")
|
||
}}
|
||
|
||
text(55,b1,paste(y, "%", sep=" "),col="{font_color}")
|
||
text(-55,b1,paste(x, "%", sep=" "),col="{font_color}")
|
||
png("{out_path}.png")
|
||
|
||
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=540)
|
||
par(omi=c(0.5,0.25,0.5,0.25),mai=c(0,0,0,0),cex=1.2,col="{font_color}")
|
||
|
||
b1<-barplot(y+75,horiz=T,xlim=c(-175,175),border=NA,col="{background_color}",axes=F)
|
||
barplot(-x-75,horiz=T,border=NA,add=T,col="{background_color}",axes=F)
|
||
barplot(rep(75,5),horiz=T,border=par("bg"),add=T,col=par("bg"),axes=F)
|
||
barplot(rep(-75,5),horiz=T,border=par("bg"),add=T,col=par("bg"),axes=F)
|
||
abline(v=seq(-175,195,by=10),col=par("bg"))
|
||
text(0,b1,Level)
|
||
|
||
# Titling
|
||
|
||
mtext("{title}",3,line=0.7,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=0,adj=0,cex={title_font_size}*0.5,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=0.25,adj=1.0,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
|
||
# Symbols
|
||
|
||
for (i in 1:5)
|
||
{{
|
||
MyMen_Number<-y[i]
|
||
text(seq(10,10*round(MyMen_Number/10),by=10)+73.5,rep(b1[i],5),rep("{letter_2}",MyMen_Number),
|
||
cex={letter_size_2},col="{letter_color_2}")
|
||
MyWomen_Number<-x[i]
|
||
text(-seq(10,10*round(MyWomen_Number/10),by=10)-68,rep(b1[i],5),rep("{letter_1}",MyWomen_Number),
|
||
cex={letter_size_1},col="{letter_color_1}")
|
||
}}
|
||
|
||
text(55,b1,paste(y, "%", sep=" "),col="{font_color}")
|
||
text(-55,b1,paste(x, "%", sep=" "),col="{font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(omi=c(0.5,0.25,0.5,0.25),mai=c(0,0,0,0),cex=1.2,col="{font_color}")
|
||
|
||
b1<-barplot(y+75,horiz=T,xlim=c(-175,175),border=NA,col="{background_color}",axes=F)
|
||
barplot(-x-75,horiz=T,border=NA,add=T,col="{background_color}",axes=F)
|
||
barplot(rep(75,5),horiz=T,border=par("bg"),add=T,col=par("bg"),axes=F)
|
||
barplot(rep(-75,5),horiz=T,border=par("bg"),add=T,col=par("bg"),axes=F)
|
||
abline(v=seq(-175,195,by=10),col=par("bg"))
|
||
text(0,b1,Level)
|
||
|
||
# Titling
|
||
|
||
mtext("{title}",3,line=0.7,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=0,adj=0,cex={title_font_size}*0.5,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=0.25,adj=1.0,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
|
||
# Symbols
|
||
|
||
for (i in 1:5)
|
||
{{
|
||
MyMen_Number<-y[i]
|
||
text(seq(10,10*round(MyMen_Number/10),by=10)+73.5,rep(b1[i],5),rep("{letter_2}",MyMen_Number),
|
||
cex={letter_size_2},col="{letter_color_2}")
|
||
MyWomen_Number<-x[i]
|
||
text(-seq(10,10*round(MyWomen_Number/10),by=10)-68,rep(b1[i],5),rep("{letter_1}",MyWomen_Number),
|
||
cex={letter_size_1},col="{letter_color_1}")
|
||
}}
|
||
|
||
text(55,b1,paste(y, "%", sep=" "),col="{font_color}")
|
||
text(-55,b1,paste(x, "%", sep=" "),col="{font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(omi=c(0.5,0.25,0.5,0.25),mai=c(0,0,0,0),cex=1.2,col="{font_color}")
|
||
|
||
b1<-barplot(y+75,horiz=T,xlim=c(-175,175),border=NA,col="{background_color}",axes=F)
|
||
barplot(-x-75,horiz=T,border=NA,add=T,col="{background_color}",axes=F)
|
||
barplot(rep(75,5),horiz=T,border=par("bg"),add=T,col=par("bg"),axes=F)
|
||
barplot(rep(-75,5),horiz=T,border=par("bg"),add=T,col=par("bg"),axes=F)
|
||
abline(v=seq(-175,195,by=10),col=par("bg"))
|
||
text(0,b1,Level)
|
||
|
||
# Titling
|
||
|
||
mtext("{title}",3,line=0.7,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=0,adj=0,cex={title_font_size}*0.5,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=0.25,adj=1.0,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
|
||
# Symbols
|
||
|
||
for (i in 1:5)
|
||
{{
|
||
MyMen_Number<-y[i]
|
||
text(seq(10,10*round(MyMen_Number/10),by=10)+73.5,rep(b1[i],5),rep("{letter_2}",MyMen_Number),
|
||
cex={letter_size_2},col="{letter_color_2}")
|
||
MyWomen_Number<-x[i]
|
||
text(-seq(10,10*round(MyWomen_Number/10),by=10)-68,rep(b1[i],5),rep("{letter_1}",MyWomen_Number),
|
||
cex={letter_size_1},col="{letter_color_1}")
|
||
}}
|
||
|
||
text(55,b1,paste(y, "%", sep=" "),col="{font_color}")
|
||
text(-55,b1,paste(x, "%", sep=" "),col="{font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(omi=c(0.5,0.25,0.5,0.25),mai=c(0,0,0,0),cex=1.2,col="{font_color}")
|
||
|
||
b1<-barplot(y+75,horiz=T,xlim=c(-175,175),border=NA,col="{background_color}",axes=F)
|
||
barplot(-x-75,horiz=T,border=NA,add=T,col="{background_color}",axes=F)
|
||
barplot(rep(75,5),horiz=T,border=par("bg"),add=T,col=par("bg"),axes=F)
|
||
barplot(rep(-75,5),horiz=T,border=par("bg"),add=T,col=par("bg"),axes=F)
|
||
abline(v=seq(-175,195,by=10),col=par("bg"))
|
||
text(0,b1,Level)
|
||
|
||
# Titling
|
||
|
||
mtext("{title}",3,line=0.7,adj=0,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,line=0,adj=0,cex={title_font_size}*0.5,outer=T,col="{title_font_color}")
|
||
mtext("{caption}",1,line=0.25,adj=1.0,cex={title_font_size}*0.4,outer=T,font=3,col="{title_font_color}")
|
||
|
||
# Symbols
|
||
|
||
for (i in 1:5)
|
||
{{
|
||
MyMen_Number<-y[i]
|
||
text(seq(10,10*round(MyMen_Number/10),by=10)+73.5,rep(b1[i],5),rep("{letter_2}",MyMen_Number),
|
||
cex={letter_size_2},col="{letter_color_2}")
|
||
MyWomen_Number<-x[i]
|
||
text(-seq(10,10*round(MyWomen_Number/10),by=10)-68,rep(b1[i],5),rep("{letter_1}",MyWomen_Number),
|
||
cex={letter_size_1},col="{letter_color_1}")
|
||
}}
|
||
|
||
text(55,b1,paste(y, "%", sep=" "),col="{font_color}")
|
||
text(-55,b1,paste(x, "%", sep=" "),col="{font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R")
|
||
|
||
|
||
# 基础参数 file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name, x_font_size, x_scale_font_size, y_font_size, y_scale_font_size, line_name_1, line_name_2, line_size_1, line_size_2, line_size_3, annotation, y_name_1, y_name_2, y_name_3
|
||
# 高级参数 title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color, line_color_1, line_color_2, line_name_3, line_name_4
|
||
# 示例文件 Time_Series_with_Trend_Panel.xlsx
|
||
|
||
def Time_Series_with_Trend_Panel(file_path, char_out_path, out_path, title, caption, subtitle, title_font_size, x_name,
|
||
x_font_size, x_scale_font_size, y_font_size, y_scale_font_size, line_name_1,
|
||
line_name_2, line_size_1,
|
||
line_size_2, line_size_3, annotation, y_name_1, y_name_2, y_name_3,
|
||
title_font_color, x_font_color, x_scale_font_color, y_font_color, y_scale_font_color,
|
||
line_color_1, line_color_2,
|
||
line_name_3, line_name_4):
|
||
|
||
line_color_1 = line_color_1[4:-1]
|
||
|
||
line_color_2 = line_color_2[4:-1]
|
||
|
||
if line_name_3 == "":
|
||
line_name_3 = line_name_1
|
||
|
||
if line_name_4 == "":
|
||
line_name_4 = line_name_2
|
||
|
||
code = f"""
|
||
library(gdata)
|
||
myData<-read.xls('{file_path}',encoding="latin1")
|
||
names(myData) <- c("year","agricultural","marriage","agriculturalz","marriagez","agriculturaltrend","marriagetrend")
|
||
attach(myData)
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(mfcol=c(3,1),cex.axis=1.4,mgp=c(5,1,0),las=1)
|
||
par(omi=c(0.5,0.5,1.1,0.5),mai=c(0,2,0,0.5))
|
||
|
||
myColour1_150<-rgb({line_color_1},150,maxColorValue=255)
|
||
myColour1_50<-rgb({line_color_1},50,maxColorValue=255)
|
||
myColour2_150<-rgb({line_color_2},150,maxColorValue=255)
|
||
myColour2_50<-rgb({line_color_2},50,maxColorValue=255)
|
||
|
||
max_1 <- max(myData[,7],na.rm = T)
|
||
min_1 <- min(myData[,7],na.rm = T)
|
||
max_2 <- max(myData[,6],na.rm = T)
|
||
min_2 <- min(myData[,6],na.rm = T)
|
||
max_3 <- max(c(myData[,4],myData[,5]),na.rm = T)
|
||
min_3 <- min(c(myData[,4],myData[,5]),na.rm = T)
|
||
|
||
par(mai=c(0,1.0,0.25,0))
|
||
plot(year,marriage,axes=F,type="n",xlab="",ylab="{y_name_1}",cex.lab={y_font_size},xlim=c(1820,1920),ylim=c(700,1000),xpd=T,col.lab="{y_font_color}")
|
||
axis(2,at=py<-c(round(min_1),round(min_1+(max_1-min_1)/3),round(max_1-(max_1-min_1)/3),round(max_1)),labels=format(py,big.mark=","),col=par("bg"),col.ticks="grey81",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
lines(year,marriage,type="l",col=myColour1_150,lwd=3,xpd=T)
|
||
lines(year,marriagetrend,type="l",col=myColour1_50,lwd=10)
|
||
text(1910,880,"{line_name_1}",cex={line_size_1},col=myColour1_150)
|
||
|
||
par(mai=c(0,1.0,0,0))
|
||
plot(year,agricultural,axes=F,type="n",xlab="",ylab="{y_name_2}",cex.lab={y_font_size},xlim=c(1820,1920),ylim=c(40,130),col.lab="{y_font_color}")
|
||
axis(4,at=c(round(min_2),round(min_2+(max_2-min_2)/3),round(max_2-(max_2-min_2)/3),round(max_2)),col=par("bg"),col.ticks="grey81",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
lines(year,agricultural,type="l",col=myColour2_150,lwd=3)
|
||
lines(year,agriculturaltrend,type="l",col=myColour2_50,lwd=10)
|
||
text(1910,125,"{line_name_2}",cex={line_size_2},col=myColour2_150,xpd=T)
|
||
text(1913,60,"{annotation}",cex={line_size_2},col=rgb(100,100,100,maxColorValue=255))
|
||
|
||
arrows(1913,68,1913,90,length=0.10,angle=10,code=0,lwd=2,col=rgb(100,100,100,maxColorValue=255))
|
||
points(1913,100,pch=19,col="white",cex=3.5)
|
||
points(1913,100,pch=1,col=rgb(25,25,25,200,maxColorValue=255),cex=3.5)
|
||
points(1913,100,pch=19,col=rgb(25,25,25,200,maxColorValue=255),cex=2.5)
|
||
|
||
par(mai=c(0.5,1.0,0,0))
|
||
plot(year,marriagez,axes=F,type="n",xlab="",ylab="{y_name_3}",cex.lab={y_font_size},xlim=c(1820,1920),ylim=c(-70,70),col.lab="{y_font_color}")
|
||
axis(1,at=pretty(year),col="{x_scale_font_color}",col.axis="{x_scale_font_color}",cex.axis={x_scale_font_size})
|
||
axis(2,at=c(round(min_3),round(min_3+(max_3-min_3)/4),round(min_3+(max_3-min_3)/2),round(max_3-(max_3-min_3)/4),round(max_3)),col=par("bg"),col.ticks="grey81",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
rect(1820,-70,1867,70,border=F,col="grey90")
|
||
lines(year,marriagez,type="l",col=myColour1_150,lwd=3)
|
||
lines(year,agriculturalz,type="l",col=myColour2_150,lwd=3)
|
||
text(1910,-40,"{line_name_3}",col=myColour1_150,cex={line_size_3})
|
||
text(1910,40,"{line_name_4}",col=myColour2_150,cex={line_size_3})
|
||
|
||
mtext("{title}",3,adj=0.5,line=3,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0.06,line=0,cex={title_font_size}*0.6,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-0.2,adj=0.55,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(mfcol=c(3,1),cex.axis=1.4,mgp=c(5,1,0),las=1)
|
||
par(omi=c(0.5,0.5,1.1,0.5),mai=c(0,2,0,0.5))
|
||
|
||
myColour1_150<-rgb({line_color_1},150,maxColorValue=255)
|
||
myColour1_50<-rgb({line_color_1},50,maxColorValue=255)
|
||
myColour2_150<-rgb({line_color_2},150,maxColorValue=255)
|
||
myColour2_50<-rgb({line_color_2},50,maxColorValue=255)
|
||
|
||
max_1 <- max(myData[,7],na.rm = T)
|
||
min_1 <- min(myData[,7],na.rm = T)
|
||
max_2 <- max(myData[,6],na.rm = T)
|
||
min_2 <- min(myData[,6],na.rm = T)
|
||
max_3 <- max(c(myData[,4],myData[,5]),na.rm = T)
|
||
min_3 <- min(c(myData[,4],myData[,5]),na.rm = T)
|
||
|
||
par(mai=c(0,1.0,0.25,0))
|
||
plot(year,marriage,axes=F,type="n",xlab="",ylab="{y_name_1}",cex.lab={y_font_size},xlim=c(1820,1920),ylim=c(700,1000),xpd=T,col.lab="{y_font_color}")
|
||
axis(2,at=py<-c(round(min_1),round(min_1+(max_1-min_1)/3),round(max_1-(max_1-min_1)/3),round(max_1)),labels=format(py,big.mark=","),col=par("bg"),col.ticks="grey81",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
lines(year,marriage,type="l",col=myColour1_150,lwd=3,xpd=T)
|
||
lines(year,marriagetrend,type="l",col=myColour1_50,lwd=10)
|
||
text(1910,880,"{line_name_1}",cex={line_size_1},col=myColour1_150)
|
||
|
||
par(mai=c(0,1.0,0,0))
|
||
plot(year,agricultural,axes=F,type="n",xlab="",ylab="{y_name_2}",cex.lab={y_font_size},xlim=c(1820,1920),ylim=c(40,130),col.lab="{y_font_color}")
|
||
axis(4,at=c(round(min_2),round(min_2+(max_2-min_2)/3),round(max_2-(max_2-min_2)/3),round(max_2)),col=par("bg"),col.ticks="grey81",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
lines(year,agricultural,type="l",col=myColour2_150,lwd=3)
|
||
lines(year,agriculturaltrend,type="l",col=myColour2_50,lwd=10)
|
||
text(1910,125,"{line_name_2}",cex={line_size_2},col=myColour2_150,xpd=T)
|
||
text(1913,60,"{annotation}",cex={line_size_2},col=rgb(100,100,100,maxColorValue=255))
|
||
|
||
arrows(1913,68,1913,90,length=0.10,angle=10,code=0,lwd=2,col=rgb(100,100,100,maxColorValue=255))
|
||
points(1913,100,pch=19,col="white",cex=3.5)
|
||
points(1913,100,pch=1,col=rgb(25,25,25,200,maxColorValue=255),cex=3.5)
|
||
points(1913,100,pch=19,col=rgb(25,25,25,200,maxColorValue=255),cex=2.5)
|
||
|
||
par(mai=c(0.5,1.0,0,0))
|
||
plot(year,marriagez,axes=F,type="n",xlab="",ylab="{y_name_3}",cex.lab={y_font_size},xlim=c(1820,1920),ylim=c(-70,70),col.lab="{y_font_color}")
|
||
axis(1,at=pretty(year),col="{x_scale_font_color}",col.axis="{x_scale_font_color}",cex.axis={x_scale_font_size})
|
||
axis(2,at=c(round(min_3),round(min_3+(max_3-min_3)/4),round(min_3+(max_3-min_3)/2),round(max_3-(max_3-min_3)/4),round(max_3)),col=par("bg"),col.ticks="grey81",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
rect(1820,-70,1867,70,border=F,col="grey90")
|
||
lines(year,marriagez,type="l",col=myColour1_150,lwd=3)
|
||
lines(year,agriculturalz,type="l",col=myColour2_150,lwd=3)
|
||
text(1910,-40,"{line_name_3}",col=myColour1_150,cex={line_size_3})
|
||
text(1910,40,"{line_name_4}",col=myColour2_150,cex={line_size_3})
|
||
|
||
mtext("{title}",3,adj=0.5,line=3,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0.06,line=0,cex={title_font_size}*0.6,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-0.2,adj=0.55,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=408)
|
||
par(mfcol=c(3,1),cex.axis=1.4,mgp=c(5,1,0),las=1)
|
||
par(omi=c(0.5,0.5,1.1,0.5),mai=c(0,2,0,0.5))
|
||
|
||
myColour1_150<-rgb({line_color_1},150,maxColorValue=255)
|
||
myColour1_50<-rgb({line_color_1},50,maxColorValue=255)
|
||
myColour2_150<-rgb({line_color_2},150,maxColorValue=255)
|
||
myColour2_50<-rgb({line_color_2},50,maxColorValue=255)
|
||
|
||
max_1 <- max(myData[,7],na.rm = T)
|
||
min_1 <- min(myData[,7],na.rm = T)
|
||
max_2 <- max(myData[,6],na.rm = T)
|
||
min_2 <- min(myData[,6],na.rm = T)
|
||
max_3 <- max(c(myData[,4],myData[,5]),na.rm = T)
|
||
min_3 <- min(c(myData[,4],myData[,5]),na.rm = T)
|
||
|
||
par(mai=c(0,1.0,0.25,0))
|
||
plot(year,marriage,axes=F,type="n",xlab="",ylab="{y_name_1}",cex.lab={y_font_size},xlim=c(1820,1920),ylim=c(700,1000),xpd=T,col.lab="{y_font_color}")
|
||
axis(2,at=py<-c(round(min_1),round(min_1+(max_1-min_1)/3),round(max_1-(max_1-min_1)/3),round(max_1)),labels=format(py,big.mark=","),col=par("bg"),col.ticks="grey81",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
lines(year,marriage,type="l",col=myColour1_150,lwd=3,xpd=T)
|
||
lines(year,marriagetrend,type="l",col=myColour1_50,lwd=10)
|
||
text(1910,880,"{line_name_1}",cex={line_size_1},col=myColour1_150)
|
||
|
||
par(mai=c(0,1.0,0,0))
|
||
plot(year,agricultural,axes=F,type="n",xlab="",ylab="{y_name_2}",cex.lab={y_font_size},xlim=c(1820,1920),ylim=c(40,130),col.lab="{y_font_color}")
|
||
axis(4,at=c(round(min_2),round(min_2+(max_2-min_2)/3),round(max_2-(max_2-min_2)/3),round(max_2)),col=par("bg"),col.ticks="grey81",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
lines(year,agricultural,type="l",col=myColour2_150,lwd=3)
|
||
lines(year,agriculturaltrend,type="l",col=myColour2_50,lwd=10)
|
||
text(1910,125,"{line_name_2}",cex={line_size_2},col=myColour2_150,xpd=T)
|
||
text(1913,60,"{annotation}",cex={line_size_2},col=rgb(100,100,100,maxColorValue=255))
|
||
|
||
arrows(1913,68,1913,90,length=0.10,angle=10,code=0,lwd=2,col=rgb(100,100,100,maxColorValue=255))
|
||
points(1913,100,pch=19,col="white",cex=3.5)
|
||
points(1913,100,pch=1,col=rgb(25,25,25,200,maxColorValue=255),cex=3.5)
|
||
points(1913,100,pch=19,col=rgb(25,25,25,200,maxColorValue=255),cex=2.5)
|
||
|
||
par(mai=c(0.5,1.0,0,0))
|
||
plot(year,marriagez,axes=F,type="n",xlab="",ylab="{y_name_3}",cex.lab={y_font_size},xlim=c(1820,1920),ylim=c(-70,70),col.lab="{y_font_color}")
|
||
axis(1,at=pretty(year),col="{x_scale_font_color}",col.axis="{x_scale_font_color}",cex.axis={x_scale_font_size})
|
||
axis(2,at=c(round(min_3),round(min_3+(max_3-min_3)/4),round(min_3+(max_3-min_3)/2),round(max_3-(max_3-min_3)/4),round(max_3)),col=par("bg"),col.ticks="grey81",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
rect(1820,-70,1867,70,border=F,col="grey90")
|
||
lines(year,marriagez,type="l",col=myColour1_150,lwd=3)
|
||
lines(year,agriculturalz,type="l",col=myColour2_150,lwd=3)
|
||
text(1910,-40,"{line_name_3}",col=myColour1_150,cex={line_size_3})
|
||
text(1910,40,"{line_name_4}",col=myColour2_150,cex={line_size_3})
|
||
|
||
mtext("{title}",3,adj=0.5,line=3,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0.06,line=0,cex={title_font_size}*0.6,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-0.2,adj=0.55,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
|
||
|
||
svg("{out_path}.svg")
|
||
par(mfcol=c(3,1),cex.axis=1.4,mgp=c(5,1,0),las=1)
|
||
par(omi=c(0.5,0.5,1.1,0.5),mai=c(0,2,0,0.5))
|
||
|
||
myColour1_150<-rgb({line_color_1},150,maxColorValue=255)
|
||
myColour1_50<-rgb({line_color_1},50,maxColorValue=255)
|
||
myColour2_150<-rgb({line_color_2},150,maxColorValue=255)
|
||
myColour2_50<-rgb({line_color_2},50,maxColorValue=255)
|
||
|
||
max_1 <- max(myData[,7],na.rm = T)
|
||
min_1 <- min(myData[,7],na.rm = T)
|
||
max_2 <- max(myData[,6],na.rm = T)
|
||
min_2 <- min(myData[,6],na.rm = T)
|
||
max_3 <- max(c(myData[,4],myData[,5]),na.rm = T)
|
||
min_3 <- min(c(myData[,4],myData[,5]),na.rm = T)
|
||
|
||
par(mai=c(0,1.0,0.25,0))
|
||
plot(year,marriage,axes=F,type="n",xlab="",ylab="{y_name_1}",cex.lab={y_font_size},xlim=c(1820,1920),ylim=c(700,1000),xpd=T,col.lab="{y_font_color}")
|
||
axis(2,at=py<-c(round(min_1),round(min_1+(max_1-min_1)/3),round(max_1-(max_1-min_1)/3),round(max_1)),labels=format(py,big.mark=","),col=par("bg"),col.ticks="grey81",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
lines(year,marriage,type="l",col=myColour1_150,lwd=3,xpd=T)
|
||
lines(year,marriagetrend,type="l",col=myColour1_50,lwd=10)
|
||
text(1910,880,"{line_name_1}",cex={line_size_1},col=myColour1_150)
|
||
|
||
par(mai=c(0,1.0,0,0))
|
||
plot(year,agricultural,axes=F,type="n",xlab="",ylab="{y_name_2}",cex.lab={y_font_size},xlim=c(1820,1920),ylim=c(40,130),col.lab="{y_font_color}")
|
||
axis(4,at=c(round(min_2),round(min_2+(max_2-min_2)/3),round(max_2-(max_2-min_2)/3),round(max_2)),col=par("bg"),col.ticks="grey81",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
lines(year,agricultural,type="l",col=myColour2_150,lwd=3)
|
||
lines(year,agriculturaltrend,type="l",col=myColour2_50,lwd=10)
|
||
text(1910,125,"{line_name_2}",cex={line_size_2},col=myColour2_150,xpd=T)
|
||
text(1913,60,"{annotation}",cex={line_size_2},col=rgb(100,100,100,maxColorValue=255))
|
||
|
||
arrows(1913,68,1913,90,length=0.10,angle=10,code=0,lwd=2,col=rgb(100,100,100,maxColorValue=255))
|
||
points(1913,100,pch=19,col="white",cex=3.5)
|
||
points(1913,100,pch=1,col=rgb(25,25,25,200,maxColorValue=255),cex=3.5)
|
||
points(1913,100,pch=19,col=rgb(25,25,25,200,maxColorValue=255),cex=2.5)
|
||
|
||
par(mai=c(0.5,1.0,0,0))
|
||
plot(year,marriagez,axes=F,type="n",xlab="",ylab="{y_name_3}",cex.lab={y_font_size},xlim=c(1820,1920),ylim=c(-70,70),col.lab="{y_font_color}")
|
||
axis(1,at=pretty(year),col="{x_scale_font_color}",col.axis="{x_scale_font_color}",cex.axis={x_scale_font_size})
|
||
axis(2,at=c(round(min_3),round(min_3+(max_3-min_3)/4),round(min_3+(max_3-min_3)/2),round(max_3-(max_3-min_3)/4),round(max_3)),col=par("bg"),col.ticks="grey81",lwd.ticks=0.5,tck=-0.025,col.axis="{y_scale_font_color}",cex.axis={y_scale_font_size})
|
||
rect(1820,-70,1867,70,border=F,col="grey90")
|
||
lines(year,marriagez,type="l",col=myColour1_150,lwd=3)
|
||
lines(year,agriculturalz,type="l",col=myColour2_150,lwd=3)
|
||
text(1910,-40,"{line_name_3}",col=myColour1_150,cex={line_size_3})
|
||
text(1910,40,"{line_name_4}",col=myColour2_150,cex={line_size_3})
|
||
|
||
mtext("{title}",3,adj=0.5,line=3,cex={title_font_size},outer=T,col="{title_font_color}")
|
||
mtext("{subtitle}",3,adj=0.06,line=0,cex={title_font_size}*0.6,outer=T,font=3,col="{title_font_color}")
|
||
mtext("{caption}",1,line=2,adj=1.0,cex={title_font_size}*0.6,font=3,outer=T,col="{title_font_color}")
|
||
mtext("{x_name}",1,line=-0.2,adj=0.55,cex={x_font_size},font=3,outer=T,col="{x_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(
|
||
f"Rscript {out_path}.R") # 基础参数 file_path_1, file_path_2, char_out_path, out_path, title, caption, subtitle, title_font_size, label_size
|
||
|
||
|
||
# 高级参数 title_font_color, scale_font_color, font_color
|
||
# 示例文件 chord_Diagram_1.csv chord_Diagram_2.csv
|
||
|
||
def chord_Diagram(file_path_1, file_path_2, char_out_path, out_path, title, caption, subtitle, title_font_size,
|
||
font_size,
|
||
title_font_color, scale_font_color, font_color):
|
||
code = f"""
|
||
library("circlize")
|
||
df0 <- read.csv("{file_path_1}", stringsAsFactors=FALSE)
|
||
df1 <- read.csv("{file_path_2}", stringsAsFactors=FALSE)
|
||
|
||
pdf("{out_path}.pdf")
|
||
par(omi=c(0.25,0.25,0.25,0.25), mai=c(0,0,0,0),col="{scale_font_color}")
|
||
|
||
circos.par(start.degree = 90, gap.degree = 4, track.margin = c(-0.1, 0.1), points.overflow.warning = FALSE)
|
||
par(mar = rep(0, 4))
|
||
|
||
chordDiagram(x = df0, grid.col = df1$col, transparency = 0.25,
|
||
order = df1$region, directional = 1,
|
||
direction.type = c("arrows", "diffHeight"), diffHeight = -0.04,
|
||
annotationTrack = "grid", annotationTrackHeight = c(0.05, 0.1),
|
||
link.arr.type = "big.arrow", link.sort = TRUE, link.largest.ontop = TRUE)
|
||
|
||
circos.trackPlotRegion(
|
||
track.index = 1,
|
||
bg.border = NA,
|
||
panel.fun = function(x, y) {{
|
||
xlim = get.cell.meta.data("xlim")
|
||
sector.index = get.cell.meta.data("sector.index")
|
||
reg1 = df1$reg1[df1$region == sector.index]
|
||
reg2 = df1$reg2[df1$region == sector.index]
|
||
|
||
circos.text(x = mean(xlim), y = ifelse(test = nchar(reg2) == 0, yes = 5.2, no = 6.0),
|
||
labels = reg1, facing = "bending", cex = {font_size}, col="{font_color}")
|
||
circos.text(x = mean(xlim), y = 4.4,
|
||
labels = reg2, facing = "bending", cex = {font_size}, col="{font_color}")
|
||
circos.axis(h = "top",
|
||
major.at = seq(from = 0, to = xlim[2], by = ifelse(test = xlim[2]>10, yes = 2, no = 1)),
|
||
minor.ticks = 1, major.tick.length = 0.5,
|
||
labels.niceFacing = FALSE)
|
||
}}
|
||
)
|
||
circos.clear()
|
||
|
||
mtext(line=-1,"{title}",cex={title_font_size},adj=0,col="{title_font_color}")
|
||
mtext(line=-2.5,"{subtitle}",cex={title_font_size}*0.6,adj=0,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=-0.5,"{caption}",cex={title_font_size}*0.6,adj=1,font=3,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{out_path}.png")
|
||
par(omi=c(0.25,0.25,0.25,0.25), mai=c(0,0,0,0),col="{scale_font_color}")
|
||
|
||
circos.par(start.degree = 90, gap.degree = 4, track.margin = c(-0.1, 0.1), points.overflow.warning = FALSE)
|
||
par(mar = rep(0, 4))
|
||
|
||
chordDiagram(x = df0, grid.col = df1$col, transparency = 0.25,
|
||
order = df1$region, directional = 1,
|
||
direction.type = c("arrows", "diffHeight"), diffHeight = -0.04,
|
||
annotationTrack = "grid", annotationTrackHeight = c(0.05, 0.1),
|
||
link.arr.type = "big.arrow", link.sort = TRUE, link.largest.ontop = TRUE)
|
||
|
||
circos.trackPlotRegion(
|
||
track.index = 1,
|
||
bg.border = NA,
|
||
panel.fun = function(x, y) {{
|
||
xlim = get.cell.meta.data("xlim")
|
||
sector.index = get.cell.meta.data("sector.index")
|
||
reg1 = df1$reg1[df1$region == sector.index]
|
||
reg2 = df1$reg2[df1$region == sector.index]
|
||
|
||
circos.text(x = mean(xlim), y = ifelse(test = nchar(reg2) == 0, yes = 5.2, no = 6.0),
|
||
labels = reg1, facing = "bending", cex = {font_size}, col="{font_color}")
|
||
circos.text(x = mean(xlim), y = 4.4,
|
||
labels = reg2, facing = "bending", cex = {font_size}, col="{font_color}")
|
||
circos.axis(h = "top",
|
||
major.at = seq(from = 0, to = xlim[2], by = ifelse(test = xlim[2]>10, yes = 2, no = 1)),
|
||
minor.ticks = 1, major.tick.length = 0.5,
|
||
labels.niceFacing = FALSE)
|
||
}}
|
||
)
|
||
circos.clear()
|
||
|
||
mtext(line=-1,"{title}",cex={title_font_size},adj=0,col="{title_font_color}")
|
||
mtext(line=-2.5,"{subtitle}",cex={title_font_size}*0.6,adj=0,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=-0.5,"{caption}",cex={title_font_size}*0.6,adj=1,font=3,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
png("{char_out_path}",width=900,height=408)
|
||
par(omi=c(0.25,0.25,0.25,0.25), mai=c(0,0,0,0),col="{scale_font_color}")
|
||
|
||
circos.par(start.degree = 90, gap.degree = 4, track.margin = c(-0.1, 0.1), points.overflow.warning = FALSE)
|
||
par(mar = rep(0, 4))
|
||
|
||
chordDiagram(x = df0, grid.col = df1$col, transparency = 0.25,
|
||
order = df1$region, directional = 1,
|
||
direction.type = c("arrows", "diffHeight"), diffHeight = -0.04,
|
||
annotationTrack = "grid", annotationTrackHeight = c(0.05, 0.1),
|
||
link.arr.type = "big.arrow", link.sort = TRUE, link.largest.ontop = TRUE)
|
||
|
||
circos.trackPlotRegion(
|
||
track.index = 1,
|
||
bg.border = NA,
|
||
panel.fun = function(x, y) {{
|
||
xlim = get.cell.meta.data("xlim")
|
||
sector.index = get.cell.meta.data("sector.index")
|
||
reg1 = df1$reg1[df1$region == sector.index]
|
||
reg2 = df1$reg2[df1$region == sector.index]
|
||
|
||
circos.text(x = mean(xlim), y = ifelse(test = nchar(reg2) == 0, yes = 5.2, no = 6.0),
|
||
labels = reg1, facing = "bending", cex = {font_size}, col="{font_color}")
|
||
circos.text(x = mean(xlim), y = 4.4,
|
||
labels = reg2, facing = "bending", cex = {font_size}, col="{font_color}")
|
||
circos.axis(h = "top",
|
||
major.at = seq(from = 0, to = xlim[2], by = ifelse(test = xlim[2]>10, yes = 2, no = 1)),
|
||
minor.ticks = 1, major.tick.length = 0.5,
|
||
labels.niceFacing = FALSE)
|
||
}}
|
||
)
|
||
circos.clear()
|
||
|
||
mtext(line=-1,"{title}",cex={title_font_size},adj=0,col="{title_font_color}")
|
||
mtext(line=-2.5,"{subtitle}",cex={title_font_size}*0.6,adj=0,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=-0.5,"{caption}",cex={title_font_size}*0.6,adj=1,font=3,col="{title_font_color}")
|
||
dev.off()
|
||
|
||
svg("{out_path}.svg")
|
||
par(omi=c(0.25,0.25,0.25,0.25), mai=c(0,0,0,0),col="{scale_font_color}")
|
||
|
||
circos.par(start.degree = 90, gap.degree = 4, track.margin = c(-0.1, 0.1), points.overflow.warning = FALSE)
|
||
par(mar = rep(0, 4))
|
||
|
||
chordDiagram(x = df0, grid.col = df1$col, transparency = 0.25,
|
||
order = df1$region, directional = 1,
|
||
direction.type = c("arrows", "diffHeight"), diffHeight = -0.04,
|
||
annotationTrack = "grid", annotationTrackHeight = c(0.05, 0.1),
|
||
link.arr.type = "big.arrow", link.sort = TRUE, link.largest.ontop = TRUE)
|
||
|
||
circos.trackPlotRegion(
|
||
track.index = 1,
|
||
bg.border = NA,
|
||
panel.fun = function(x, y) {{
|
||
xlim = get.cell.meta.data("xlim")
|
||
sector.index = get.cell.meta.data("sector.index")
|
||
reg1 = df1$reg1[df1$region == sector.index]
|
||
reg2 = df1$reg2[df1$region == sector.index]
|
||
|
||
circos.text(x = mean(xlim), y = ifelse(test = nchar(reg2) == 0, yes = 5.2, no = 6.0),
|
||
labels = reg1, facing = "bending", cex = {font_size}, col="{font_color}")
|
||
circos.text(x = mean(xlim), y = 4.4,
|
||
labels = reg2, facing = "bending", cex = {font_size}, col="{font_color}")
|
||
circos.axis(h = "top",
|
||
major.at = seq(from = 0, to = xlim[2], by = ifelse(test = xlim[2]>10, yes = 2, no = 1)),
|
||
minor.ticks = 1, major.tick.length = 0.5,
|
||
labels.niceFacing = FALSE)
|
||
}}
|
||
)
|
||
circos.clear()
|
||
|
||
mtext(line=-1,"{title}",cex={title_font_size},adj=0,col="{title_font_color}")
|
||
mtext(line=-2.5,"{subtitle}",cex={title_font_size}*0.6,adj=0,font=3,col="{title_font_color}")
|
||
mtext(side=1,line=-0.5,"{caption}",cex={title_font_size}*0.6,adj=1,font=3,col="{title_font_color}")
|
||
dev.off()
|
||
"""
|
||
|
||
keep_txt_to_file(code, f"{out_path}.R")
|
||
os.system(f"Rscript {out_path}.R") |