0

I'm trying to plot a line chart in ggplot with two Y-axis. However, I can't make now of the lines corresponding to the secondary axis - it will only plot according to the primary one. Here is the code for the dataframe:

df <- data.frame(
  type = c("A", "B", "C", "D"),
  prop1 = c(0.31, 0.12, 0.55, 0.02),
  prop2 = c(0.16, 0.22, 0.15, 0.22)
)

df$type <- factor(df$type,
                  levels = c("D", "B", "A", "C")) #to reorder the plot

Now, the left Y-axis should correspond to prop1, while the right Y-axis should correspond to prop2. My code is as follows:

ggplot(df) +
  aes(x = type) +
  geom_point(aes(y = prop1), size = 2) +
  geom_line(aes(y = prop1), size = 1, group = 1) +
  geom_point(aes(y = prop2), size = 2, shape = 15) +
  geom_line(aes(y = prop2), size = 1, group = 1) +
  theme_bw() +
  labs(x = "") +
  scale_y_continuous(name = "prop1\n", 
                     breaks = seq(0,0.6,0.1),
                     limits = c(0,0.6),
                     labels = function(x) paste0(x * 100, "%"),
                     
                     sec.axis = sec_axis(~., name = "prop2"))

This code plots this:

enter image description here

The blue line (prop2) should be expanded to correspond to the right Y-axis (with the limits being, for example, [0.1, 0.25]). The problem is that that line is still being plotted according to the left Y-axis.

The desired plot would have the blue line with a bigger drop since the limits in the secondary axis (right) is smaller than the primary (left) one.

How can I fix this? Thanks!

  • There are some ways to approach this problem here: https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales – George Savva Apr 24 '23 at 15:58
  • Secondary axes in ggplot2 are just a decoration and can't be used directly as the scale for any of your layers. It sounds like you want your secondary axis to be a translation like `sec_axis(~ (. * 100/15)+0.1, name = "prop2")`, and your blue line should use `aes(y = prop2 * 100/15 + 0.1)` – Jon Spring Apr 24 '23 at 16:09
  • Well, that wasn't what I was aiming for, but maybe could also work. Using y = prop2 * 100/15 + 0.1 the plot has now two lines very separated from each other, when what I wanted was a kind of cross between them, just like the shape of an "X". – Artur Vidaurre de Almeida Apr 24 '23 at 16:47

1 Answers1

1

One way could be using a coefficients:

coeff <- 0.5

ggplot(df, aes(x = type)) +
  geom_point(aes(y = prop1), size = 2, color = "red")+
  geom_line(aes(y = prop1), size = 1, group = 1, color = "red") +
  geom_point(aes(y = prop2 / coeff), shape=23, fill="blue", size=2) +
  geom_line(aes(y = prop2 / coeff), size = 1, group = 1, color = "blue") +
  scale_y_continuous(
    name = "Prop1",
    sec.axis = sec_axis(~.*coeff, name = "Prop2")
  ) +
  xlab("\n My x label")+
  theme_bw(14)+
  theme(
    axis.title.y = element_text(color = "red", size=13, face="bold"),
    axis.title.y.right = element_text(color = "blue", size=13, face="bold"),
    axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1)
  ) +
  ggtitle("My title")

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • 1
    This is better than my plot, so thank you a lot! However, I wanted to "stretch" the right Y-axis a bit more. The goal is to make the blue line have a bigger drop. Let's say we put the limits starting at 0.1. Can you see it changing the scale? Do you think that that is possible? – Artur Vidaurre de Almeida Apr 24 '23 at 16:33
  • You can do this by changing `coeff`. For example if you use `coeff <- 0.9` the blue line will drop down. Just play around by changing `coeff`. – TarJae Apr 24 '23 at 17:27