Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: can't change axis.title.y angle with position = 'right'

Tags:

r

ggplot2

Here's a basic plot:

ggplot(data.frame(x=1:10,y=1:10), aes(x,y)) +
  geom_point() +
  theme_linedraw()

enter image description here

If I want the y axis label not to be rotated, i can do that like this:

ggplot(data.frame(x=1:10,y=1:10), aes(x,y)) +
  geom_point() +
  theme_linedraw() +
  theme(axis.title.y = 
          element_text(color='red', angle=0, vjust = 0.5))

enter image description here

And if I want it on the right side, I can do that like this:

ggplot(data.frame(x=1:10,y=1:10), aes(x,y)) +
  geom_point() +
  theme_linedraw() +
  scale_y_continuous(position = 'right')

enter image description here

But why can't I do both?

ggplot(data.frame(x=1:10,y=1:10), aes(x,y)) +
  geom_point() +
  theme_linedraw() +
  scale_y_continuous(position = 'right') +
  theme(axis.title.y = 
          element_text(color='red', angle=0, vjust = 0.5))

enter image description here

For reference:

> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X El Capitan 10.11.6

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets 
[6] methods   base     

other attached packages:
[1] magrittr_1.5  ggplot2_2.2.1

loaded via a namespace (and not attached):
 [1] labeling_0.3     colorspace_1.2-6 scales_0.4.1    
 [4] lazyeval_0.2.0   plyr_1.8.4       tools_3.3.3     
 [7] gtable_0.2.0     tibble_1.3.4     yaml_2.1.13     
[10] Rcpp_0.12.12.5   grid_3.3.3       digest_0.6.12   
[13] rlang_0.1.2      munsell_0.4.3 
like image 524
arvi1000 Avatar asked Dec 14 '17 21:12

arvi1000


People also ask

How to change the title position of a plot in ggplot2?

By default, the title of plots in ggplot2 are left-aligned. However, you can use the following methods to change the title position: some_ggplot + theme (plot.title = element_text (hjust = 0.5)) The following examples show how to use each method in practice with the built-in mtcars dataset in R.

How to change the direction of Y-axis title using Ggplot2 in R?

The default direction of Y-axis title using ggplot2 in R is vertical and we can change to horizontal. For this purpose, we can use theme function of ggplot2 package. We would need to use the argument of theme function as axis.title.y=element_text (angle=0)) and this will write the Y-axis title to horizontal but the position will be changed to top.

How to set axis labels to a vertical angle in ggplot2?

Note that we could use any other type of ggplot2 graphic or diagram (e.g. histogram, scatterplot, boxplot etc.) for the following examples. If we want to set our axis labels to a vertical angle, we can use the theme & element_text functions of the ggplot2 package.

How to change axis labels and main titles in a plot?

Axis labels and main titles can be changed to reflect the desired appearance. For this element_text () function is passed with the required attributes. For this theme () function is called with reference to which part of the plot has to be modified. To these references, pass element_blank () without any argument.


1 Answers

ggplot2::theme has left and right options for applicable components. So you can use axis.title.y.right.

Strange that you need to explicitly specify right when axis.title.y colours both left and right as you say in your comments, good spotting!

library(ggplot2)
ggplot(data.frame(x=1:10,y=1:10), aes(x,y)) +
  geom_point() +
  theme_linedraw() +
  scale_y_continuous(position = 'right') +
  theme(axis.title.y.right = element_text(color='red', angle=0, vjust = 0.5))

like image 170
rdh Avatar answered Nov 15 '22 01:11

rdh