How can I add x-axis label 'New x-lab' in the same family font to a ggplot2 object where element_blank has been used? xlab and labs does not seem to work.
library(ggplot2)
iris_p <- ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, col = Species)) +
geom_point() +
theme(axis.title = element_blank(),
text = element_text(family = 'Times'))
EDIT:
I am fully aware I can change the original code. I would like to add a lab to the object iris_p without changing the first part of the code.
Assuming iris_p is the output from some function, which you can't change directly (otherwise this seems like a rather convoluted exercise), you can add another theme() component to the plot, specifying the full element_text() for axis.title.x:
# I'm referencing the default theme_grey parameters here
iris_p +
theme(axis.title.x = element_text(family = "Times", face = "plain",
colour = "black", size = 11, lineheight = 0.9,
hjust = 0.5, vjust = 0.5, angle = 0,
margin = margin(), debug = FALSE)) +
xlab("some x axis label")
You can use element_blank() partially on the graphic.
For that, you can just specify theme(axis.title.y= element_blank()), then the x.axis can be renamed in a normal way
Related
This is a bit harder to explain than my last question because the problem is not exactly reproducible.
I am producing legends for a couple of maps and am drawing a box around both legends since one has only 1 item (a line feature) and the others are discrete fills (a polygon feature). Using geom_sf to plot both.
I end up with a weird artefact that looks like part of the lines are drawn twice with just a slightly shifted position.
I managed to produce a similar error with the iris dataset where legend.box.background is only partially drawn.
data(iris)
ggplot(iris)+theme_classic()+
geom_point(aes(x=Petal.Length, y=Sepal.Length, color=Species, size=Sepal.Width))+
scale_color_manual(name=NULL, values=c("red","green","blue") ,labels=c("supersupersupersuperlong", "test2", "test3"))+
theme(legend.position=c(0.1,0.75),legend.box.background=element_rect(fill="white", color="black"), legend.spacing.y=unit(0,"cm"))
UPDATE
I noticed in my original example it had to do with text length, so I tried adding a space after some of the labels which changes the "arrangement" of the twice-drawn lines a little bit. But I can't find an arrangement of whitespace that makes it go away completely.
Anyone know how to manually change the size of the legend.box.background. If not I will draw a geometric rectangle and call it quits.
I think the problem here is that the legend.background (which is a white rectangle behind each component of your legend), is partially drawing over the line surrounding the legend.box, which is the rectangle surrounding the whole legend. You can simply remove the legend.background
For example, your plot goes from this:
ggplot(iris) +
theme_classic() +
geom_point(aes(x = Petal.Length, y = Sepal.Length, color = Species,
size = Sepal.Width)) +
scale_color_manual(name = NULL, values = c("red", "green", "blue"),
labels = c("supersupersupersuperlong", "test2", "test3")) +
theme(legend.position = c(0.1, 0.75),
legend.box.background = element_rect(fill = "white", color = "black"),
legend.spacing.y = unit(0, "cm"))
To this:
ggplot(iris) +
theme_classic() +
geom_point(aes(x = Petal.Length, y = Sepal.Length, color = Species,
size = Sepal.Width)) +
scale_color_manual(name = NULL, values = c("red", "green", "blue"),
labels = c("supersupersupersuperlong", "test2", "test3")) +
theme(legend.position = c(0.1, 0.75),
legend.background = element_blank(),
legend.box.background = element_rect(fill = "white", color = "black"),
legend.spacing.y = unit(0, "cm"))
I'm new here, I hope I'm not doing anything wrong for my first post!
I'm using R again (it's been ages, almost 15 years when I first used it).
It's impressive how powerful it is.
Anyway, I'm trying to do heat maps using ggplot2 and I'm nearly there.
There's just the text of the legend of my scale that I need to modify (namely increase the font size and put bold characters).
Here's the piece of code I use:
yellowgreen <- c("yellow", "dark green")
pal <- colorRampPalette(yellowgreen)(100)
library(ggplot2)
data <- read.table("file_directory", sep=";", header = T)
v <- ggplot(data, aes(x = reorder(A, B) , y = Pourcentage, fill = B))
v + geom_tile() + geom_text(aes(label = round(B,1)), color = "black", size = 4, fontface=2) +
scale_fill_gradientn(colors = pal, name = expression("Legend Title")) +
labs(y = "Title for Y", x ="Title for X") +
theme(axis.title.x = element_text(face="bold",size=24, color="black"), axis.text.x = element_text(face="bold",size=24, color="black"), axis.title.y = element_text(face="bold",size=24, color="black"), axis.text.y = element_text(face="bold", size=24, color="black"))
Could you please help me to increase the font size of the legend and put it bold?
Thanks a lot
Cheers
I am using the ggpubr package to do boxplots with ggboxplot. Any suggestions on how to increase the distance between the adjacent boxplots?
I have been using R for a couple of weeks and I am aware that my script might be written better.
My code:
flowdata <- read.csv("flowdata.csv", header = TRUE, sep = ";")
flowdata$Haplotype = factor(flowdata$Haplotype,levels(flowdata$Haplotype)
[c(5,1,2,3,4,6)])
library(ggpubr)
p<-ggboxplot(flowdata, x="TP", y="Treg", add = "jitter",width = 0.5, shape
= "Treatment", fill = "Haplotype", palette = c("#0092d1","#62b232","#b23a32","#b232a3","#99cccc","#132a64"))+scale_shape_manual(values = c(21,23))
p1<-p+theme(legend.title = element_blank(), legend.text = element_text(size=8), text = element_text(family = "Calibri"), axis.text.x = element_text(angle = 45, hjust = 1))+ labs(x = expression(paste("")),
y = expression(paste(CD4^+{}, CD25^+{}, "cells/µL")))
p1
The parameter 'width' specifies the width of the boxes, so a simple solution would be to reduce that value (from 0.5). This would not increase spacing of the boxes, but increase spacing between them and therefore make the boxes narrower.
However, it seems to me like your boxplots are well spaced, but your points (jitter) are overlapping, making the graph look messy. A simpler solution would be to remove them, or change them to points instead of jitter. Alternatively you could use a violin plot.
For finer control, 'standard' ggplot2 can be used, perhaps with cowplot which can give you formatting:
p <- ggplot(data = flowdata, mapping = aes(x = TP, y = Treg, fill = Haplotype)) +
geom_boxplot(position = position_dodge(0.5)) +
geom_jitter(aes(shape = Treatment)) +
scale_shape_manual(c(21, 23)) +
scale_fill_manual(c("#0092d1","#62b232","#b23a32","#b232a3","#99cccc","#132a64")) +
theme(legend.title = element_blank(), legend.text = element_text(size=8), text = element_text(family = "Calibri"), axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(x = expression(paste("")), y = expression(paste(CD4^+{}, CD25^+{}, "cells/µL")))
Building on Adams answer, I would furthermore suggest you get the boxplots into several facets, so that the plot is actually readable. I suppose the haplotype might be the interesting facet. Also, you could reduce the size of the jitter points or get some transparency alpha so that they are less present, but with the facet the boxplots are larger already, so this might solve the problem of readability by itself.
p <- ggplot(flowdata, aes(x = TP, y = Treg)) +
geom_boxplot(position = position_dodge(0.5)) +
geom_jitter(aes(shape = Treatment), size = 0.5, alpha = 0.8) +
facet_wrap(~Haplotype, ncol = 3) +
scale_shape_manual(c(21, 23)) +
theme(legend.title = element_blank(),
legend.text = element_text(size=8),
text = element_text(family = "Calibri"),
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(x = "",
y = expression(paste(CD4^+{}, CD25^+{}, "cells/µL")))
I want to place the title inside the plot instead at the default top position.
Here is a simple code snippet
library(ggplot2)
df <- data.frame(x = c(1:10), y = rnorm(10, 1, 2))
ggplot(df, aes(x, y))+
geom_line() +
ggtitle("Demo") +
theme(plot.title = element_text(vjust = -3))
In the past I was able to do this by varying vjust value, but now it is not working. Any idea how to do this?
In the ggplot issue "vjust not working in v 2.0 for plot.title?", Hadley writes:
"All text elements now have a margin, which by default scale with the font
size in the theme. This leads to nicer spacing, particularly at large font
sizes. This means hacks with vjust and hjust no longer work. Instead,
use the margin() parameter of element_text()"
Play around with the t and b arguments in margin to adjust the title, e.g.:
ggplot(df, aes(x, y))+
geom_line() +
ggtitle("Demo") +
theme(plot.title = element_text(margin = margin(t = 10, b = -20)))
See ?margin for further arguments.
Note that you should use the margin argument for axis.title.x and axis.title.y as well:
ggplot() + ggtitle("this is title") + xlab("this is x") + ylab("this is y") +
theme(plot.title = element_text(margin = margin(b = -10)),
axis.title.x = element_text(margin = margin(t = -10)),
axis.title.y = element_text(margin = margin(r = -10)))
I am using the R package cooccur and cannot figure out how to change the font size in the associated graphics. The par() method does not seem to work.
Here is the example given by the package:
data(finches)
cooccur.finches <- cooccur(mat=finches,
type="spp_site",
thresh=TRUE,
spp_names=TRUE)
plot(cooccur.finches)
I am trying to change the font size of the species, the title and the legend to no avail on the heat map that is produced. Any help would be MUCH appreciated. Thanks!
Unfortunately the author didn't use defined theme inside the function, so if you want to not mess up the other customizations in place, this should work:
p <- plot(cooccur.finches)
p + theme_bw(base_size = 28) +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(vjust = -4, face = "bold"),
panel.background = element_rect(fill = "white", colour = "white"),
panel.grid = element_blank()
legend.position = c(0.9, 0.5))
You can also use this code to set the size of the legend or title independently, e.g.
p + theme(plot.title = element_text(vjust = -4, face = "bold", size = 36))
Most unfortunately, this won't change the size of the species labels because they are set with geom_text(). To alter them, you'll have to hack the function yourself cooccur:::plot.cooccur. You only need to modify the last line:
p + geom_text(data = dfids, aes(label = X1), hjust = 1, vjust = 0,
angle = -22.5)
# change to
p + geom_text(data = dfids, aes(label = X1), hjust = 1, vjust = 0,
angle = -22.5, size = 24)
It is a ggplot2 plot not a base one. So par will not work.
p <- plot(cooccur.finches)
p + theme(text = element_text(size = 10)) ## change text font size
or
p + theme_grey(base_size = 18) ## chnage all font size.
Author of Cooccur here. Sorry for the hassle with the text sizes being hard to adjust. I will deal with this when I get a chance.
Not a permanent solution, but easier than changing the function each time for the species labels, is to just directly re-assign the value in the ggplot object:
p$layers[[2]]$geom_params$size <- 10
Hope that helps. I might be a bit late to the scene...