使用pgfplot旋转yticklabel的问题

时间:2019-05-29 15:30:20

标签: latex

我正在使用dt3 <- read.table(header=T, text="birth_year Name role hh 1877 Snijders HeadOfHousehold 1 1885 Marteen NA 1 1897 Snijders NA 1 1892 Zelstra NA 1 1878 Kuipers HeadOfHousehold 2 1870 Marteen NA 2 1897 Wals NA 2 1900 Venstra NA 2 1900 Lippe HeadOfHousehold 3 1905 Flachs NA 3 1920 Lippe NA 3 1922 Lippe NA 3", as.is = T) dt3 <- dt3[with(dt3, order(hh,role!="HeadOfHousehold")),] dt3$role[with(dt3, as.logical(ave(Name, hh, FUN = function(x) x==x[1])) & ave(birth_year, hh, FUN = function(x) x>(x[1]+14)))] <- "children" dt3 birth_year Name role hh 1 1877 Snijders HeadOfHousehold 1 2 1885 Marteen <NA> 1 3 1897 Snijders children 1 4 1892 Zelstra <NA> 1 5 1878 Kuipers HeadOfHousehold 2 6 1870 Marteen <NA> 2 7 1897 Wals <NA> 2 8 1900 Venstra <NA> 2 9 1900 Lippe HeadOfHousehold 3 10 1905 Flachs <NA> 3 11 1920 Lippe children 3 12 1922 Lippe children 3 将pgfplot中的ytick标签旋转90°。标签文本未正确水平对齐。

在下面的示例中,“分组”未与其他标签对齐(特别是因为它包含“ p”)。

图片:

enter image description here

我用不了“样式”选项来解决此问题,是否知道如何使这些值水平对齐?谢谢!

编辑:

最小示例(请注意,我的实际代码使用标签来寻址CSV文件中的列):

"yticklabel style={rotate=90}"

1 个答案:

答案 0 :(得分:0)

使用yticklabel style={rotate=90,anchor=base,yshift=0.2cm}

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pagestyle{empty}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    xbar, xmin=0,
    width=12cm, height=5.5cm, enlarge y limits=0.5,
    xlabel={\#participants},
    symbolic y coords={Flat,Grouped},
    ytick=data,
    nodes near coords, nodes near coords align={horizontal},
    yticklabel style={rotate=90,anchor=base,yshift=0.2cm},
    ]
    \addplot coordinates {(3,{Flat}) (7,Grouped)};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

(红线仅表示基线已对齐)