我正在尝试重新创建下面的图表,但我不确定如何添加另一个 y 轴,因为两个系列有不同的单位。[![Graph][1]][1]
现在我只添加了价值数据(回报和夏普比率),我不知道如何继续。我从以下示例开始,得到了下面的图表使用 pgfplots 制作分组条形图并根据我的需要编辑了一些内容。希望有人能帮助我,先谢谢了。
\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper,top=3cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{caption}
\begin{document}
\begin{figure}[ht]
\makeatletter
\pgfplotsset{
calculate offset/.code={
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
\pgfkeys{/pgf/fpu=false}
},
every node near coord/.style={
/pgfplots/calculate offset,
yshift=-\testmacro
}
}
\pgfplotstableread{
%Value-return %Growth
0 6.4 4.5
1 12.8 4.4
2 13.6 6.5
3 11.1 9.4
}\dataset
\begin{tikzpicture}
\begin{axis}[ybar,
width=12cm,
height=8cm,
ymin=0,
ymax=16,
ylabel={Average annual return (\%)},
xtick=data,
xtick pos=left,
ytick pos=left,
xticklabels = {
P/E ratio,
P/B ratio,
P/C ratio,
P/S ratio
},
xticklabel style={yshift=-10ex},
major x tick style = {opacity=0},
minor x tick num = 1,
minor tick length=2ex,
area legend,
every node near coord/.append style={
anchor=east,
rotate=90
}
]
\addplot[draw=black,fill=black!20] table[x index=0,y index=1] \dataset; %Return
\addlegendentry{Return}
\addplot[draw=black,fill=black!40] table[x index=0,y index=2] \dataset; %Sharpe ratio
\end{axis}
\end{tikzpicture}
\captionsetup{labelfont=bf, format=plain, labelformat=default}
\caption{Average annual returns and Sharpe ratios for value and growth portfolios in the period 1992-2017, portfolios were created using different financial ratios.}
\label{fig:overview}
\end{figure}
\end{document}
更新:在一些帮助下我已经取得了一些进展,现在我几乎已经得到了我想要的东西。 上面的图片就是我现在所拥有的。我想隐藏价值和增长上方的小线条,如果可能的话,将标题稍微向上移动一点。
\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper,top=3cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{caption}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{figure}[ht]
\centering
\pgfmathsetmacro{\BarOffset}{0.15}
\pgfplotsset{
% define a new style to use on both axis environments
MyAxis/.style={
ybar,
scale only axis,
width=12cm,
height=8cm,
ymin=0,
xmin=-0.5,
xmax=7.5, % you need to increase this if you add more data
area legend,
bar width=10pt
}
}
\pgfplotstableread{
%Returns %Sharpe
0 6.4 0.232
1 4.5 0.279
2 12.8 0.397
3 4.4 0.279
4 13.6 0.462
5 6.5 0.221
6 11.1 0.337
7 9.4 0.377
}\dataset
\begin{tikzpicture}
\begin{axis}[
title= \Large{Average annual returns and Sharpe ratios for value and growth portfolios},
ylabel={Average annual return (\%)},
ymax=16,
ymajorgrids=true,
xtick={0,...,7},
xtick pos=left,
ytick pos=left,
xticklabels = {
Value,
Growth,
Value,
Growth,
Value,
Growth,
Value,
Growth
},
yticklabel style={xshift=-0.5ex},
tickwidth=5pt,
extra x ticks={-0.5,1.5,...,3.5,5.5,7.5},
extra x tick labels={},
extra x tick style={tickwidth=1.2cm},
minor x tick style = {opacity=0},
MyAxis
]
\addplot[draw=black,fill=black!20] table[x expr=\coordindex-\BarOffset,y index=1] \dataset; %Return
\label{returnplot}
\end{axis}
\begin{axis}[
name=ax2,
ymax=0.5,
MyAxis,
ylabel=Average Sharpe ratio,
ytick pos=right,
yticklabel style={xshift=0.5ex},
clip=false,
xtick={0.5,2.5,4.5,6.5},
xticklabels={P/E ratio, P/B ratio, P/C ratio, P/S ratio},
xticklabel style={yshift=-5mm},
tickwidth=5pt,
xtick style={draw=none}
]
\addplot[draw=black,fill=black!40] table[x expr=\coordindex+\BarOffset,y index=2] \dataset; %Sharpe ratio
\label{ratioplot}
\end{axis}
% legend
\node [below=1.3cm] at (ax2.south) {\ref{returnplot} Return \quad \ref{ratioplot} Sharpe ratio};
\end{tikzpicture}
\captionsetup{labelfont=bf, format=plain, labelformat=default}
\caption{Average annual returns and Sharpe ratios for value and growth portfolios in the period 1992-2017, portfolios were created using different financial ratios.}
\label{fig:overview}
\end{figure}
\end{document}
[1]: https://i.stack.imgur.com/
答案1
我想这就是你想要的。这里没有太多解释,但如果有任何不清楚的地方,请询问。
\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper,top=3cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{caption}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{figure}[ht]
\centering
\pgfmathsetmacro{\BarOffset}{0.15}
\pgfplotsset{
% define a new style to use on both axis environments
MyAxis/.style={
ybar,
scale only axis,
width=12cm,
height=8cm,
ymin=0,
xmin=-0.5,
xmax=7.5, % you need to increase this if you add more data
area legend,
bar width=10pt
}
}
\pgfplotstableread{
%Returns %Sharpe
0 6.4 0.232
1 4.5 0.279
2 12.8 0.397
3 4.4 0.279
4 13.6 0.462
5 6.5 0.221
6 11.1 0.337
7 9.4 0.377
}\dataset
\begin{tikzpicture}
\begin{axis}[
title= {Average annual returns and\\Sharpe ratios for value and growth portfolios}, %% modified
title style={yshift=3mm,font=\Large,align=center}, % added
ylabel={Average annual return (\%)},
ymax=16,
ymajorgrids=true,
xtick={0,...,7},
xtick pos=left,
ytick pos=left,
xticklabels = {
Value,
Growth,
Value,
Growth,
Value,
Growth,
Value,
Growth
},
yticklabel style={xshift=-0.5ex},
xticklabel style={yshift=-5pt}, %% added
tickwidth=0pt, % modified
extra x ticks={-0.5,1.5,...,3.5,5.5,7.5},
extra x tick labels={},
extra x tick style={tickwidth=1.2cm},
minor x tick style = {opacity=0},
MyAxis
]
\addplot[draw=black,fill=black!20] table[x expr=\coordindex-\BarOffset,y index=1] \dataset; %Return
\label{returnplot}
\end{axis}
\begin{axis}[
name=ax2,
ymax=0.5,
MyAxis,
ylabel=Average Sharpe ratio,
ytick pos=right,
yticklabel style={xshift=0.5ex},
clip=false,
xtick={0.5,2.5,4.5,6.5},
xticklabels={P/E ratio, P/B ratio, P/C ratio, P/S ratio},
xticklabel style={yshift=-5mm},
tickwidth=5pt,
xtick style={draw=none}
]
\addplot[draw=black,fill=black!40] table[x expr=\coordindex+\BarOffset,y index=2] \dataset; %Sharpe ratio
\label{ratioplot}
\end{axis}
% legend
\node [below=1.3cm] at (ax2.south) {\ref{returnplot} Return \quad \ref{ratioplot} Sharpe ratio};
\end{tikzpicture}
\captionsetup{labelfont=bf, format=plain, labelformat=default}
\caption{Average annual returns and Sharpe ratios for value and growth portfolios in the period 1992-2017, portfolios were created using different financial ratios.}
\label{fig:overview}
\end{figure}
\end{document}