Подтвердить что ты не робот

Изменить цвет оси вторичной линии в ggplot2

В ggplot2, как я могу изменить axis.line.y только на sec.axis (если это возможно)?

    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

ОБНОВЛЕНИЕ: Как показано в комментариях ниже, полный контроль над элементами правой оси теперь включен в версию разработки ggplot2 theme( axis.line.y.right = element_line(color = "red"), axis.ticks.y.right = element_line(color = "red"))

4b9b3361

Ответ 1

Обновление:

Благодаря комментарию ниже, это можно сделать с помощью последней версии dev.v из ;

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"))



Может быть, есть прямое решение, но я могу подумать об этом взломе/обходном пути 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