Stacked Bar Chart - How to center values inside/outside the bar?
I modified a code (found here in the forum) to produce a stacked bar chart. Unfortunately I do not understand the whole code…
How can I center the values within the bar chart or right beside the chart? Unfortunately there are values outside the chart (0.430, 0.054, …)!
Furthermore: How can I change the Formate? 0.430 --> 43.00% and so on?
documentclass[a4paper]{scrbook}
usepackage{pgfplotstable}
usepackage[
backend=biber
]
{biblatex}
pgfplotsset{compat=1.8}
centering
scriptsize
pgfplotstableread[col sep=comma,header=true]{
Label,1,2,3
a,0.439,0.130,0.430
b,0.874,0.072,0.054
c,0.757,0.061,0.183
d,0.783,0.139,0.078
e,0.451,0.115,0.434
f,0.398,0.115,0.487
}data
pgfplotstablecreatecol[create col/expr={thisrow{1} + thisrow{2} + thisrow{3}}]{sum}{data}
pgfplotsset{
percentage plot/.style={point meta=explicit,every node near coord/.append style={font=scriptsize,},
nodes near coords={
pgfmathtruncatemacroiszero{originalvalue==0}
ifnumiszero=0
pgfmathprintnumber[fixed,fixed zerofill,precision=3]{pgfplotspointmeta}
fi},
yticklabel=pgfmathprintnumber{tick},$%$,
ymin=0,
ymax=100.01,
visualization depends on={y as originalvalue},
},
percentage series/.style={
table/x expr=coordindex,
table/y expr=(thisrow{#1}/thisrow{sum}*100),
table/meta=#1
}
}
begin{document}
begin{tikzpicture}
begin{axis}[
ybar stacked,
width=0.80textwidth,
height=0.33textheight,
percentage plot,
bar width=7ex,
xticklabels from table={data}{Label},
xtick=data,
x tick label style={align=center},
legend style={draw=none, font=scriptsize},
legend cell align={left},
legend pos=outer north east
]
addplot [fill=black!75] table[percentage series=1] {data};
addplot [fill=black!25] table[percentage series=2] {data};
addplot [fill=black!0] table[percentage series=3] {data};
legend{y, p,n}
end{axis}
end{tikzpicture}
end{document}
Link to original code.
pgfplots bar-chart
add a comment |
I modified a code (found here in the forum) to produce a stacked bar chart. Unfortunately I do not understand the whole code…
How can I center the values within the bar chart or right beside the chart? Unfortunately there are values outside the chart (0.430, 0.054, …)!
Furthermore: How can I change the Formate? 0.430 --> 43.00% and so on?
documentclass[a4paper]{scrbook}
usepackage{pgfplotstable}
usepackage[
backend=biber
]
{biblatex}
pgfplotsset{compat=1.8}
centering
scriptsize
pgfplotstableread[col sep=comma,header=true]{
Label,1,2,3
a,0.439,0.130,0.430
b,0.874,0.072,0.054
c,0.757,0.061,0.183
d,0.783,0.139,0.078
e,0.451,0.115,0.434
f,0.398,0.115,0.487
}data
pgfplotstablecreatecol[create col/expr={thisrow{1} + thisrow{2} + thisrow{3}}]{sum}{data}
pgfplotsset{
percentage plot/.style={point meta=explicit,every node near coord/.append style={font=scriptsize,},
nodes near coords={
pgfmathtruncatemacroiszero{originalvalue==0}
ifnumiszero=0
pgfmathprintnumber[fixed,fixed zerofill,precision=3]{pgfplotspointmeta}
fi},
yticklabel=pgfmathprintnumber{tick},$%$,
ymin=0,
ymax=100.01,
visualization depends on={y as originalvalue},
},
percentage series/.style={
table/x expr=coordindex,
table/y expr=(thisrow{#1}/thisrow{sum}*100),
table/meta=#1
}
}
begin{document}
begin{tikzpicture}
begin{axis}[
ybar stacked,
width=0.80textwidth,
height=0.33textheight,
percentage plot,
bar width=7ex,
xticklabels from table={data}{Label},
xtick=data,
x tick label style={align=center},
legend style={draw=none, font=scriptsize},
legend cell align={left},
legend pos=outer north east
]
addplot [fill=black!75] table[percentage series=1] {data};
addplot [fill=black!25] table[percentage series=2] {data};
addplot [fill=black!0] table[percentage series=3] {data};
legend{y, p,n}
end{axis}
end{tikzpicture}
end{document}
Link to original code.
pgfplots bar-chart
1
Could you please explicitly link to the source of your code? I am also asking for that in order to make sense ofthisrow{sum}
, in your table there is no explicit sum row. This would most likely allow me to address the second part of your question (which I missed in my answer, sorry) in a not too complicated fashion.
– marmot
Dec 9 at 16:06
add a comment |
I modified a code (found here in the forum) to produce a stacked bar chart. Unfortunately I do not understand the whole code…
How can I center the values within the bar chart or right beside the chart? Unfortunately there are values outside the chart (0.430, 0.054, …)!
Furthermore: How can I change the Formate? 0.430 --> 43.00% and so on?
documentclass[a4paper]{scrbook}
usepackage{pgfplotstable}
usepackage[
backend=biber
]
{biblatex}
pgfplotsset{compat=1.8}
centering
scriptsize
pgfplotstableread[col sep=comma,header=true]{
Label,1,2,3
a,0.439,0.130,0.430
b,0.874,0.072,0.054
c,0.757,0.061,0.183
d,0.783,0.139,0.078
e,0.451,0.115,0.434
f,0.398,0.115,0.487
}data
pgfplotstablecreatecol[create col/expr={thisrow{1} + thisrow{2} + thisrow{3}}]{sum}{data}
pgfplotsset{
percentage plot/.style={point meta=explicit,every node near coord/.append style={font=scriptsize,},
nodes near coords={
pgfmathtruncatemacroiszero{originalvalue==0}
ifnumiszero=0
pgfmathprintnumber[fixed,fixed zerofill,precision=3]{pgfplotspointmeta}
fi},
yticklabel=pgfmathprintnumber{tick},$%$,
ymin=0,
ymax=100.01,
visualization depends on={y as originalvalue},
},
percentage series/.style={
table/x expr=coordindex,
table/y expr=(thisrow{#1}/thisrow{sum}*100),
table/meta=#1
}
}
begin{document}
begin{tikzpicture}
begin{axis}[
ybar stacked,
width=0.80textwidth,
height=0.33textheight,
percentage plot,
bar width=7ex,
xticklabels from table={data}{Label},
xtick=data,
x tick label style={align=center},
legend style={draw=none, font=scriptsize},
legend cell align={left},
legend pos=outer north east
]
addplot [fill=black!75] table[percentage series=1] {data};
addplot [fill=black!25] table[percentage series=2] {data};
addplot [fill=black!0] table[percentage series=3] {data};
legend{y, p,n}
end{axis}
end{tikzpicture}
end{document}
Link to original code.
pgfplots bar-chart
I modified a code (found here in the forum) to produce a stacked bar chart. Unfortunately I do not understand the whole code…
How can I center the values within the bar chart or right beside the chart? Unfortunately there are values outside the chart (0.430, 0.054, …)!
Furthermore: How can I change the Formate? 0.430 --> 43.00% and so on?
documentclass[a4paper]{scrbook}
usepackage{pgfplotstable}
usepackage[
backend=biber
]
{biblatex}
pgfplotsset{compat=1.8}
centering
scriptsize
pgfplotstableread[col sep=comma,header=true]{
Label,1,2,3
a,0.439,0.130,0.430
b,0.874,0.072,0.054
c,0.757,0.061,0.183
d,0.783,0.139,0.078
e,0.451,0.115,0.434
f,0.398,0.115,0.487
}data
pgfplotstablecreatecol[create col/expr={thisrow{1} + thisrow{2} + thisrow{3}}]{sum}{data}
pgfplotsset{
percentage plot/.style={point meta=explicit,every node near coord/.append style={font=scriptsize,},
nodes near coords={
pgfmathtruncatemacroiszero{originalvalue==0}
ifnumiszero=0
pgfmathprintnumber[fixed,fixed zerofill,precision=3]{pgfplotspointmeta}
fi},
yticklabel=pgfmathprintnumber{tick},$%$,
ymin=0,
ymax=100.01,
visualization depends on={y as originalvalue},
},
percentage series/.style={
table/x expr=coordindex,
table/y expr=(thisrow{#1}/thisrow{sum}*100),
table/meta=#1
}
}
begin{document}
begin{tikzpicture}
begin{axis}[
ybar stacked,
width=0.80textwidth,
height=0.33textheight,
percentage plot,
bar width=7ex,
xticklabels from table={data}{Label},
xtick=data,
x tick label style={align=center},
legend style={draw=none, font=scriptsize},
legend cell align={left},
legend pos=outer north east
]
addplot [fill=black!75] table[percentage series=1] {data};
addplot [fill=black!25] table[percentage series=2] {data};
addplot [fill=black!0] table[percentage series=3] {data};
legend{y, p,n}
end{axis}
end{tikzpicture}
end{document}
Link to original code.
pgfplots bar-chart
pgfplots bar-chart
edited Dec 9 at 16:29
asked Dec 9 at 15:43
TRJW
1847
1847
1
Could you please explicitly link to the source of your code? I am also asking for that in order to make sense ofthisrow{sum}
, in your table there is no explicit sum row. This would most likely allow me to address the second part of your question (which I missed in my answer, sorry) in a not too complicated fashion.
– marmot
Dec 9 at 16:06
add a comment |
1
Could you please explicitly link to the source of your code? I am also asking for that in order to make sense ofthisrow{sum}
, in your table there is no explicit sum row. This would most likely allow me to address the second part of your question (which I missed in my answer, sorry) in a not too complicated fashion.
– marmot
Dec 9 at 16:06
1
1
Could you please explicitly link to the source of your code? I am also asking for that in order to make sense of
thisrow{sum}
, in your table there is no explicit sum row. This would most likely allow me to address the second part of your question (which I missed in my answer, sorry) in a not too complicated fashion.– marmot
Dec 9 at 16:06
Could you please explicitly link to the source of your code? I am also asking for that in order to make sense of
thisrow{sum}
, in your table there is no explicit sum row. This would most likely allow me to address the second part of your question (which I missed in my answer, sorry) in a not too complicated fashion.– marmot
Dec 9 at 16:06
add a comment |
1 Answer
1
active
oldest
votes
You only need to change the anchor of the nodes near coords
. (I also changed the text color of the lowest nodes for a better readability.)
documentclass[a4paper]{scrbook}
usepackage{pgfplotstable}
usepackage[
backend=biber
]
{biblatex}
pgfplotsset{compat=1.8}
centering
scriptsize
pgfplotstableread[col sep=comma,header=true]{
Label,1,2,3
a,0.439,0.130,0.430
b,0.874,0.072,0.054
c,0.757,0.061,0.183
d,0.783,0.139,0.078
e,0.451,0.115,0.434
f,0.398,0.115,0.487
}data
pgfplotstablecreatecol[create col/expr={thisrow{1} + thisrow{2} + thisrow{3}}]{sum}{data}
pgfplotsset{
percentage plot/.style={point meta=explicit,
every node near coord/.append style={font=scriptsize,anchor=north,inner
ysep=1pt},
nodes near coords={
pgfmathtruncatemacroiszero{originalvalue==0}
ifnumiszero=0
pgfmathprintnumber[fixed,fixed zerofill,precision=3]{pgfplotspointmeta}
fi},
yticklabel=pgfmathprintnumber{tick},$%$,
ymin=0,
ymax=100.01,
visualization depends on={y as originalvalue},
},
percentage series/.style={
table/x expr=coordindex,
table/y expr=(thisrow{#1}/thisrow{sum}*100),
table/meta=#1
}
}
begin{document}
begin{tikzpicture}
begin{axis}[
ybar stacked,
width=0.80textwidth,
height=0.33textheight,
percentage plot,
bar width=7ex,
xticklabels from table={data}{Label},
xtick=data,
x tick label style={align=center},
legend style={draw=none, font=scriptsize},
legend cell align={left},
legend pos=outer north east
]
addplot [fill=black!75,text=white] table[percentage series=1] {data};
addplot [fill=black!25] table[percentage series=2] {data};
addplot [fill=black!0] table[percentage series=3] {data};
legend{y, p,n}
end{axis}
end{tikzpicture}
end{document}
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f463964%2fstacked-bar-chart-how-to-center-values-inside-outside-the-bar%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You only need to change the anchor of the nodes near coords
. (I also changed the text color of the lowest nodes for a better readability.)
documentclass[a4paper]{scrbook}
usepackage{pgfplotstable}
usepackage[
backend=biber
]
{biblatex}
pgfplotsset{compat=1.8}
centering
scriptsize
pgfplotstableread[col sep=comma,header=true]{
Label,1,2,3
a,0.439,0.130,0.430
b,0.874,0.072,0.054
c,0.757,0.061,0.183
d,0.783,0.139,0.078
e,0.451,0.115,0.434
f,0.398,0.115,0.487
}data
pgfplotstablecreatecol[create col/expr={thisrow{1} + thisrow{2} + thisrow{3}}]{sum}{data}
pgfplotsset{
percentage plot/.style={point meta=explicit,
every node near coord/.append style={font=scriptsize,anchor=north,inner
ysep=1pt},
nodes near coords={
pgfmathtruncatemacroiszero{originalvalue==0}
ifnumiszero=0
pgfmathprintnumber[fixed,fixed zerofill,precision=3]{pgfplotspointmeta}
fi},
yticklabel=pgfmathprintnumber{tick},$%$,
ymin=0,
ymax=100.01,
visualization depends on={y as originalvalue},
},
percentage series/.style={
table/x expr=coordindex,
table/y expr=(thisrow{#1}/thisrow{sum}*100),
table/meta=#1
}
}
begin{document}
begin{tikzpicture}
begin{axis}[
ybar stacked,
width=0.80textwidth,
height=0.33textheight,
percentage plot,
bar width=7ex,
xticklabels from table={data}{Label},
xtick=data,
x tick label style={align=center},
legend style={draw=none, font=scriptsize},
legend cell align={left},
legend pos=outer north east
]
addplot [fill=black!75,text=white] table[percentage series=1] {data};
addplot [fill=black!25] table[percentage series=2] {data};
addplot [fill=black!0] table[percentage series=3] {data};
legend{y, p,n}
end{axis}
end{tikzpicture}
end{document}
add a comment |
You only need to change the anchor of the nodes near coords
. (I also changed the text color of the lowest nodes for a better readability.)
documentclass[a4paper]{scrbook}
usepackage{pgfplotstable}
usepackage[
backend=biber
]
{biblatex}
pgfplotsset{compat=1.8}
centering
scriptsize
pgfplotstableread[col sep=comma,header=true]{
Label,1,2,3
a,0.439,0.130,0.430
b,0.874,0.072,0.054
c,0.757,0.061,0.183
d,0.783,0.139,0.078
e,0.451,0.115,0.434
f,0.398,0.115,0.487
}data
pgfplotstablecreatecol[create col/expr={thisrow{1} + thisrow{2} + thisrow{3}}]{sum}{data}
pgfplotsset{
percentage plot/.style={point meta=explicit,
every node near coord/.append style={font=scriptsize,anchor=north,inner
ysep=1pt},
nodes near coords={
pgfmathtruncatemacroiszero{originalvalue==0}
ifnumiszero=0
pgfmathprintnumber[fixed,fixed zerofill,precision=3]{pgfplotspointmeta}
fi},
yticklabel=pgfmathprintnumber{tick},$%$,
ymin=0,
ymax=100.01,
visualization depends on={y as originalvalue},
},
percentage series/.style={
table/x expr=coordindex,
table/y expr=(thisrow{#1}/thisrow{sum}*100),
table/meta=#1
}
}
begin{document}
begin{tikzpicture}
begin{axis}[
ybar stacked,
width=0.80textwidth,
height=0.33textheight,
percentage plot,
bar width=7ex,
xticklabels from table={data}{Label},
xtick=data,
x tick label style={align=center},
legend style={draw=none, font=scriptsize},
legend cell align={left},
legend pos=outer north east
]
addplot [fill=black!75,text=white] table[percentage series=1] {data};
addplot [fill=black!25] table[percentage series=2] {data};
addplot [fill=black!0] table[percentage series=3] {data};
legend{y, p,n}
end{axis}
end{tikzpicture}
end{document}
add a comment |
You only need to change the anchor of the nodes near coords
. (I also changed the text color of the lowest nodes for a better readability.)
documentclass[a4paper]{scrbook}
usepackage{pgfplotstable}
usepackage[
backend=biber
]
{biblatex}
pgfplotsset{compat=1.8}
centering
scriptsize
pgfplotstableread[col sep=comma,header=true]{
Label,1,2,3
a,0.439,0.130,0.430
b,0.874,0.072,0.054
c,0.757,0.061,0.183
d,0.783,0.139,0.078
e,0.451,0.115,0.434
f,0.398,0.115,0.487
}data
pgfplotstablecreatecol[create col/expr={thisrow{1} + thisrow{2} + thisrow{3}}]{sum}{data}
pgfplotsset{
percentage plot/.style={point meta=explicit,
every node near coord/.append style={font=scriptsize,anchor=north,inner
ysep=1pt},
nodes near coords={
pgfmathtruncatemacroiszero{originalvalue==0}
ifnumiszero=0
pgfmathprintnumber[fixed,fixed zerofill,precision=3]{pgfplotspointmeta}
fi},
yticklabel=pgfmathprintnumber{tick},$%$,
ymin=0,
ymax=100.01,
visualization depends on={y as originalvalue},
},
percentage series/.style={
table/x expr=coordindex,
table/y expr=(thisrow{#1}/thisrow{sum}*100),
table/meta=#1
}
}
begin{document}
begin{tikzpicture}
begin{axis}[
ybar stacked,
width=0.80textwidth,
height=0.33textheight,
percentage plot,
bar width=7ex,
xticklabels from table={data}{Label},
xtick=data,
x tick label style={align=center},
legend style={draw=none, font=scriptsize},
legend cell align={left},
legend pos=outer north east
]
addplot [fill=black!75,text=white] table[percentage series=1] {data};
addplot [fill=black!25] table[percentage series=2] {data};
addplot [fill=black!0] table[percentage series=3] {data};
legend{y, p,n}
end{axis}
end{tikzpicture}
end{document}
You only need to change the anchor of the nodes near coords
. (I also changed the text color of the lowest nodes for a better readability.)
documentclass[a4paper]{scrbook}
usepackage{pgfplotstable}
usepackage[
backend=biber
]
{biblatex}
pgfplotsset{compat=1.8}
centering
scriptsize
pgfplotstableread[col sep=comma,header=true]{
Label,1,2,3
a,0.439,0.130,0.430
b,0.874,0.072,0.054
c,0.757,0.061,0.183
d,0.783,0.139,0.078
e,0.451,0.115,0.434
f,0.398,0.115,0.487
}data
pgfplotstablecreatecol[create col/expr={thisrow{1} + thisrow{2} + thisrow{3}}]{sum}{data}
pgfplotsset{
percentage plot/.style={point meta=explicit,
every node near coord/.append style={font=scriptsize,anchor=north,inner
ysep=1pt},
nodes near coords={
pgfmathtruncatemacroiszero{originalvalue==0}
ifnumiszero=0
pgfmathprintnumber[fixed,fixed zerofill,precision=3]{pgfplotspointmeta}
fi},
yticklabel=pgfmathprintnumber{tick},$%$,
ymin=0,
ymax=100.01,
visualization depends on={y as originalvalue},
},
percentage series/.style={
table/x expr=coordindex,
table/y expr=(thisrow{#1}/thisrow{sum}*100),
table/meta=#1
}
}
begin{document}
begin{tikzpicture}
begin{axis}[
ybar stacked,
width=0.80textwidth,
height=0.33textheight,
percentage plot,
bar width=7ex,
xticklabels from table={data}{Label},
xtick=data,
x tick label style={align=center},
legend style={draw=none, font=scriptsize},
legend cell align={left},
legend pos=outer north east
]
addplot [fill=black!75,text=white] table[percentage series=1] {data};
addplot [fill=black!25] table[percentage series=2] {data};
addplot [fill=black!0] table[percentage series=3] {data};
legend{y, p,n}
end{axis}
end{tikzpicture}
end{document}
answered Dec 9 at 15:54
marmot
85.9k499183
85.9k499183
add a comment |
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f463964%2fstacked-bar-chart-how-to-center-values-inside-outside-the-bar%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Could you please explicitly link to the source of your code? I am also asking for that in order to make sense of
thisrow{sum}
, in your table there is no explicit sum row. This would most likely allow me to address the second part of your question (which I missed in my answer, sorry) in a not too complicated fashion.– marmot
Dec 9 at 16:06