21

In ggplot2, how can I modify axis.line.y only on the sec.axis (if possible)?

    p <- ggplot(mtcars, aes(cyl, mpg)) +
      geom_point()

    # Create a simple secondary axis
    p + scale_y_continuous(sec.axis = sec_axis(~.+10)) +
      theme(axis.line.y = element_line(color = "red"),
            # I can modify text color but not sure about line?
            axis.text.y.right = element_text(color = "red"))

enter image description here

As shown in comments below, full control over right axis elements is now comprised in the development version of ggplot2 theme( axis.line.y.right = element_line(color = "red"), axis.ticks.y.right = element_line(color = "red"))

M--
  • 25,431
  • 8
  • 61
  • 93
AJMA
  • 1,134
  • 2
  • 13
  • 28
  • 1
    `+ geom_segment(aes(x=xmax+0.2,xend=xmax+0.2,y=ymin-2,yend=ymax+2), color = "red") + coord_cartesian(xlim=c(xmin, xmax), ylim=c(ymin, ymax))` this would be a hack. `ggplot2` is a bit hard on secondary axis and won't give you that much of flexibility. Because of this: https://stackoverflow.com/questions/3099219/plot-with-2-y-axes-one-y-axis-on-the-left-and-another-y-axis-on-the-right/3101876#3101876 – M-- Aug 14 '17 at 21:56
  • Thanks, it would be, nonetheless, nice to have a `ggplot2`solution on this, provided `sec.axis`is already available... – AJMA Aug 14 '17 at 22:15
  • 2
    Based on [this closed issue](https://github.com/tidyverse/ggplot2/issues/1899), I think this may be fixed in the development version of *ggplot2*. – aosmith Aug 14 '17 at 22:25

1 Answers1

21

Update:

Thanks to the comment below, this can be done by using latest dev.v of (Now available in the CRAN version);

install.packages("devtools")
devtools::install_github("tidyverse/ggplot2")
library(ggplot2)

p + theme( axis.line.y.right = element_line(color = "red"), 
       axis.ticks.y.right = element_line(color = "red"))



Maybe there is a straight solution but this is a hack/workaround that I can think of using geom_segment:

p <- ggplot(mtcars, aes(cyl, mpg)) +
     geom_point()

#get the ylim and xlim
xmin <- min(ggplot_build(p)$layout$panel_ranges[[1]]$x.range) 
xmax <- max(ggplot_build(p)$layout$panel_ranges[[1]]$x.range)
ymin <- min(ggplot_build(p)$layout$panel_ranges[[1]]$y.range)
ymax <- max(ggplot_build(p)$layout$panel_ranges[[1]]$y.range)

# Create a simple secondary axis
p + scale_y_continuous(sec.axis = sec_axis(~.+10)) +
    theme(axis.text.y.right = element_text(color = "red"))+
    geom_segment(aes(x=xmax+0.2,xend=xmax+0.2,
                     y=ymin-2,yend=ymax+2), color = "red") +
    coord_cartesian(xlim=c(xmin, xmax), ylim=c(ymin, ymax))

enter image description here

M--
  • 25,431
  • 8
  • 61
  • 93
  • 10
    if you update ggplot using github, this feature is no added `theme( axis.line.y.right = element_line(color = "red"), axis.ticks.y.right = element_line(color = "red"))` – user20650 Aug 14 '17 at 23:11
  • Hello , in this question when i run theme ( axis.line.y.right = element_line(color = "red") it says Error in (function (el, elname) : "axis.ticks.y.right" is not a valid theme element name. Why i am geeting this error, what is solution ? – Lily Nature Jun 27 '18 at 19:31
  • 1
    @LilyNature You need to install latest developer version from github. run this: `install.packages("devtools");devtools::install_github("tidyverse/ggplot2");library(ggplot2)` and then the code above should work for you. – M-- Jun 27 '18 at 19:38
  • @M-- Say we have another variable on the second axis called "V1." How would you change the color of that, without changing the color of other axis titles? – ljh2001 Jan 07 '20 at 19:59
  • 1
    @ljh2001 use this ```axis.title.y.right = element_text(colour = "red")``` within `theme(...)`. – M-- Jan 07 '20 at 20:12
  • 1
    this should now be possible with the CRAN version :) – tjebo Feb 19 '21 at 06:34