Fix axis numbering in Circlize chord diagram and font issues - r

I have been experimenting with the 'circlize' package but I have been having issues with my axis label spacing. I wanted to customize the axis labelling to have major ticks at a specified intervals. I succeeded using the below code, but my issues are that as I change font size, the outer labels of each sector of the chord diagram, move in (a white space issue I believe). I cant find anything through my searching and need some help to fix this issue. The larger the font size, the more drastic the labels move inwards.
Additionally, I cannot find out how to change the font to 'Calibri' or bold (I have tried extrafonts and showtext but may not be implementing it correctly). Any thoughts would be appreciated.
Here is a reproducible example:
#sample matrix
A<-c(4680,0,0,0,0,0,0,0,0,0)
B<-c(109,23323,0,0,0,0,0,0,0,0)
C<-c(9,12,5405,0,0,0,0,0,0,0)
D<-c(19,25,26,4615,0,0,0,0,0,0)
E<-c(108,816,32,38,5511,0,0,0,0,0)
F_1<-c(50,319,67,55,202,18198,0,0,0,0)
G<-c(15,11,8,178,11,18,38279,0,0,0)
H<-c(24,180,22,39,171,212,14,10673,0,0)
I<-c(211,2378,18,28,1603,251,25,188,34458,0)
J<-c(162,1623,31,48,1003,370,34,265,5319,27235)
m <- rbind(A,B,C,D,E,F_1,G,H,I,J)
row.names(m) <- LETTERS[seq(1,10,1)]
colnames(m)<- LETTERS[seq(1,10,1)]
The graph:
colfunc <- colorRampPalette(c("#856705", "#d3d3d3", "#113069"))
grid.col <- setNames(colfunc(10), LETTERS[seq(1,10,1)])
grid.col
library(showtext); library(circlize)
#my attempt to load the correct font but I am unsure how to call the font within circlize.
font_add(family = "Calibri", regular = "C:/Windows/Fonts/calibri.ttf", italic ="C:/Windows/Fonts/calibrii.ttf", bold = "C:/Windows/Fonts/calibrib.ttf", bolditalic = "C:/Windows/Fonts/calibriz.ttf")
circos.clear()
showtext_auto()
circos.par(start.degree = 90, gap.degree = 5)
par(family = "Calibri") #tried to set the family to "Calibri"
chordDiagram(m/1000,
transparency = 0.5,
grid.col = grid.col,
annotationTrack = "grid",
self.link = 1,
link.lwd = 0.5, # Line width
link.lty = 1,
link.border = "light grey",
annotationTrackHeight = mm_h(10))
for(si in get.all.sector.index()) {
circos.axis(h = 'top',
major.at = c(0,5,10,15,20,25,30,35,40,45,50),
labels = c(0,5,10,15,20,25,30,35,40,45,50),
minor.ticks = 4,
labels.cex = fontsize(30),
sector.index = si,
track.index = 1,
labels.niceFacing = TRUE)
xlim = get.cell.meta.data("xlim", sector.index = si, track.index = 1)
ylim = get.cell.meta.data("ylim", sector.index = si, track.index = 1)
circos.text(mean(xlim),
mean(ylim),
si,
sector.index = si,
track.index = 1,
niceFacing = TRUE,
cex = fontsize(50),
col = "white",
font = par("Calibri"))
}
dev.copy(png, "test chord diagram.png", width = 5, height = 5, units = "cm", res = 600)
dev.off()
circos.clear()
With this, I get this graph:
As you can see from the graph, the '0' are all off position of their respective mark. Additionally , sector 'C' and 'D' have their axis labels moved in and are overlapping (including for sector 'J').
So, if anyone has an idea to move the end labels above their respective position, how to change the font to 'Calibri' and to bold, that would be great.
Thank you.

Well it seemed that the labels were fixed by using "labels.pos.adjust = c(0,0)", in the "circos.axis" argument. The only issue left is that the font still needs to be change to calibri.

Related

Adjust the legend labels in image plot

