Discussion:
Help with double-axis bar graph (x, y, y1) and different scale.
Henry Francisco Soria-Díaz
2018-08-20 02:08:23 UTC
Permalink
Hello,

I would like to know how I can make a double-axis bar graph (x, y, y1) with
different scales with the purpose of comparing a suppository value (Price
per year) and a real value (Price per year) of each categorical variable
(type=Tipo). That is, how to add a secondary axis (y1 = Valor_Carbon):

Tipo Valor_SIMB Valor_Carbon
1 SEMMAS 1.452.400 2.120.087,1
2 SEMTEC 24.028 25.285,1
3 DA_ONÇA 3.105 138.373,4
... ........... ...... .....


For example: p <- ggplot (daa, aes (x = Tipo, y = Value_SIMB, fill = Tipo))
+ geom_bar (stat = "identity")
p

[image: exemplo1.jpg] <about:invalid#zClosurez>

I can not add the second value (y1 = Valor_Carbon) in the graph, but with a
different scale. Also, how can I sort the values in ascending order
(2000-1600000) on the "Y" axis, and how could I correct the names of the
labels and the legend so that they are more visible and coherent?

I will be grateful for your help and kindness.

Sincerely,

Henry
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thierry Onkelinx
2018-08-20 11:57:35 UTC
Permalink
Dear Henry,

The seconday axis in ggplot() is just a transformation of the first axis.
See https://ggplot2.tidyverse.org/reference/sec_axis.html. Think like all
data in degree Celcius, the first axis in degree Celcius and the second in
Kelvin. Hence you'll need to convert Valor_calor to the same scale as
Valor_SIMB and then convert the data into a long format, e.g. with
tidyr::gather (https://tidyr.tidyverse.org/reference/gather.html)

The order of the levels in "Typo" defines the order in the plot. You can
alter that with reorder(). The levels are used as levels. So the best fix
is to use the correct labels in the factor. Changing how the labels are
displayed is done with theme(axis.text.x = element_text()). See
https://ggplot2.tidyverse.org/reference/element.html

Best regards,


ir. Thierry Onkelinx
Statisticus / Statistician

Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND
FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
***@inbo.be
Havenlaan 88 bus 73, 1000 Brussel
www.inbo.be

///////////////////////////////////////////////////////////////////////////////////////////
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
///////////////////////////////////////////////////////////////////////////////////////////

<https://www.inbo.be>
Post by Henry Francisco Soria-Díaz
Hello,
I would like to know how I can make a double-axis bar graph (x, y, y1)
with different scales with the purpose of comparing a suppository value
(Price per year) and a real value (Price per year) of each categorical
variable (type=Tipo). That is, how to add a secondary axis (y1 =
Tipo Valor_SIMB Valor_Carbon
1 SEMMAS 1.452.400 2.120.087,1
2 SEMTEC 24.028 25.285,1
3 DA_ONÇA 3.105 138.373,4
... ........... ...... .....
For example: p <- ggplot (daa, aes (x = Tipo, y = Value_SIMB, fill =
Tipo)) + geom_bar (stat = "identity")
p
[image: exemplo1.jpg]
I can not add the second value (y1 = Valor_Carbon) in the graph, but with
a different scale. Also, how can I sort the values in ascending order
(2000-1600000) on the "Y" axis, and how could I correct the names of the
labels and the legend so that they are more visible and coherent?
I will be grateful for your help and kindness.
Sincerely,
Henry
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/
devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Henry Francisco Soria Díaz
2018-08-22 02:43:01 UTC
Permalink
Dear Thierry,

Thank you very much for your response and help.

Grateful,

Henry




Em seg, 20 de ago de 2018 às 07:57, Thierry Onkelinx <
Post by Thierry Onkelinx
Dear Henry,
The seconday axis in ggplot() is just a transformation of the first axis.
See https://ggplot2.tidyverse.org/reference/sec_axis.html. Think like all
data in degree Celcius, the first axis in degree Celcius and the second in
Kelvin. Hence you'll need to convert Valor_calor to the same scale as
Valor_SIMB and then convert the data into a long format, e.g. with
tidyr::gather (https://tidyr.tidyverse.org/reference/gather.html)
The order of the levels in "Typo" defines the order in the plot. You can
alter that with reorder(). The levels are used as levels. So the best fix
is to use the correct labels in the factor. Changing how the labels are
displayed is done with theme(axis.text.x = element_text()). See
https://ggplot2.tidyverse.org/reference/element.html
Best regards,
ir. Thierry Onkelinx
Statisticus / Statistician
Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND
FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
Havenlaan 88 bus 73, 1000 Brussel
www.inbo.be
///////////////////////////////////////////////////////////////////////////////////////////
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
///////////////////////////////////////////////////////////////////////////////////////////
<https://www.inbo.be>
Post by Henry Francisco Soria-Díaz
Hello,
I would like to know how I can make a double-axis bar graph (x, y, y1)
with different scales with the purpose of comparing a suppository value
(Price per year) and a real value (Price per year) of each categorical
variable (type=Tipo). That is, how to add a secondary axis (y1 =
Tipo Valor_SIMB Valor_Carbon
1 SEMMAS 1.452.400 2.120.087,1
2 SEMTEC 24.028 25.285,1
3 DA_ONÇA 3.105 138.373,4
... ........... ...... .....
For example: p <- ggplot (daa, aes (x = Tipo, y = Value_SIMB, fill =
Tipo)) + geom_bar (stat = "identity")
p
[image: exemplo1.jpg]
I can not add the second value (y1 = Valor_Carbon) in the graph, but with
a different scale. Also, how can I sort the values in ascending order
(2000-1600000) on the "Y" axis, and how could I correct the names of the
labels and the legend so that they are more visible and coherent?
I will be grateful for your help and kindness.
Sincerely,
Henry
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups
"ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...