I am new to pgfplots and actually I want to create something pretty basic.
Yesterday, I started modifying my chart with the help of the pgfplot manual and the help of Google but unfortunately, I couldn't make it yet.
Something like this I would like to create:
This leads me to the following questions:
How can I add
- the long descriptions with linebreaks?
- the horizontal guidelines
- the percantage in or above the bars?
I am not that far yet, but maybe I need the standard-deviation later as well. It would be awesome if someone could tell me how to add this!
I also tried to use something like a separate legend on the right to outsource the long descriptions but I couldn't archieve this.
Here my unsuccessful attempt:
\begin{figure}
\centering
\pgfplotstableread[row sep=\\,col sep=&]{
interval & carT \\
A & 31.1 \\
B & 17.8 \\
C & 44.4 \\
D & 6.7 \\
}\mydata
\begin{tikzpicture}
\begin{axis}[
title=Aus welchen Gründen wurde eLearning in Ihrem Klinikum implementiert?,
ybar,
bar width=30pt,
width=250pt,
ymax=50,
ylabel=Anzahl in \%,
symbolic x coords={A,B,C,D},
xtick=data
]
\addplot table[x=interval,y=carT]{\mydata};
\end{axis}
\end{tikzpicture}
\caption{test}
\end{figure}
Another thing I would like to try, would be the mapping of the bars via different colors, instead of A=, B=… and so on…
To be honest, pgfplots might be a very powerful tool to create charts, but imho it is very complex and time-consuming to create it! Maybe someone knows a simpler way to create basic charts? (except bchart, there is too less functionality)
I hope someone can help me with this.
Thanks alot in advance!
Best Answer
1. The Labels: you can add the description to the
xticklabels
field. There you can enter the text, and add "normal" linebreaks with\\
. In order for this to work, you'll have to change the style ofxticklabel
to e.g.align=center
(you also do this for "normal" TikZ nodes, if you want linebreaks. You can also make the font smaller, so more words fit on one line:2. The Grid Lines: Adding grid lines is very easy. As decribed on page 330 of the PGFPlots manual, you can create major and minor grids for all 3 axes (x, y, z) independently, or all together:
So you need to set
3. The Percentage Values: As shown in example 77 in the PGFPlots gallery, you can add nodes with
Again, from the documentation, page 114, you'll find that you can even specify the format. The default is
\pgfmathprintnumber\pgfplotspointmeta
, which prints the current point. To add a%
, simply add a%
at the back:4. Standard deviation: You can add a standard deviation, if you also add it to the table (e.g. in a column called
sd
), and then add the following to the\addplot
: (taken from PGFPlots manual, page 293-294):To sum up: Add everything to the axes options, and you're finished:
Different Colors: If you create a new
\addplot
for every bar, then they will be drawn next to each other and in different colors. You can then easily specify a\legend
. As I'm not very familiar with thetables
, I added the plots manually withcoordinates
. Maybe somebody else can give you a hint on how to create such a plot from a table.Final words: Yes, PGFPlots is complex, but it is very powerful. After working with it for some time, you'll have a quite good feeling about what you'll have to look for, and you'll quickly find the relevant information via Google or here on TeX.SE. To be honest: I had to do some quick Google queries for this answer too, but if you know what you're looking for, this is quite fast. So imo, it is worth the time to get to know PGFPlots.
If you have MATLAB, one alternative is to use the Matlab2Tikz script, which converts a MATLAB figure to a PGFPlots graph.