Normally I provide some sample code for an reproducible example, but in this case it is a bit tricky. But I believe this question can be solved also without a reproduced example.
I have an image plot generated with the following code:
col <- c("red", "white", "blue") # set the color palette
image.plot(lon.list[[1]], lat.list[[1]], MK.list3[[6]][[8]],
main = paste0("Drought Index Trend \n", names(Index.list[[1]])[1]),
col = col,
xlab = "Longitude [°]",
ylab = "Latitude [°]",
legend.lab = "",
legend.line = 2.5,
zlim = c(-1,1))
plot(sf_object, add = T, border = "Black")
The resulting plot is the following:
I have a problem with the legend labels. Since there are only three values, -1, 0, 1, it is sufficient that only these values are displayed at the legend in the middle of each color.
So it should like this (the different colors don't matter):
Anybody with a hint how to achieve this?
Thanks to the comment of user20650, I found the solution. Here is the updated code which does what I want:
col <- c("red", "white", "blue") # set the color palette
image.plot(lon.list[[1]], lat.list[[1]], MK.list3[[6]][[8]],
main = names(Index.list[[1]])[8],
col = col,
xlab = "",
ylab = "",
legend.lab = "",
legend.line = 2.5,
zlim = c(-1,1),
axis.args = list(c(-1:1))) ### this is the new part ###
plot(sf_object, add = T, border = "Black")

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.

I want to adjust the graph area in ggplot2

I made the following graph using the geomnet package and ggplot2. Then exported it to a pdf. But the graph by itself seems to be larger than the graphing area. it seems to be framed in a small square, as you can see in this picture:
graph
I don't know how to change the size of the square that's framing my graph so that the net nodes will be shown fully in my pdf. Thanks in advance.
Here's the code i'm using, and a data example:
red_list<-data_frame(From=c("A","B","C","D","D"),To=c("C","C","D","Z","A"))
red_list%>%ggplot(aes(from_id=From,to_id=To))+
geom_net(layout.alg = "circle", labelon = TRUE,
size = 12, directed = TRUE, fontsize=2, vjust = 0.5, labelcolour = "grey80",
arrowsize = 1.5, linewidth = 0.5, arrowgap = 0.05, col="darkred",
selfloops = F, ecolour = "grey40") +
theme_net() +
theme(plot.title=element_text(hjust=.5),
plot.subtitle=element_text(hjust=.5))+
ggtitle(label=paste("Figura",i,sep=" "),subtitle = paste("Interacciones entre los sectores de",names(red_list)[i],by=" ")))
I'm not sure how well it will work with graphs, but I uselly play with coord_cartesian(xlim = c(...,...), ylim = c(...,...) to adjust the plotting area.

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)

Plot legend problems using R. Help removing excessive white space in legend box

I am creating a plot of some hydrology data and I am having trouble with the legend. The legend has a large amount of blank space around the text. I have tried using par(mar= list), cex, x.intersp, and anything else I could find with no luck. I can't post images yet, but there is a link below. This is all of the code I am using to create the plot and legend. The abline() below indicates the mean value of the data and that is what I am putting in the legend. I have researched on the site and I can make it work using mtext() or legend(no bg or box) and rect(), but it seems like I am missing something. I am fairly new to R, but I have written code in the past. I am using RStudio version 0.99.903 and R version 3.2.2.
plot(dataFile$time_s,dataFile$w_cmps, type = "l",col="grey50",
xlim = c(0,18.5), ylim = c(-7,7), xlab = "Time (s)", ylab = "w (cm/s)",
main = "Vertical Velocity", xaxt="n")
axis(side = 1, at = c(0,2,4,6,8,10,12,14,16,18),
labels = c(0,2,4,6,8,10,12,14,16,18))
abline(h = mean_w, col = "black", lty = 3, lwd = 4)
legend("bottomleft","Mean w", lty = 3, col = "red",
xjust = 1, cex = 1, bg = "grey75", trace = TRUE, ncol = 1, xpd = NA,
seg.len = 1, y.intersp = 0.25, x.intersp = 0.25)

Resources