Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position of axis titles in ggplot, relative placement?

Tags:

r

ggplot2

Does anyone have any insight into the placement of the axis titles in ggplot?

ggplot(mtcars, aes(x=mpg, y=hp)) + geom_point() + ylab("HP") + ylim(50,350) + xlim(10,35)

enter image description here

You can see the P in HP is below the top of the 200. And likewise the m on mpg is almost in line (or partly overlapping) with the 0 on 20 leaving the g in the middle of 20 and 25. I would have thought the middle of the gap between the H and P would have been in the middle (vertically) of the 2 in 200. If that makes sense.

Adding a few spaces to the label (" HP") helps to fix it. Obviously the title is being centered relative to some co-ordinates, my guess is a 'box' that goes all the way to the bottom of the x labels (for the y title) and to the left of the y labels (for the x title). When having them relative to the actual plotting area would be more desirable.

Is this achievable?

like image 542
nzcoops Avatar asked Nov 30 '11 02:11

nzcoops


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 move axis labels using Ggplot2 bar plot in R?

In this article, we are going to see how to move the axis labels using ggplot2 bar plot in the R programming language. First, you need to install the ggplot2 package if it is not previously installed in R Studio. For creating a simple bar plot we will use the function geom_bar ( ). stat : Set the stat parameter to identify the mode.

How to draw a plot title in a vertical position in R?

We can draw our plot title in a vertically higher position with the following R syntax: my_ggplot + theme ( plot.title = element_text ( vjust = 3)) # Change vertical position. my_ggplot + theme (plot.title = element_text (vjust = 3)) # Change vertical position.

What are the different position scales in ggplot2?

There are several other position scales for continuous variables— scale_x_log10 (), scale_x_reverse (), etc—most of which are convenience functions used to provide easy access to common transformations: For more information on scale transformations see Section 10.1.9 . By default, ggplot2 converts data outside the scale limits to NA.


1 Answers

Per @Hadley's comment, as of version 0.9.0 this bug-let has been fixed:

ggplot(mtcars, aes(x=mpg, y=hp)) + 
    geom_point() + 
    ylab("HP") + 
    ylim(50,350) + 
    xlim(10,35)

produces:

enter image description here

like image 100
joran Avatar answered Dec 05 '22 13:12

joran