Pietro
Pietro

Reputation: 347

ggplot2 - Add labels to second graph with secondary axis

I would like to add labels to both the red line on the graph and the columns. I managed to add labels either to the columns or the line but I am not able to do both at the same time.

Can anyone help?

Test Dataframe

           ebit ebit_margin year
Company1    2421    0.054   2019    
Company1.1  1928    0.049   2018    
Company1.2  1915    0.050   2017    
Company1.3  2135    0.060   2016    
Company1.4  1534    0.041   2015

Data

test <- structure(list(ebit = c(2421.459, 1928.349, 
1914.744, 2134.6, 1534.048), ebit_margin = c(0.05367, 0.04859, 
0.04973, 0.06016, 0.04143), year = structure(5:1, .Label = c("2015", 
"2016", "2017", "2018", "2019"), class = "factor")), class = "data.frame", row.names = c("Company1", 
"Company1", "Company1", "Company1", 
"Company1"))

Code

scaleFactor <- max(test$ebit) / max(test$ebit_margin)

ggplot(test, aes(as.factor(year), ebit, label=test$ebit)) + 
  geom_col(width = 0.5, fill = "darkblue", alpha = 0.5) +
  geom_line(aes(as.factor(year), ebit_margin * scaleFactor, group = 1),  size = 1,  col = "red", alpha = 0.5) +
  scale_y_continuous(name = "EBIT Value", sec.axis = sec_axis(~./scaleFactor, name = "EBIT Margin", labels = function(b) { paste0(round(b * 100, 3), "%")})) +
  #geom_text(size = 3, position = position_stack(vjust = 0.5)) + 
  theme_bw()  +
  labs(title = "Key Financials Across Years",
       subtitle = "Earnings Before Interest and Taxation",
       x = "Year") +
  theme(axis.text.x = element_text(angle = 60, hjust = 1)) +
  geom_label(
    label = c(test$ebit),
    position = position_stack(vjust = 0.75),
    check_overlap = T
  )

What I managed to get so far is the following:

Current Outcome

Upvotes: 1

Views: 1304

Answers (2)

Demetry Pascal
Demetry Pascal

Reputation: 544

just add

  geom_label(
    aes(as.factor(year), ebit_margin * scaleFactor, group = 1),
    label = c(test$ebit),
    position = position_stack(vjust = 0.99),
    check_overlap = T
  )

So ur result will be

ggplot(test, aes(as.factor(year), ebit, label=test$ebit)) + 
  geom_col(width = 0.5, fill = "darkblue", alpha = 0.5) +
  geom_line(aes(as.factor(year), ebit_margin * scaleFactor, group = 1),  size = 1,  col = "red", alpha = 0.5) +
  scale_y_continuous(name = "EBIT Value", sec.axis = sec_axis(~./scaleFactor, name = "EBIT Margin", labels = function(b) { paste0(round(b * 100, 3), "%")})) +
  #geom_text(size = 3, position = position_stack(vjust = 0.5)) + 
  theme_bw()  +
  labs(title = "Key Financials Across Years",
       subtitle = "Earnings Before Interest and Taxation",
       x = "Year") +
  theme(axis.text.x = element_text(angle = 60, hjust = 1)) +
  geom_label(
    label = c(test$ebit),
    position = position_stack(vjust = 0.75),
    check_overlap = T
  )+
  geom_label(
    aes(as.factor(year), ebit_margin * scaleFactor, group = 1),
    label = c(test$ebit),
    position = position_stack(vjust = 0.99),
    check_overlap = T
  )

Upvotes: 1

Allan Cameron
Allan Cameron

Reputation: 173793

You need to add a second geom_label with the appropriate y and label aesthetic:

ggplot(test, aes(as.factor(year), ebit, label=test$ebit)) + 
  geom_col(width = 0.5, fill = "darkblue", alpha = 0.5) +
  geom_line(aes(as.factor(year), ebit_margin * scaleFactor, group = 1),  
            size = 1, col = "red", alpha = 0.5) +
  scale_y_continuous(name = "EBIT Value", 
                     sec.axis = sec_axis(~./scaleFactor, 
                     name = "EBIT Margin", 
                     labels = function(b) { 
                       paste0(round(b * 100, 3), "%")})) +
  theme_bw()  +
  labs(title = "Key Financials Across Years",
       subtitle = "Earnings Before Interest and Taxation",
       x = "Year") +
  theme(axis.text.x = element_text(angle = 60, hjust = 1)) +
  geom_label(
    aes(label = ebit),
    position = position_stack(vjust = 0.75),
    check_overlap = T
  ) +
  geom_label(
    aes(label = ebit_margin * 100, y = ebit_margin * scaleFactor),
    colour = "red"
  )

enter image description here

Upvotes: 3

Related Questions