0

I am facing a few issues regarding the sec.axis. The following is my code. I'm trying to plot the Asking price on the left side and the Area on the right side with the x-axis showing the District. Both the values can be shown on the graph but the Area values are not distributing according to the secondary axis, it's still showing display according to the primary Y-axis.

picture:

enter image description here

ggplot(df2,mapping = aes(x=District.))+ 
    geom_bar(aes(y=Asking/1000),stat="identity") + 
    geom_line(data=df2, aes(x=District., y=Area, group=1), inherit.aes = FALSE) +
    scale_y_continuous(sec.axis = sec_axis(~ .*0.4, name = "Area")) +
    theme_light() + 
    theme(axis.text.x = element_text(angle = 90, hjust=1), 
    axis.text.y  = element_text(color = 'green'), 
    axis.title.y = element_text(color='green'), 
    axis.text.y.right =  element_text(color = 'blue'),
    axis.title.y.right = element_text(color='blue'))
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
nike.96
  • 67
  • 1
  • 1
  • 5
  • Please edit the question by providing `dput(df2)`. See [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – UseR10085 Sep 12 '20 at 12:36

1 Answers1

0

Please note that your secondary y-axis is still a transformation of the primary y-axis. It is not an independent y-axis. For more details, please see here. Also, you need to define y=Area*2 in geom_line(), as you are defining sec_axis(~ .*0.4,...). You can see Sebastian Rothbucher's explanation in the same link.

YBS
  • 19,324
  • 2
  • 9
  • 27