Reputation: 590
I'm trying to do this plot (1st plot) but it doesn't show the negatives percentages on the secondary axis using pretty_breaks()
inside scale_y_continuous
and sec_axis
.
scale_y_continuous(sec.axis=sec_axis(breaks = pretty_breaks(), labels = label_percent()), breaks = pretty_breaks(), labels = label_dollar())
.
And I need to show like this.
library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)
test = structure(list(Año = c(2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 2019L, 2019L,
2019L, 2019L, 2019L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L
), Vars = c("Oil.GAV", "Non-Oil.GAV", "Other.GDP.elements", "Oil.GAV.%",
"Non-Oil.GAV.%", "Other.GDP.elements.%", "Oil.GAV", "Non-Oil.GAV",
"Other.GDP.elements", "Oil.GAV.%", "Non-Oil.GAV.%", "Other.GDP.elements.%",
"Oil.GAV", "Non-Oil.GAV", "Other.GDP.elements", "Oil.GAV.%",
"Non-Oil.GAV.%", "Other.GDP.elements.%", "Oil.GAV", "Non-Oil.GAV",
"Other.GDP.elements", "Oil.GAV.%", "Non-Oil.GAV.%", "Other.GDP.elements.%"
), value = c(5639977, 91442756, 7213129, 0.0987732330025509,
0.869107003693333, 0.0321197633041161, 6918891, 93037062, 7606055,
0.0913116709595953, 0.875434164471086, 0.0332541645693185, 6519993,
94351584, 7236432, 0.0923079337383433, 0.876825035531481, 0.0308670307301762,
4951333, 88365192, 5491485, -0.06, -0.35, -0.078)), row.names = c(NA,
-24L), class = c("tbl_df", "tbl", "data.frame"))
scaleFactor_t <- max(test %>%
filter(Vars %in% c("Oil.GAV", "Non-Oil.GAV", "Other.GDP.elements")) %>%
select(value)) / max(test %>%
filter(!Vars %in% c("Oil.GAV", "Non-Oil.GAV", "Other.GDP.elements")) %>%
select(value))
ggplot() +
geom_col(data = test %>%
filter(Vars %in% c("Oil.GAV", "Non-Oil.GAV", "Other.GDP.elements")),
aes(x = Año, y = value , fill = Vars))+
geom_line(data = test %>%
filter(!Vars %in% c("Oil.GAV", "Non-Oil.GAV", "Other.GDP.elements")),
aes(x = Año, y = value*scaleFactor_t, colour = Vars), size = 1)+
geom_point(data = test %>%
filter(!Vars %in% c("Oil.GAV", "Non-Oil.GAV", "Other.GDP.elements")),
aes(x = Año, y = value*scaleFactor_t, colour = Vars), size = 2)+
scale_y_continuous(name="Miles de dólares",
sec.axis=sec_axis(~./scaleFactor_t, name="Kcal per day", breaks =scales::pretty_breaks(), labels = scales::label_percent()),
breaks =scales::pretty_breaks(), labels = scales::label_dollar())
Upvotes: 1
Views: 282
Reputation: 41285
You could say the number of breaks like this pretty_breaks(6)
. Here is a reproducible example:
library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)
test = structure(list(Año = c(2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 2019L, 2019L,
2019L, 2019L, 2019L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L
), Vars = c("Oil.GAV", "Non-Oil.GAV", "Other.GDP.elements", "Oil.GAV.%",
"Non-Oil.GAV.%", "Other.GDP.elements.%", "Oil.GAV", "Non-Oil.GAV",
"Other.GDP.elements", "Oil.GAV.%", "Non-Oil.GAV.%", "Other.GDP.elements.%",
"Oil.GAV", "Non-Oil.GAV", "Other.GDP.elements", "Oil.GAV.%",
"Non-Oil.GAV.%", "Other.GDP.elements.%", "Oil.GAV", "Non-Oil.GAV",
"Other.GDP.elements", "Oil.GAV.%", "Non-Oil.GAV.%", "Other.GDP.elements.%"
), value = c(5639977, 91442756, 7213129, 0.0987732330025509,
0.869107003693333, 0.0321197633041161, 6918891, 93037062, 7606055,
0.0913116709595953, 0.875434164471086, 0.0332541645693185, 6519993,
94351584, 7236432, 0.0923079337383433, 0.876825035531481, 0.0308670307301762,
4951333, 88365192, 5491485, -0.06, -0.35, -0.078)), row.names = c(NA,
-24L), class = c("tbl_df", "tbl", "data.frame"))
scaleFactor_t <- max(test %>%
filter(Vars %in% c("Oil.GAV", "Non-Oil.GAV", "Other.GDP.elements")) %>%
select(value)) / max(test %>%
filter(!Vars %in% c("Oil.GAV", "Non-Oil.GAV", "Other.GDP.elements")) %>%
select(value))
ggplot() +
geom_col(data = test %>%
filter(Vars %in% c("Oil.GAV", "Non-Oil.GAV", "Other.GDP.elements")),
aes(x = Año, y = value , fill = Vars))+
geom_line(data = test %>%
filter(!Vars %in% c("Oil.GAV", "Non-Oil.GAV", "Other.GDP.elements")),
aes(x = Año, y = value*scaleFactor_t, colour = Vars), size = 1)+
geom_point(data = test %>%
filter(!Vars %in% c("Oil.GAV", "Non-Oil.GAV", "Other.GDP.elements")),
aes(x = Año, y = value*scaleFactor_t, colour = Vars), size = 2)+
scale_y_continuous(name="Miles de dólares",
sec.axis=sec_axis(~./scaleFactor_t, name="Kcal per day", breaks =scales::pretty_breaks(6), labels = scales::label_percent()),
breaks =scales::pretty_breaks(), labels = scales::label_dollar())
Created on 2022-09-11 with reprex v2.0.2
Upvotes: 2