How to change theme font for Volcano Plots? - r

I'm trying to wrangle a Volcano plot made with the EnhancedVolcano package to have all text in Arial font style. I tried to do so with this code:
a <- EnhancedVolcano(data.matrix,
lab = rownames(data.matrix), x = "Log2.fold.change",
y = "P.value", xlim = c(-2, 2), ylim = c(0, 6), xlab = bquote(~ Log[2] ~ "fold change"),
ylab = bquote(~ -Log[10] ~ italic(P)), axisLabSize = 12,
title = paste("NanoString -", data.name), subtitle = "",
labFace = "bold", pointSize = 2, labSize = 5, pCutoff = 10e-2, FCcutoff = 0.4,
gridlines.major = FALSE, gridlines.minor = FALSE, drawConnectors = TRUE,
widthConnectors = 0.2, colConnectors = "black", legendPosition = "none"
)
print(a)
EV_merge <- a + theme(text = element_text(size = 8, family = "sans"))
print(EV_merge)
The problem I'm having is the element_text command, which I thought would work since the plot is an object in ggplot2, seems to only work for the axis and title text, but the font of the labels for the specific genes seems to remain the same. What function should I be using in this case?

The labels are nothing to do with theme. They are created by geom_text_repel, so they are part of a data layer. There doesn't seem to be an option to change the font family of the labels, but you can change it in situ after the plot is created.
Obviously, I don't have your data, but using the example from the help page and saving it as EV_merge, we have:
EV_merge
To change the font face and family, we can do:
EV_merge$layers[[4]]$aes_params$fontface <- 1
EV_merge$layers[[4]]$aes_params$family <- "sans"
EV_merge

Related

Change data label font in ggplot:

