Move Axis Labels Ggplot

move axis labels ggplot

You can adjust the positioning of the x-axis title using:

+ opts(axis.title.x = theme_text(vjust=-0.5))

Play around with the -0.5 "vertical justification" parameter until it suits you/your display device.

Moving labels in ggplot graph so that they match the x-axis tick marks

If you set hjust and vjust inside theme(axis.text.x = element_text(...)) you can tweak the positions however you like:

library(ggplot2)

ggplot(data = data, aes(x = location, y = vehicles)) +
geom_bar(stat = 'identity', fill = 'steelblue') +
theme(axis.text.x = element_text(angle = 90, vjust = 0.3, hjust = 1)) +
xlab("Location") +
ylab("Vehicles")+
ggtitle("Vehicles per location")

Sample Image

Move only some x axis labels down in ggplot

This is how to do it the hard way, if you really want those long ticks

tibble(x = 0:10, y = rnorm(11)) %>%
ggplot(aes(x,y)) +
geom_vline(xintercept = seq(2, 10, 4)/10, color = "white", size = 0.5) +
geom_point() +
scale_x_continuous(breaks = c(seq(0,10,4)/10, seq(2,10,1))) +
coord_cartesian(clip = "off", ylim = c(-2, 2)) +
annotate("text", label = format(seq(2, 10, 4)/10, nsmall = 1),
x = seq(2, 10, 4)/10, y = -2.4,
size = 3) +
annotate("segment", x = seq(2, 10, 4)/10, xend = seq(2, 10, 4)/10,
y = -2.33, yend = -2.2, size = 0.2) +
theme(axis.text = element_text(color = "black"))

Sample Image

Move Y axis Title horizontally ggplot2

Just wonder if annotate can do the trick. You may need to adjust x and y values as well as add size on your side

library(tidyverse)
df <- data.frame(
Salary = c(14000, 146000, 138000, 121000, 135000, 90000, 5000, 50),
Age = c(20, 40, 50, 79, 40, 30, 80, 50),
Gender = c("Female", "Male", "Female", "Male","Female", "Male","Female", "Male")
)

ggplot(data=df,
aes(y=Salary, x=Age, color=Gender)) +
geom_point() +
xlab("Age") + ylab("salary") +
ggtitle("Relationship Salary, Age, and Gender") +
stat_smooth(method="lm", se=FALSE) + theme_bw() +
theme(plot.title = element_text(face = "bold", size = 14),
axis.text = element_text(face = "bold", size = 13),
axis.title = element_text(face = "bold", size = 11),
axis.title.y = element_blank()
) +
coord_cartesian(clip = "off", ylim = c(0, 150000), xlim = c(20, NA))+
annotate("text", x = 12, y = 161000, label = "Salary")

Sample Image

How can I move x-axis labels away from the centre of a ggplot that uses coord_polar?

This doesn't look nearly as nice as the answer from @Allan Cameron using his very slick {geomtextpath} library but has the (possible) advantage of being easy to implement with native {ggplot2}. Here we simply use expand_limits() in the y direction to push the axis further from the plot and leave room for the label. It's a bit of a hack because I came up with a fudge factor for how far to expand based on the length of the label and the size of that bar. It works OK with labels of a certain size but if they get really long it will break down a bit unless you introduce a line break. Also you can add theme(plot.margin = unit(c(3, 0, 3, 0), "lines")) to get more space around the plot to avoid cutting of super long labels.

library(tidyverse)

# data
plotThis <-
tibble(
names = c("foo", "bar", "baz", "biff", "zoop", "sesquipedalian"),
values = c(100, 150, 42, 135.45, 177, 182))

# calculate scaling of axis based on longest label
new_max_y <-
plotThis %>%
mutate(nchar = nchar(names),
new_max_y = values * (1 + 0.01 * nchar)) %>%
pull(new_max_y) %>%
max()

# plot - expand margins if names get really long
plotThis %>%
ggplot(aes(x = names, y = values)) +
geom_col(color = "black", fill = "blue", width = 1) +
coord_polar(clip = "off") +
theme_minimal() +
scale_y_continuous(name = NULL, breaks = NULL) +
expand_limits(y = c(0, new_max_y)) +
xlab(NULL)

Sample Image

Created on 2022-02-15 by the reprex package (v2.0.1)

How does one move the tick labels closer to the axis?

Version 2.0.0 introduced the new margin() which we can use here:

ggplot(mtcars, aes(x = mpg, y = wt*1000, color = factor(cyl))) +
geom_point() +
ylab("weight (lb)") +
theme_bw() +
theme(axis.text.y = element_text(margin = margin(r = 0)))

Sample Image

My reading of this issue on github is, that you should use vjust only for the y-axis and hjust only for the x-axis. To alter the distance between tick-label and axis, use margin(r = x) on the y-axis, and margin(t = x) on the x-axis. Doc for element_text reads: "When creating a theme, the margins should be placed on the side of the text facing towards the center of the plot."



Related Topics



Leave a reply



Submit