0

This question has been asked on this platform quite many times but my situation is a bit different: There are negative values. This is what I wish to achieve:

enter image description here

This is where I've got so far:

enter image description here

This is my code:

yearlyMedicalGWP <- tibble( Year = 2015:2019 |> as.character(), Amount = c(29.5, 38.5, 38.3, 40.2, 42.3), growth_rate = c(16.6, 30.5, -0.5, 4.8, 5.4) ) transf_fact <- max(yearlyMedicalGWP$Amount) / max(yearlyMedicalGWP$growth_rate) yearlyMedicalGWP |> ggplot(mapping = aes(x = Year, y = Amount)) + geom_bar(stat = "identity", width = 0.5, color = "#003f5c", fill = "#003f5c", position = position_dodge(width = 0.7)) + geom_text(mapping = aes(label = Amount), vjust = -0.2, fontface = "bold") + geom_line(aes(y = transf_fact * growth_rate), group = 1) + scale_y_continuous(sec.axis = sec_axis(trans = ~ . / transf_fact, name = "Second axis")) + geom_point(aes(y = transf_fact * growth_rate)) + ggtitle(expression(paste(underline("Gross Written Premium")))) + ylab("Amount in Kshs Billions") + theme( aspect.ratio = 0.65, panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank(), plot.title = element_text(face = "bold", hjust = 0.5, size = 15), axis.title = element_text(face = "bold.italic"), axis.text = element_text(size = 13), axis.ticks = element_blank() ) 

The second axis should "move up" a bit so the negative point is not below the bars .

I also know (& understand) these types of charts may be misleading but I'm required to do this. Help!

0

1 Answer 1

1

Add an offset to your transformation

transf_fact <- max(yearlyMedicalGWP$Amount) / max(yearlyMedicalGWP$growth_rate) transf_offset <- 10 

Then change your line and point to use

geom_line(aes(y = transf_fact * growth_rate + transf_offset), group = 1) geom_point(aes(y = transf_fact * growth_rate + transf_offset)) 

and adjust your scale transformation

scale_y_continuous(sec.axis = sec_axis(trans = ~ . / transf_fact - transf_offset, name = "Second axis")) + 

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.