r - How to change secondary line axis color in ggplot2 -
in ggplot2
, how can modify axis.line.y
on sec.axis
(if possible)?
p <- ggplot(mtcars, aes(cyl, mpg)) + geom_point() # create simple secondary axis p + scale_y_continuous(sec.axis = sec_axis(~.+10)) + theme(axis.line.y = element_line(color = "red"), # can modify text color not sure line? axis.text.y.right = element_text(color = "red"))
update: shown in comments beloew, full control on right axis elements comprised in development version of ggplot2 theme( axis.line.y.right = element_line(color = "red"), axis.ticks.y.right = element_line(color = "red"))
maybe there straight solution hack/workaround can think of using geom_segment
:
p <- ggplot(mtcars, aes(cyl, mpg)) + geom_point() #get ylim , 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 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))
Comments
Post a Comment