Multi-line legend text including exponent with ggplot
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
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 |
up vote
6
down vote
favorite
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 |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
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
up vote
4
down vote
accepted
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 |
up vote
1
down vote
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 |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
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 |
up vote
4
down vote
accepted
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 |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
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
419214
419214
add a comment |
add a comment |
up vote
1
down vote
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 |
up vote
1
down vote
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 |
up vote
1
down vote
up vote
1
down vote
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
814219
814219
add a comment |
add a comment |
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