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"))
Related
This question already has answers here:
Add mean to grouped box plot in R with ggplot2
(2 answers)
Closed 1 year ago.
I am trying to add the mean values (as shown in red dots in the plot below) in the boxplot with ggplot2. I used stat_summary to add mean values.
However, the following plot is not the exact one that I am looking for. What I'd like to get is to show two mean values for both Y (blue box) and N (red box), not one mean value for both.
Here is my code.
ggplot(data = df.08.long,
aes(x = TMT_signals, y = as.numeric(TMT_Intensities), fill = `probe.Mod.or.not(Y/N)`)) +
geom_boxplot() +
stat_summary(fun.y=mean, geom="point", shape=20, size=5, color="red", fill="red") +
coord_cartesian(
xlim = NULL,
ylim = c(0, 2e4),
expand = TRUE,
default = FALSE,
clip = "on")
theme_classic() +
theme(axis.title=element_text(size=8),
axis.text=element_text(size=10),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
Does anyone know how to solve this problem?
Thanks so much for any help!
mtcars example
Code
mtcars %>%
ggplot(aes(as.factor(vs),drat, fill = as.factor(am)))+
geom_boxplot()+
stat_summary(
fun=mean,
geom="point",
shape=21,
size=5,
#Define the aesthetic inside stat_summary
aes(fill = as.factor(am)),
position = position_dodge2(width = .75),
show.legend = FALSE
)
Output
I would like to make a histogram using facet_grid with multiple facets on the y axis with angle 0. The way shown below but it keeps all the facets in one line. How can I get each facet in a different line to minimize the width of the y facet boxes? If there is a solution where the label remains that would be great.
p <- ggplot(mtcars,(aes(x = mpg))) + geom_histogram(bins = 5) + facet_grid(vs+am~., labeller = label_both)
p <- p + theme(strip.text.y = element_text(angle = 0, hjust = 0))
This question already has answers here:
Distancing `facet_grid` strips from the faceted canvas?
(2 answers)
Closed 2 years ago.
I am using facet_grid to create a title in my plot that is surrounded by a black box with white background colour. I wonder how I could increase the space between this box with the title and the upper edge of my plot. Below I show a similar plot and I give a fake code to play with:
library(ggplot2)
library(cowplot)
library(ggpubr)
theme_set(theme_cowplot())
df1 <- data.frame(x = 1:12, y = (1:12)^2, grp=c('A', 'B', 'C'), depth=c("5m","5m","5m","5m"))
p1 <- ggplot(df1, aes(x, y, color=grp)) + geom_point() + facet_grid(.~depth) +
theme(strip.background = element_rect(colour = "black", fill = "white", size = 1.5, linetype = "solid"))
p1
Does anyone know how to do it? An alternative to increase space between the facet box and the upper edge of the plot could be also to do not use facet_grid. However, I couldn't find an alternative way to draw a title with a black box surrounding. If you have other way instead of using facet_grid is welcome.
Using #Dekike link:
ggplot(df1, aes(x, y, color=grp)) +
geom_point() +
facet_grid(.~depth) +
theme(strip.background.x = element_rect(colour = "black", fill = "white", size = 1.5, linetype = "solid"),
strip.placement = "outside",
strip.switch.pad.grid = unit(0.2, "in"))
This question already has answers here:
Remove space between plotted data and the axes
(3 answers)
Force the origin to start at 0
(4 answers)
Closed 5 years ago.
I have a data frame that looks like this:
df = data.frame(
comp_id= c("1A","1A","1A"),
rate = c(93,93,93),
quartile = c("25 pctl","50","75 pctl"),
quartile.value = c(88,92,95)
)
It graphs a point against quartiles. For some reason, I cannot remove the gap between the bar and the left axis.
-Setting cartesian doesn't work, and adding expand(0,0) to scale_y_continuous also doesn't work
library(scales)
ggplot(df,
aes(x = comp_id)) +
geom_col(aes(y = quartile.value, fill = quartile),
position = position_dodge(0), width = 2.5) + scale_color_manual(values=c("black"),labels=c("comp_id")) +
geom_point(aes(y = rate, color="comp_id"), size=3, shape=20) +
scale_fill_manual(values = c("azure2", "azure3", "azure4"), labels=c("25th Pctl","Median","75th Pctl")) +
labs(x="", y="") + scale_y_continuous(limits=c(80,100), oob=rescale_none) + theme(panel.border = element_blank(), panel.background=element_blank()) +
theme(legend.title=element_blank()) + theme(legend.position="bottom") + scale_x_discrete(expand=c(0,0))
Edit: I used the scales package because the bars disappeared when I specified the limits on the y axis.
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'))