Multi-line legend text including exponent with ggplot
With ggplot, I want to add a left aligned legend title with multiple lines and exponents in the text for the units of the values in the legend. I'm plotting data of a form similar to:
leakage_rates_levels <- c(5.4, 0.25)
leakage_rates <- as.factor(rep(leakage_rates_levels, 3)) # L/s-m^2 at 75 Pa
data_groups_levels <- c('Set 1', 'Set 2', 'Set 3')
data_groups <- as.factor(rep(data_groups_levels, each=2))
moisture_level <- c(7, 3, 11, 10, 16, 6)
plotdt <- data.frame(data_groups, leakage_rates, moisture_level)
I use expression()
to add exponents to the units in the legend. The following code generates the desired figure, but with the legend title text mis-formatted.
ggplot(plotdt, aes(data_groups)) +
geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
labs(y='Moisture Level') +
labs(fill=expression(paste('Leakage Ratenat 75 Pan(L/s-', m^2, ')', sep=''))) +
theme(panel.grid.major.x = element_blank(),
axis.title.x = element_blank())
The legend title appears left aligned except for the final line, which has a bunch of extraneous spaces in the middle of it.
Using legend_title_align=0
(suggested here) and/or legend_title=element_text(hjust=1)
in theme()
have no effect. Trying to add phantom()
spacing also did not work (suggested here). The end of the top answer to this question notes the same problem I'm encountering but does not propose a solution.
Is there a way to get the meter squared term in the legend to be left-aligned like the rest of the text?
I am using ggplot 3.1.0 and R 3.5.1.
r ggplot2
add a comment |
With ggplot, I want to add a left aligned legend title with multiple lines and exponents in the text for the units of the values in the legend. I'm plotting data of a form similar to:
leakage_rates_levels <- c(5.4, 0.25)
leakage_rates <- as.factor(rep(leakage_rates_levels, 3)) # L/s-m^2 at 75 Pa
data_groups_levels <- c('Set 1', 'Set 2', 'Set 3')
data_groups <- as.factor(rep(data_groups_levels, each=2))
moisture_level <- c(7, 3, 11, 10, 16, 6)
plotdt <- data.frame(data_groups, leakage_rates, moisture_level)
I use expression()
to add exponents to the units in the legend. The following code generates the desired figure, but with the legend title text mis-formatted.
ggplot(plotdt, aes(data_groups)) +
geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
labs(y='Moisture Level') +
labs(fill=expression(paste('Leakage Ratenat 75 Pan(L/s-', m^2, ')', sep=''))) +
theme(panel.grid.major.x = element_blank(),
axis.title.x = element_blank())
The legend title appears left aligned except for the final line, which has a bunch of extraneous spaces in the middle of it.
Using legend_title_align=0
(suggested here) and/or legend_title=element_text(hjust=1)
in theme()
have no effect. Trying to add phantom()
spacing also did not work (suggested here). The end of the top answer to this question notes the same problem I'm encountering but does not propose a solution.
Is there a way to get the meter squared term in the legend to be left-aligned like the rest of the text?
I am using ggplot 3.1.0 and R 3.5.1.
r ggplot2
add a comment |
With ggplot, I want to add a left aligned legend title with multiple lines and exponents in the text for the units of the values in the legend. I'm plotting data of a form similar to:
leakage_rates_levels <- c(5.4, 0.25)
leakage_rates <- as.factor(rep(leakage_rates_levels, 3)) # L/s-m^2 at 75 Pa
data_groups_levels <- c('Set 1', 'Set 2', 'Set 3')
data_groups <- as.factor(rep(data_groups_levels, each=2))
moisture_level <- c(7, 3, 11, 10, 16, 6)
plotdt <- data.frame(data_groups, leakage_rates, moisture_level)
I use expression()
to add exponents to the units in the legend. The following code generates the desired figure, but with the legend title text mis-formatted.
ggplot(plotdt, aes(data_groups)) +
geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
labs(y='Moisture Level') +
labs(fill=expression(paste('Leakage Ratenat 75 Pan(L/s-', m^2, ')', sep=''))) +
theme(panel.grid.major.x = element_blank(),
axis.title.x = element_blank())
The legend title appears left aligned except for the final line, which has a bunch of extraneous spaces in the middle of it.
Using legend_title_align=0
(suggested here) and/or legend_title=element_text(hjust=1)
in theme()
have no effect. Trying to add phantom()
spacing also did not work (suggested here). The end of the top answer to this question notes the same problem I'm encountering but does not propose a solution.
Is there a way to get the meter squared term in the legend to be left-aligned like the rest of the text?
I am using ggplot 3.1.0 and R 3.5.1.
r ggplot2
With ggplot, I want to add a left aligned legend title with multiple lines and exponents in the text for the units of the values in the legend. I'm plotting data of a form similar to:
leakage_rates_levels <- c(5.4, 0.25)
leakage_rates <- as.factor(rep(leakage_rates_levels, 3)) # L/s-m^2 at 75 Pa
data_groups_levels <- c('Set 1', 'Set 2', 'Set 3')
data_groups <- as.factor(rep(data_groups_levels, each=2))
moisture_level <- c(7, 3, 11, 10, 16, 6)
plotdt <- data.frame(data_groups, leakage_rates, moisture_level)
I use expression()
to add exponents to the units in the legend. The following code generates the desired figure, but with the legend title text mis-formatted.
ggplot(plotdt, aes(data_groups)) +
geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
labs(y='Moisture Level') +
labs(fill=expression(paste('Leakage Ratenat 75 Pan(L/s-', m^2, ')', sep=''))) +
theme(panel.grid.major.x = element_blank(),
axis.title.x = element_blank())
The legend title appears left aligned except for the final line, which has a bunch of extraneous spaces in the middle of it.
Using legend_title_align=0
(suggested here) and/or legend_title=element_text(hjust=1)
in theme()
have no effect. Trying to add phantom()
spacing also did not work (suggested here). The end of the top answer to this question notes the same problem I'm encountering but does not propose a solution.
Is there a way to get the meter squared term in the legend to be left-aligned like the rest of the text?
I am using ggplot 3.1.0 and R 3.5.1.
r ggplot2
r ggplot2
asked Nov 18 at 19:30
trynthink
101210
101210
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use the unicode representation of superscript two (U+00B2) and avoid the
problem-causing combination of expression()
and a multi-line legend title:
ggplot(plotdt, aes(data_groups)) +
geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
labs(y='Moisture Level') +
labs(fill=paste('Leakage Ratenat 75 Pan(L/s-mu00b2)', sep='')) +
theme(panel.grid.major.x = element_blank(),
axis.title.x = element_blank())
add a comment |
You can use atop
to have lines "atop" each other.
Because you have 3 lines and atop
only accepts 2 arguments however, you need to have 2 atop
nested in one another. This makes the font on some of the lines smaller. The way to prevent this is to pass the expressions to either textstyle
or displaystyle
:
ggplot(plotdt, aes(data_groups)) +
geom_bar(aes(weight = moisture_level, fill = leakage_rates), position = "dodge") +
labs(y = "Moisture Level") +
labs(fill = expression(atop(atop(textstyle("Leakage Rate"),
textstyle("at 75 Pa")),
"(L/s-" ~m^2~ ")"))) +
theme(panel.grid.major.x = element_blank(), axis.title.x = element_blank())
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53364672%2fmulti-line-legend-text-including-exponent-with-ggplot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the unicode representation of superscript two (U+00B2) and avoid the
problem-causing combination of expression()
and a multi-line legend title:
ggplot(plotdt, aes(data_groups)) +
geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
labs(y='Moisture Level') +
labs(fill=paste('Leakage Ratenat 75 Pan(L/s-mu00b2)', sep='')) +
theme(panel.grid.major.x = element_blank(),
axis.title.x = element_blank())
add a comment |
You can use the unicode representation of superscript two (U+00B2) and avoid the
problem-causing combination of expression()
and a multi-line legend title:
ggplot(plotdt, aes(data_groups)) +
geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
labs(y='Moisture Level') +
labs(fill=paste('Leakage Ratenat 75 Pan(L/s-mu00b2)', sep='')) +
theme(panel.grid.major.x = element_blank(),
axis.title.x = element_blank())
add a comment |
You can use the unicode representation of superscript two (U+00B2) and avoid the
problem-causing combination of expression()
and a multi-line legend title:
ggplot(plotdt, aes(data_groups)) +
geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
labs(y='Moisture Level') +
labs(fill=paste('Leakage Ratenat 75 Pan(L/s-mu00b2)', sep='')) +
theme(panel.grid.major.x = element_blank(),
axis.title.x = element_blank())
You can use the unicode representation of superscript two (U+00B2) and avoid the
problem-causing combination of expression()
and a multi-line legend title:
ggplot(plotdt, aes(data_groups)) +
geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
labs(y='Moisture Level') +
labs(fill=paste('Leakage Ratenat 75 Pan(L/s-mu00b2)', sep='')) +
theme(panel.grid.major.x = element_blank(),
axis.title.x = element_blank())
answered Nov 18 at 20:33
Mr. Zen
483215
483215
add a comment |
add a comment |
You can use atop
to have lines "atop" each other.
Because you have 3 lines and atop
only accepts 2 arguments however, you need to have 2 atop
nested in one another. This makes the font on some of the lines smaller. The way to prevent this is to pass the expressions to either textstyle
or displaystyle
:
ggplot(plotdt, aes(data_groups)) +
geom_bar(aes(weight = moisture_level, fill = leakage_rates), position = "dodge") +
labs(y = "Moisture Level") +
labs(fill = expression(atop(atop(textstyle("Leakage Rate"),
textstyle("at 75 Pa")),
"(L/s-" ~m^2~ ")"))) +
theme(panel.grid.major.x = element_blank(), axis.title.x = element_blank())
add a comment |
You can use atop
to have lines "atop" each other.
Because you have 3 lines and atop
only accepts 2 arguments however, you need to have 2 atop
nested in one another. This makes the font on some of the lines smaller. The way to prevent this is to pass the expressions to either textstyle
or displaystyle
:
ggplot(plotdt, aes(data_groups)) +
geom_bar(aes(weight = moisture_level, fill = leakage_rates), position = "dodge") +
labs(y = "Moisture Level") +
labs(fill = expression(atop(atop(textstyle("Leakage Rate"),
textstyle("at 75 Pa")),
"(L/s-" ~m^2~ ")"))) +
theme(panel.grid.major.x = element_blank(), axis.title.x = element_blank())
add a comment |
You can use atop
to have lines "atop" each other.
Because you have 3 lines and atop
only accepts 2 arguments however, you need to have 2 atop
nested in one another. This makes the font on some of the lines smaller. The way to prevent this is to pass the expressions to either textstyle
or displaystyle
:
ggplot(plotdt, aes(data_groups)) +
geom_bar(aes(weight = moisture_level, fill = leakage_rates), position = "dodge") +
labs(y = "Moisture Level") +
labs(fill = expression(atop(atop(textstyle("Leakage Rate"),
textstyle("at 75 Pa")),
"(L/s-" ~m^2~ ")"))) +
theme(panel.grid.major.x = element_blank(), axis.title.x = element_blank())
You can use atop
to have lines "atop" each other.
Because you have 3 lines and atop
only accepts 2 arguments however, you need to have 2 atop
nested in one another. This makes the font on some of the lines smaller. The way to prevent this is to pass the expressions to either textstyle
or displaystyle
:
ggplot(plotdt, aes(data_groups)) +
geom_bar(aes(weight = moisture_level, fill = leakage_rates), position = "dodge") +
labs(y = "Moisture Level") +
labs(fill = expression(atop(atop(textstyle("Leakage Rate"),
textstyle("at 75 Pa")),
"(L/s-" ~m^2~ ")"))) +
theme(panel.grid.major.x = element_blank(), axis.title.x = element_blank())
edited Nov 19 at 1:45
answered Nov 18 at 21:44
prosoitos
910219
910219
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53364672%2fmulti-line-legend-text-including-exponent-with-ggplot%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