This solution https://stackoverflow.com/a/71590169/7106842
is effective a changing the font of the title, x-axis, and y-axis labels. But not of the annotated data labels. They are still default font. Is there a way to change those labels as well?
I recognize this isn't a minimally reproducible example but the code below was the original partial solution to the problem.
#incorrect model with labels added by sjPlot
a = (plot_model(sl_distr_model,
order.terms = c(1,2,3,4,5,6,7),
show.values = TRUE,
value.offset = .3,
rm.terms = c("taxon_detailAnas platyrhynchos","taxon_detailAnas strepera","logshape","logshape:cadencefactor1h","logshape:cadencefactor2h","logshape:cadencefactor3h","logshape:cadencefactor6h","logshape:cadencefactor12h","logshape:cadencefactor24h"),
axis.lim = c(xlimrange_min,xlimrange_max),
colors = c("firebrick"),
wrap.labels = 60,
title = c("Plot 3: Intercept parameters by time interval - Relationship bewtween shape and scale gamma parameters of step length distributions 7.429"),
axis.title = "Intercept = Red; Ratio of Shape to Rate parameters = Blue"))
#added 2 fonts I had installed
windowsFonts(A = windowsFont("Times New Roman"), B = windowsFont("Century Gothic"))
#incomplete formating
a + theme(text = element_text(family = "A")
The above code did not change the data label font. The all components of the chart were Times New Roman except for the data labels which remained the system deault (courier), to fix this I had to manually add the relevant values using geom_text() referencing the same font family as the rest of the chart
#correct model without data labels
aa = (plot_model(sl_distr_model,
order.terms = c(1,2,3,4,5,6,7),
show.values = FALSE,
value.offset = .3,
rm.terms = c("taxon_detailAnas platyrhynchos","taxon_detailAnas strepera","logshape","logshape:cadencefactor1h","logshape:cadencefactor2h","logshape:cadencefactor3h","logshape:cadencefactor6h","logshape:cadencefactor12h","logshape:cadencefactor24h"),
axis.lim = c(xlimrange_min,xlimrange_max),
colors = c("firebrick"),
wrap.labels = 60,
title = c("Plot 3: Intercept parameters by time interval - Relationship bewtween shape and scale gamma parameters of step length distributions 7.429"),
axis.title = "Intercept = Red; Ratio of Shape to Rate parameters = Blue"))
aa + theme(text = element_text(family = "B")) + geom_text(aes(label= round(sl_distr_model#beta[2:8],2)), family = "B", nudge_x = 0.25, check_overlap = F)
Now all text is Century Gothic.

contourplot color and labels options in Lattice for R

I am quite new to Lattice and I am stuck with some possibly basic coding. I am using shapefiles and geoTIFFS to produce maps of animals distribution and in particular I have:
1 x point shapefile
2 x geoTIFF
1 x polygon shapefile
I am overlapping a levelplot of one of the geoTIFF (UD generated with adehabitatHR) with a contourplot of the same geoTIFF at specific intervals (percentile values), a contourplot of the second geoTIFF (depth raster from ETOPO2) for three specific values (-200, -1000 and -2000), the point shapefile (animal locations) and the polygon shapefile (land). All works fine but I need to change the font size of contour plot labels, their length (i.e. from 0.12315 to 0.123) and positioning for all the contourplots. For the depth contourplot I would like to change the style of each line in something like "continous line", "dashed line" and "point line", and for the contourplot of the UD I would like to change the color of each line using a yellow to red palette.
As far as I understand, I should use panel functions to implement these changes (e.g. Controlling z labels in contourplot) but i am not quite sure how to do it. Part of my code to generate the "plot":
aa <-
quantile(
UD_raster,
probs = c(0.25, 0.75),
type = 8,
names = TRUE
)
my.at <- c(aa[1], aa[2])
depth<-c(-100, -200, -2000)
levelplot(
UD_raster,
xlab = "",
ylab = "",
margin = FALSE,
contour = FALSE,
col.regions = viridis(100),
main = "A",
maxpixels = 2e5
) + layer(sp.polygons(Land, fill = "grey40", col = NA)) + layer(sp.points(locations, pts = 2, col = "red")) + contourplot(
UD_raster,
at = my.at,
labels = TRUE,
margin = FALSE
) + contourplot(
ETOPO2,
at = depth,
labels = TRUE,
margin = FALSE
)
A simplified image, with no UD layer and no point shapefile can be found here and as you can see it is pretty messy. Thanks for your help.
So far for the ETOPO2 countourplot I have solved by eliminating the labels and adding the argument lty to style the line. Because I can't figure out how to use lty with different values for each single line in my contour, I have replicated the contourplot function three times on the same surface, one for each contour I am interested into (this was easy because I only need three contours).
For the position, font and font size of the labels of the remaining contourplot I have used
labels = list(cex = 0.8, "verdana"),
label.style = "flat"
To "shorten" the length of the labels I have used the function round where I specify to which decimal digit to round number.
So now my new code looks like:
aa <-
quantile(
UD_raster,
probs = c(0.25, 0.75),
type = 8,
names = TRUE
)
my.at <- c(aa[1], aa[2])
my.at <- round(my.at, 3)
levelplot(
UD_raster,
xlab = "",
ylab = "",
margin = FALSE,
contour = FALSE,
col.regions = viridis(100),
main = "A",
maxpixels = 2e5
) + layer(sp.polygons(Land, fill = "grey40", col = NA)) + layer(sp.points(positions, pts = 2, col = "red")) + contourplot(
UD_raster,
at = my.at,
labels = list(cex = 0.8, "verdana"),
label.style = "flat",
margin = FALSE
) + contourplot(
ETOPO2,
at = -200,
labels = FALSE,
margin = FALSE,
lty = 1,
pretty = TRUE
) + contourplot(
ETOPO2,
at = -1000,
labels = FALSE,
margin = FALSE,
lty = 2,
pretty = TRUE
) + contourplot(
ETOPO2,
at = -2000,
labels = FALSE,
margin = FALSE,
lty = 3,
pretty = TRUE
)
As one could expect, it takes a bit longer to produce the plot. Still no idea on how to change the colors of the UD contourplot.

Change the size of labels in mosaic function, R

I have a mosaic, and I want to change the size of the character of the labels. What can I do?
library(vcd)
tbl<-structable(GWage~Gender,data=dat)
mosaic(tbl,shade=TRUE, legend=TRUE)
Updated for Mosaic()
mosaic(UCBAdmissions, sort = 3:1,
gp_varnames = gpar(fontsize = 14, fontface = 1),
gp_labels = gpar(fontsize = 10),
main = "Student admissions at UC Berkeley")
?labelings
See ?labelings & play around with the font sizes till you get what you want. You should probably post a tibble /mini-extract of your data to make it easily reproducible.
you may be able to do more with mosaicplot()?
Did you try
cex.axis = 0.7 # or whatever size works for your plot?
#Example:
table1 <- table(airquality$Temp[1:7], airquality$Month[1:7])
mosaicplot(table1,
main = "Example",
xlab = "Y",
ylab = "X",
las = 2,
border = "chocolate",
cex.axis = 0.7,
shade = TRUE)

Adjust Font Size and Decimal Places in R Boxplot (ggpubr)

I am using the R package GGPubr to make Boxplots. I really like the nice visuals that it provides but am having problems. Does anyone know how to increase the font size of the numbers on the axes, and the axis labels, and class labels? Also how do I set the mean values so that they only display 2 decimal places?
Here is the code that I'm using:
library("ggpubr")
mydata <- read.csv("C:\\temp\\ndvi.csv")
ggboxplot(mydata, x = "class", y = "NDVI",
color = "class",
order = c("Conifer", "Deciduous", "Grasslands"), ggtheme=theme_gray(),
ylab = "NDVI Value", xlab = "Land Cover Class",
add="mean",
font.label = list(size = 30, face = "bold"))+ stat_summary(fun.data
= function(x) data.frame(y=1, label = paste("Mean=",mean(x))), geom="text")
+theme(legend.position="none")
And the csv:
NDVI,class
0.25,Conifer
0.27,Conifer
0.29,Conifer
0.403,Deciduous
0.38,Deciduous
0.365,Deciduous
0.31983489,Grasslands
0.32005,Grasslands
0.328887766,Grasslands
I would prefer to achieve the desired effects above with GGPubr rather than boxplot() or ggplot/ggplot 2. Thanks.
Here is one option where we use round() to take care of the two decimal places and add another theme() to change the text size.
ggboxplot(mydata, x = "class", y = "NDVI",
color = "class",
order = c("Conifer", "Deciduous", "Grasslands"), ggtheme=theme_gray(),
ylab = "NDVI Value", xlab = "Land Cover Class",
add="mean",
font.label = list(size = 30, face = "bold")) +
# use round() and set y = .45
stat_summary(fun.data = function(x) data.frame(y=1, label = paste("Mean=", round(mean(x), 2))), geom="text") +
theme(legend.position="none") +
theme(text = element_text(size = 16)) # change text size of theme components

Neat formatting for Venn diagram in R with unbalanced group sizes

I'm using the VennDiagram R package to try to generate a neatly formatted diagram comparing two groups. I have successfully used this package in the past to compare relatively similarly-sized groups. However, now I'm comparing groups that have significantly different sizes (# of unique elements in the first group is ~3,600, # of unique elements in the second group is ~60, and # of overlapping elements is ~80).
The appearance of my current Venn diagram is that the group with the larger # of elements has this value displayed within its circle, but the labels for the intersection of the two groups and the unique elements in the second group are too large to be included in those regions of the diagram, so instead, they are displayed outside of the diagram with a line connecting them to the associated region. I don't like the appearance of this, and would like to reduce the size of all 3 labels so that they can be displayed within their respective regions of the diagram. However, after having reviewed the associated documentation/examples and publication (Chen & Boutros 2011), I'm still not clear about how to do this. (For example, I see parameters that permit the specification of font size of the figure title and subtitle, but I don't see where the labels' font size can be specified...)
I have attempted workarounds such as trying to make the labels invisible so that I can manually add them in a separate application, but this doesn't seem to be an option...
Any suggestions for how I can reduce the font size of my labels and specify that these labels appear within the regions of the diagram rather than outside of the diagram, will be appreciated. Thanks!
Update: As requested below, I am providing my example code:
library(VennDiagram);
library(grid);
Data <- read.csv('ExampleDataset_VennDiagram.csv')
Dataset1 <- Data[,1]
Dataset2 <- Data[,2]
MyVennDiagram <- venn.diagram(
x = list(
A = Dataset1,
B = Dataset2
),
main = "",
main.cex = NULL,
filename = NULL,
lwd = 2,
fill = c("blue", "green"),
alpha = 0.75,
label.col = "black",
cex=c(2,2,2),
fontfamily = "sansserif",
fontface = "bold",
cat.col = c("blue", "green"),
cat.cex = 0,
cat.fontfamily = "serif",
cat.fontface = "bold",
cat.dist = c(0.05, 0.05),
cat.pos = c(-20, 14),
);
grid.newpage()
grid.draw(MyVennDiagram)
Update: Based on missuse's suggestion below, using ext.text = FALSE works perfectly!
Thanks to everyone who contributed to this thread.
The eulerr library appears to generate nice-looking diagrams, and will definitely be a resource I use in the future -- thanks for sharing.
A possible solution to this is to avoid using euler diagrams.
To illustrate your problem here is some data:
A = sample(1:1000, 500, replace = T)
B = sample(1:10000, 50)
Here is the diagram obtained by
library(VennDiagram);
library(grid)
MyVennDiagram = venn.diagram(
x = list(
A = A,
B = B
),
main = "",
main.cex = NULL,
filename = NULL,
lwd = 2,
fill = c("cornflowerblue", "pink"),
alpha = 0.75,
label.col = "black",
cex=c(2,2,2),
fontface = "plain",
cat.col = c("cornflowerblue", "pink"),
cat.cex = 0,
cat.fontfamily = "serif",
cat.fontface = "plain",
cat.dist = c(0.05, 0.05),
cat.pos = c(-20, 14),
cat.default.pos = "text",
)
grid.newpage()
grid.draw(MyVennDiagram)
by avoiding scaling of the circles with scaled = FALSE
MyVennDiagram = venn.diagram(
x = list(
A = A,
B = B
),
main = "",
main.cex = NULL,
filename = NULL,
lwd = 2,
fill = c("cornflowerblue", "pink"),
alpha = 0.75,
label.col = "black",
cex=c(2,2,2),
fontface = "plain",
cat.col = c("cornflowerblue", "pink"),
cat.cex = 0,
cat.fontfamily = "serif",
cat.fontface = "plain",
cat.dist = c(0.05, 0.05),
cat.pos = c(-20, 14),
cat.default.pos = "text",
scaled = FALSE
)
grid.newpage()
grid.draw(MyVennDiagram)
As per user20650 suggestion the best option is to use ext.text=FALSE in the original call:
Also check library(eulerr) it accepts a bit different input, here is an illustration:
library(eulerr)
library(tidyverse)
data.frame(dat = unique(c(A, B))) %>%
mutate(A = dat %in% A,
B = dat %in% B) %>%
select(A, B) %>%
euler() %>%
eulerr:::plot.euler(counts = T)
As per user20650 comment acceptable input is also:
plot(euler(setNames(list(unique(A),unique(B)), c("A", "B"))), counts=TRUE)

Resources