Help with double-axis bar graph (x, y, y1) and different scale.

21 views
Skip to first unread message

Henry Francisco Soria-Díaz

unread,
Aug 20, 2018, 7:08:15 AM8/20/18
to ggplot2
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

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

Thierry Onkelinx

unread,
Aug 20, 2018, 7:57:49 AM8/20/18
to Henry Francisco Soria-Díaz, ggplot2
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
thierry....@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
///////////////////////////////////////////////////////////////////////////////////////////



--
--
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 ggp...@googlegroups.com
To unsubscribe: email ggplot2+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Henry Francisco Soria Díaz

unread,
Aug 21, 2018, 10:43:28 PM8/21/18
to thierry....@inbo.be, ggp...@googlegroups.com
Dear Thierry,

Thank you very much for your response and help.

Grateful,

Henry




To unsubscribe: email ggplot2+u...@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+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages