library(ggplot2)
p <- ggplot(mtcars, aes(x=mpg, y=wt*1000, color = factor(cyl))) + geom_point()
p + ylab("weight (lb)") +theme_bw()
I would like to move 5000, 4000, 3000, and 2000 closer to the vertical axis. I know one can instead use theme(axis.title.y=element_text(vjust=0.36,hjust=.36)) or similar to move the axis title further away, but sometimes I really want to move the tick labels, not the axis title.
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)))
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."
One solution would be to use axis.ticks.margin= element of theme() and set it to 0. But this will influence both axis.
library(ggplot2)
library(grid)
ggplot(mtcars, aes(x=mpg, y=wt*1000, color = factor(cyl))) + geom_point() +
ylab("weight (lb)") +theme_bw()+
theme(axis.ticks.margin=unit(0,'cm'))
Related
I have a plot
and I would like to move the y axis label in the right but without changing the scale. Is there a way to do it? My code is:
ggplot(co, aes(x=model1,y = model2, color=ex)) +
geom_point()
One option to achieve your desired result would be to first duplicate the y axis, then use theme options to get rid of the title on the left, the ticks and of course the labels on the right:
Using a simple plot based on mtcars as an example:
library(ggplot2)
p <- ggplot(mtcars, aes(hp, mpg)) +
geom_point()
# Move only the label to the right
p +
scale_y_continuous(sec.axis = dup_axis(labels = NULL)) +
theme(axis.title.y.left = element_blank(),
axis.ticks.y = element_blank())
EDIT If you only want the title on the right, then switch the position and get rid of the text and ticks using theme options:
# Move the label to the right and get rid of everything else
p +
scale_y_continuous(position = "right") +
theme(
axis.ticks.y = element_blank(),
axis.text.y = element_blank()
)
moving the axis to the right hand side can be accomplished using
+ scale_y_continuous(position = "right")
I know this is a very basic question, but I have trouble changing font size of axis labels in ggplot2. I used the code like below:
a <- ggplot(data1, aes(x=data2, y=data3)) +
geom_hline(yintercept=c(1, -1)) +
labs(x = data2, y = data3) +
theme_bw() +
theme(axis.text=element_text(size=10))
I tried to change the axis label size but they do not change.... Does anybody have suggestion?
Check out here how to alter labels in ggplot:
http://www.sthda.com/english/wiki/ggplot2-title-main-axis-and-legend-titles
In the example below we use axis.title to change the size, colour and face of the text.
library(ggplot2)
ggplot(mtcars, aes(x=mpg, y=disp)) +
geom_point() +
theme_bw() +
theme(axis.title=element_text(size=10, colour = "red", face = "bold"))
How can I create the following style of graph:
Notice the gap between x-y axis (red circle) and protruded ticks in x-y axis (arrow).
At best I can do is this now:
library(ggplot2)
p <- ggplot(mpg, aes(class, hwy)) +
geom_boxplot() +
theme_bw(base_size=10)
p
One option is to remove the built-in axis lines and then use geom_segment to add axes with a gap. In order to make it easier to get the broken axis lines in the right place, we also use scale_y_continuous to specify exactly where we want the axis breaks and limits. The code also shows how to increase the size of the tick marks.
ggplot(data=mpg, aes(class, hwy)) +
geom_segment(y=10, yend=50, x=0.4, xend=0.4, lwd=0.5, colour="grey30", lineend="square") +
geom_segment(y=5, yend=5, x=1, xend=length(unique(mpg$class)),
lwd=0.5, colour="grey30", lineend="square") +
geom_boxplot() +
scale_y_continuous(breaks=seq(10,50,10), limits=c(5,50), expand=c(0,0)) +
theme_classic(base_size=12) +
theme(axis.line = element_blank(),
axis.ticks.length = unit(7,"pt"))
You can achieve something similar using ggthemes which provides geom_rangeframe and theme_tufte.
library(ggplot2)
library(ggthemes)
ggplot(mpg, aes(class, hwy)) +
geom_boxplot() +
geom_rangeframe() +
theme_tufte() +
theme(axis.ticks.length = unit(7, "pt"))
More inspiration here.
Originally posted as an answer to a related question, I was encouraged to share my answer here as well.
The ggh4x package has a truncated axis guide that solves this problem by taking advantage of position guide customisation introduced in ggplot2 v3.3.0. Because it uses the guide system directly instead of working through a geom, it is responsive to theme settings just as regular axes. (Disclaimer: I'm the author of ggh4x).
By default, it truncates the axis to the outermost breaks, but this can be adjusted.
library(ggplot2)
library(ggh4x)
ggplot(mpg, aes(class, hwy)) +
geom_boxplot() +
guides(x = "axis_truncated", y = "axis_truncated") +
theme(axis.line = element_line(colour = "black"))
Created on 2021-04-19 by the reprex package (v1.0.0)
The bars on the top and bottom of the lines are added with
geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2)
or by adding geom to another layer.
stat_summary(fun.data = mean_sdl,
fun.args = list(mult = 1),
geom = "errorbar",
width = 0.1)
How can I create the following style of graph:
Notice the gap between x-y axis (red circle) and protruded ticks in x-y axis (arrow).
At best I can do is this now:
library(ggplot2)
p <- ggplot(mpg, aes(class, hwy)) +
geom_boxplot() +
theme_bw(base_size=10)
p
One option is to remove the built-in axis lines and then use geom_segment to add axes with a gap. In order to make it easier to get the broken axis lines in the right place, we also use scale_y_continuous to specify exactly where we want the axis breaks and limits. The code also shows how to increase the size of the tick marks.
ggplot(data=mpg, aes(class, hwy)) +
geom_segment(y=10, yend=50, x=0.4, xend=0.4, lwd=0.5, colour="grey30", lineend="square") +
geom_segment(y=5, yend=5, x=1, xend=length(unique(mpg$class)),
lwd=0.5, colour="grey30", lineend="square") +
geom_boxplot() +
scale_y_continuous(breaks=seq(10,50,10), limits=c(5,50), expand=c(0,0)) +
theme_classic(base_size=12) +
theme(axis.line = element_blank(),
axis.ticks.length = unit(7,"pt"))
You can achieve something similar using ggthemes which provides geom_rangeframe and theme_tufte.
library(ggplot2)
library(ggthemes)
ggplot(mpg, aes(class, hwy)) +
geom_boxplot() +
geom_rangeframe() +
theme_tufte() +
theme(axis.ticks.length = unit(7, "pt"))
More inspiration here.
Originally posted as an answer to a related question, I was encouraged to share my answer here as well.
The ggh4x package has a truncated axis guide that solves this problem by taking advantage of position guide customisation introduced in ggplot2 v3.3.0. Because it uses the guide system directly instead of working through a geom, it is responsive to theme settings just as regular axes. (Disclaimer: I'm the author of ggh4x).
By default, it truncates the axis to the outermost breaks, but this can be adjusted.
library(ggplot2)
library(ggh4x)
ggplot(mpg, aes(class, hwy)) +
geom_boxplot() +
guides(x = "axis_truncated", y = "axis_truncated") +
theme(axis.line = element_line(colour = "black"))
Created on 2021-04-19 by the reprex package (v1.0.0)
The bars on the top and bottom of the lines are added with
geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2)
or by adding geom to another layer.
stat_summary(fun.data = mean_sdl,
fun.args = list(mult = 1),
geom = "errorbar",
width = 0.1)
This question already has an answer here:
ggplot2 plot area margins?
(1 answer)
Closed 5 years ago.
How can I increase the area around a plot area in ggplot 2 to give my axis titles some breathing room. I am aware of vjust and hjust (as below), however, I can't seem to create actual space around the plotting area to move my axes titles onto.
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p
p<- p + theme(axis.title.x = element_text(family="Times",size=20,face="bold",colour = "Black",vjust=-1,hjust=0.5))
p
Margins around plot can be modified with theme(), plot.margin = and function margin() where you provide size of margins starting with top, then right, bottom and left, and units (default is "pt").
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() +
theme(axis.title.x = element_text(family = "Times",size = 20,
face = "bold",colour = "Black",vjust = -1,hjust = 0.5))+
theme(plot.margin = margin(1,1,1.5,1.2, "cm"))