9
1 - Contextualization
I want to build a bar chart using the ggplot2
, however I am having difficulties to gather the information and to make the graphic edition. I have data of the oscillation of the phenomenon El Niño (ONI) classified in intervals. With this, I would like to construct a chart like this:
But instead use areas, use bars, reversing X and Y, adding only the lines of the intensity classes (Weak, Moderate, Strong and Very Strong) with their respective captions (annotations).
2 - Data
Considering a sample with 50-month measurements of OMN data, the variables are: Data
, the index(ONI
) and the phase of Fenomeno
(El Nino, Normal and La Niña), arranged in a data frame.:
ONIenla<-data.frame(Data=c("1950-01-31","1950-02-28","1950-03-31","1950-04-30","1950-05-31","1950-06-30","1950-07-31","1950-08-31","1950-09-30","1950-10-31","1950-11-30","1950-12-31","1951-01-31","1951-02-28","1951-03-31","1951-04-30","1951-05-31","1951-06-30","1951-07-31","1951-08-31","1951-09-30","1951-10-31","1951-11-30","1951-12-31","1952-01-31","1952-02-29","1952-03-31","1952-04-30","1952-05-31","1952-06-30","1952-07-31","1952-08-31","1952-09-30","1952-10-31","1952-11-30","1952-12-31","1953-01-31","1953-02-28","1953-03-31","1953-04-30","1953-05-31","1953-06-30","1953-07-31","1953-08-31","1953-09-30","1953-10-31","1953-11-30","1953-12-31","1954-01-31","1954-02-28"),ONI = c(-1.4,-1.2,-1.1,-1.2,-1.1,-0.9,-0.6,-0.6,-0.5,-0.6,-0.7,-0.8,-0.8,-0.6,-0.2,0.2,0.2,0.4,0.5,0.7,0.8,0.9,0.7,0.6,0.5,0.4,0.4,0.4,0.4,0.2,0.0,0.1,0.2,0.2,0.2,0.3,0.5,0.6,0.7,0.7,0.7,0.7,0.7,0.7,0.8,0.8,0.8,0.7,0.7,0.4), Fenomeno = c("La Nina","La Nina","La Nina","La Nina","La Nina","La Nina","La Nina","La Nina","La Nina","La Nina","La Nina","La Nina","La Nina","La Nina","La Nina","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","Normal","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino","El Nino"))
The classes:
classesONI<-data.frame(ClasAno=c("Fraca","Moderado","Forte","MuitoForte"),MimEl=c(0.5,1,1.5,2),MaxEl=c(0.99,1.49,1.99,NA),MimLA=c(-0.5,-1,-1.5,-2),MaxLA=c(-0.99,-1.49,-1.99,NA))
3 - Current script
I used two libraries a ggplot2 and a lubridate
.
library(ggplot2)
library(lubridate)
dados$Data<-ymd(dados$Data)
grafico<- ggplot(ONIenla,aes(x=Data,y=ONI,guide=F)) + geom_bar(stat='identity', position='dodge',aes(color=Fenomeno, group=1)) + coord_flip() + ggtitle("Índice oceâncico Niño (ONI)") + scale_fill_hue('Legenda') + scale_x_date() + theme(panel.grid.major = lement_blank(),panel.grid.minor = element_blank()) + theme(legend.position="none")
4 - Editions
What form of:
insert phase legend: El Niño, La Niña and Normal;
edit bar color: El Niño=Red, La Niña= Blue and Normal= Green;
insert the lines of the classes and their annotations next to them (tip: check in the example graph, the red and blue lines, with their annotations (Weak, moderate, Strong and very Strong);
insert a frame around the image;
edit graphic background to blank;
step dates of 5 years;
reverse the order of dates.
Grateful for the suggestions.
can solve....
– Jean
what is the object
dados
in your current script? I am unable to play.– Daniel Falbel
I wrote in a hurry, I will correct the gafs. Data is actually Onienla. And I still mention IOS, which is ONI. This happened because I received a tip to change the indicator of my analysis, something that at the last moment made me overlook these details. My apologies!
– Jean