showing vertex labels inside vertexes only - r

I have this data frame:
dput(df2)
structure(list(Receiver = structure(c(4L, 3L, 2L, 1L), .Label = c("Australia",
"United Arab Emirates", "United Kingdom", "United States of America"
), class = "factor"), Sender = structure(c(1L, 1L, 1L, 1L), .Label = "United States of America", class = "factor")), .Names = c("Receiver",
"Sender"), row.names = c(NA, -4L), class = "data.frame")
I would like to draw and igraph as this:
library(igraph)
g<-graph.data.frame(df2)
plot(g, layout = layout.kamada.kawai, vertex.label = V(g)$name,
vertex.label.color= "red", edge.arrow.size=0.8,
edge.curved=T, edge.label.color="white",
edge.label.cex=0.8,vertex.shape="circle",edge.color="pink",
vertex.color="lightblue", asp=0, margin=0)
I would like to show vertex lables inside the verexes, without increasing the size of the vertexes. Any ideas how I can do this?

You can do something like this before the call plot:
V(g)$label.cex <- 0.5
But why not to use a shortcut of the names?
V(g)$name<-c('USA','UK','UAE','Aus')
dat <- structure(list(Receiver = structure(c(4L, 3L, 2L, 1L), .Label = c("Australia",
"United Arab \nEmirates", "United \nKingdom", "United \nStates of \nAmerica"
), class = "factor"), Sender = structure(c(1L, 1L, 1L, 1L), .Label = "United \nStates of \nAmerica", class = "factor")), .Names = c("Receiver",
"Sender"), row.names = c(NA, -4L), class = "data.frame")
library(igraph)
g<-graph.data.frame(dat)
V(g)$label.cex <- 0.6
plot(g, layout = layout.kamada.kawai, vertex.label = V(g)$name,
vertex.label.color= "red", edge.arrow.size=0.8,
edge.curved=T, edge.label.color="white",
edge.label.cex=0.8,vertex.shape="circle",edge.color="pink",
vertex.color="lightblue", asp=0, margin=0)

The following might help.
# size the network nodes based on their centrality
deg = igraph::degree(graph = g, v = V(g), mode = "all", loops = TRUE, normalized = FALSE);
igraph::V(g)$size = deg*3;
# set the label size based on the node size
igraph::V(g)$label.cex = igraph::V(g)$size/max(igraph::V(g)$size);

Related

R: For loop to subset Granges objects by list

I have a txf_df which I subset by gene.list$entrez and then found the list of unique number of transcripts. The txf_df is then converted to txf_grange.
Now, I want to create a for loop of the 15 unique genes, where upon each iteration, subset the txf_grange objects by only the specific gene.
Code:
# Subset by the Entrez IDs
txf_df <- txf_df %>% filter(geneName %in% gene.list$entrez)
# Find the number of common transcripts
unique <- unique(txf_df$geneName)
length(unique)
# Recast this dataframe back to a GRanges object
txf_grange <- makeGRangesFromDataFrame(txf_df, keep.extra.columns=T)
# For each of the 15 genes, subset the Granges objects by only the gene
for (i in gene.list["entrez"]) {
for (j in txf_grange$geneName) {
if (i==j) {
assign(paste0("gene.", i), 1:j) <- txf_grange[j,]
}
}
}
Data:
> dput(head(txf_df))
structure(list(seqnames = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "16", class = "factor"),
start = c(12058964L, 12059311L, 12059311L, 12060052L, 12060198L,
12060198L), end = c(12059311L, 12060052L, 12061427L, 12060198L,
12060877L, 12061427L), width = c(348L, 742L, 2117L, 147L,
680L, 1230L), strand = structure(c(1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("+", "-", "*"), class = "factor"), type = structure(c(3L,
1L, 1L, 2L, 1L, 1L), .Label = c("J", "I", "F", "L", "U"), class = "factor"),
txName = structure(list(c("uc002dbv.3", "uc010buy.3", "uc010buz.3"
), c("uc002dbv.3", "uc010buy.3"), "uc010buz.3", c("uc002dbv.3",
"uc010buy.3"), "uc010buy.3", "uc002dbv.3"), class = "AsIs"),
geneName = structure(list("608", "608", "608", "608", "608",
"608"), class = "AsIs")), row.names = c(NA, 6L), class = "data.frame")
> dput(head(gene.list))
structure(list(Name = c("AQP8", "CLCA1", "GUCA2B", "ZG16", "CA4",
"CA1"), Pvalue = c(3.24077275512836e-22, 2.57708986670727e-21,
5.53491656902485e-21, 4.14482213350182e-20, 2.7795892896524e-19,
1.23890644641685e-18), adjPvalue = c(8.3845272720681e-18, 6.66744690314504e-17,
1.43199361473811e-16, 1.07234838237959e-15, 7.19135341018869e-15,
3.20529875816967e-14), logFC = c(-3.73323340223377, -2.96422555675244,
-3.34493724166712, -2.87787132076412, -2.87670608798164, -3.15664667432159
), entrez = c(AQP8 = "343", CLCA1 = "1179", GUCA2B = "2981",
ZG16 = "653808", CA4 = "762", CA1 = "759")), row.names = c(NA,
6L), class = "data.frame")

Draw scatter plot and set color based one categorial column in ggplot2

Given a dataframe as follows:
structure(list(city = structure(c(1L, 3L, 4L, 2L), .Label = c("bj",
"cq", "sh", "tj"), class = "factor"), area = c(1580.86, 1927.95,
532.24, 613.09), price = c(9.51, 94.42, 10.77, 8.58), level = structure(c(1L,
1L, 2L, 2L), .Label = c("a", "b"), class = "factor")), class = "data.frame", row.names = c(NA,
-4L))
I want to draw a scatter plot which x for area and y for price, at same time, the color of points only based on level, which means only two colors to distinguish a and b.
How could I draw this with ggplot2? Thanks.
You can use the following code for that
library(ggplot2)
ggplot(df, aes(x = area, y = price, col=level)) + geom_point()
ggplot(df, aes(x = area, y = price, col=level)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = city))
Data
df=structure(list(city = structure(c(1L, 3L, 4L, 2L), .Label = c("bj",
"cq", "sh", "tj"), class = "factor"), area = c(1580.86, 1927.95,
532.24, 613.09), price = c(9.51, 94.42, 10.77, 8.58), level = structure(c(1L,
1L, 2L, 2L), .Label = c("a", "b"), class = "factor")), class = "data.frame", row.names = c(NA,
-4L))

Credibility interval with respect two factors using ggplot2 in r

I have problem ploting credibility interval like this:
My data structure is following,L1,L2,M,U1,U2 stand for 0.025quant,0.25quant,0.5quant,0.75quant,0.975quant,respectively.
`
structure(list(approach = structure(c(1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("INLA",
"rjags")), param = structure(c(1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("alpha",
"beta", "sig2")), L1 = c(0.0844546867936143, 1.79242348175439,
0.163143886545317, 0.0754165380733685, 1.79067991488052, 3.66675821267498
), L2 = c(0.60090835904286, 1.95337968870806, 0.898159977552433,
0.606017177641373, 1.95260448314298, 4.07080184844179), M = c(0.870204161297956,
2.03768437879748, 2.20651061559405, 0.87408237273113, 2.03725552264872,
4.32531027636171), U2 = c(1.13905085248391, 2.12210930874551,
4.26836270504725, 1.66260576926063, 2.28900567640091, 5.10063756831338
), U1 = c(1.65214011950274, 2.28396345192398, 4.9109804477583,
1.1450384685802, 2.12117799328209, 4.55657971279654), AP = structure(c(1L,
4L, 5L, 2L, 3L, 6L), .Label = c("INLA.alpha", "rjags.alpha",
"INLA.beta", "rjags.beta", "INLA.sig2", "rjags.sig2"), class = "factor")), .Names = c("approach",
"param", "L1", "L2", "M", "U2", "U1", "AP"), row.names = c(NA,
-6L), class = "data.frame")`
I referenced this answerenter link description here,but 'fill' seems only work for boxplot case.the code I tried so far is:
CI$AP=interaction(CI$approach,CI$param)
p=ggplot(CI,aes(y=AP))+geom_point(aes(x=M))
p=p+geom_segment(aes(x=L1,xend=U1,y=AP,yend=AP))
p=p+geom_segment(aes(x=L2,xend=U2,y=AP,yend=AP),size=1.5)
It is far away from what I want.
Many thanks!
How about the following:
ggplot(df, aes(x = param, y = M, colour = approach)) +
geom_point(position = position_dodge2(width = 0.3), size = 3) +
geom_linerange(
aes(ymin = L2, ymax = U2, x = param),
position = position_dodge2(width = 0.3),
size = 2) +
geom_linerange(
aes(ymin = L1, ymax = U1, x = param),
position = position_dodge2(width = 0.3),
size = 1) +
coord_flip() +
labs(x = "Parameter", y = "Estimate")
Sample data
df <- structure(list(approach = structure(c(1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("INLA",
"rjags")), param = structure(c(1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("alpha",
"beta", "sig2")), L1 = c(0.0844546867936143, 1.79242348175439,
0.163143886545317, 0.0754165380733685, 1.79067991488052, 3.66675821267498
), L2 = c(0.60090835904286, 1.95337968870806, 0.898159977552433,
0.606017177641373, 1.95260448314298, 4.07080184844179), M = c(0.870204161297956,
2.03768437879748, 2.20651061559405, 0.87408237273113, 2.03725552264872,
4.32531027636171), U2 = c(1.13905085248391, 2.12210930874551,
4.26836270504725, 1.66260576926063, 2.28900567640091, 5.10063756831338
), U1 = c(1.65214011950274, 2.28396345192398, 4.9109804477583,
1.1450384685802, 2.12117799328209, 4.55657971279654), AP = structure(c(1L,
4L, 5L, 2L, 3L, 6L), .Label = c("INLA.alpha", "rjags.alpha",
"INLA.beta", "rjags.beta", "INLA.sig2", "rjags.sig2"), class = "factor")), .Names = c("approach",
"param", "L1", "L2", "M", "U2", "U1", "AP"), row.names = c(NA,
-6L), class = "data.frame")

Mosaic Plot does not fit

The labels for the mosaic plot don't fit the screen ( they're partially cut) so id like to move/shift the plot to the right so that the labels fully fit -- tried using ''par'' function but to no avail -- any ideas?
structure(list(Road_Type = structure(c(4L, 4L, 4L, 4L, 4L, 4L
), .Label = c("Roundabout", "One way Street", "Dual Carriageway",
"Single carriageway", "Slip Road"), class = "factor"), Accident_Severity_combined = structure(c(2L,
2L, 2L, 2L, 1L, 2L), .Label = c("Serious", "Slight"), class = "factor")), .Names = c("Road_Type",
"Accident_Severity_combined"), row.names = c(NA, 6L), class = "data.frame")
>
mos <- mosaic(~Road_Type + Accident_Severity_combined, data = uk1, shade = TRUE, legend = TRUE,
labeling_args = list(set_varnames = c(Accident_Severity_combined="Gender", Road_Type="survival"),
highlighting_fill = c("darlblue","red")
labeling=labeling_border(
rot_labels = c(90, 0, 90, 0),
just_labels=c("left","left","right","right"),
tl_varnames = FALSE,
gp_labels = gpar(fontsize = 9)))

Draw radial background in ggplot

this is my dataset:
> dput(dfw)
structure(list(SITE = c("ASPEN", "ASPEN", "BioCON", "DUKE", "Lancaster",
"Merrit Island", "Nevada FACE", "NZ", "ORNL", "PHACE", "BioCON"
), SPECIES = c("A", "AB", "Legume", "PITA", "mixed", "Oak", "desert",
"grassland", "SG", "grassland", "C3forb"), FRr = c(0.197028535345918,
0.296799297050907, 0.195436310641759, 0.152972526753089, 0.0313948973476966,
0.139533057346518, 0.188221278921143, NA, 0.70542764380006, 0.119320766735777,
0.135665667633474), Nupr = c(0.122177669046786, 0.305573297532757,
0.131181914007488, 0.217519050530067, -0.0436788294371676, 0.153632658941404,
-0.00803217169726427, 0.168440046857285, 0.145172439177718, -0.108563178158001,
0.00546006390438276), myc = c("ECM", "ECM", "N-fixing", "ECM",
"ECM", "ECM", "AM", "AM", "AM", "AM", "AM"), SITE_Sps = structure(c(1L,
2L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 3L), .Label = c("Aspen FACE-A",
"Aspen FACE-AB", "BioCON", "BioCON-legumes", "Duke FACE", "Lascaster",
"Florida OTC", "Nevada FACE", "NZ FACE", "ORNL FACE", "PHACE"
), class = "factor")), row.names = c(NA, -11L), vars = list(SITE,
SPECIES, myc), indices = list(0L, 1L, 10L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L), group_sizes = c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
SITE = c("ASPEN", "ASPEN", "BioCON", "BioCON", "DUKE", "Lancaster",
"Merrit Island", "Nevada FACE", "NZ", "ORNL", "PHACE"), SPECIES = c("A",
"AB", "C3forb", "Legume", "PITA", "mixed", "Oak", "desert",
"grassland", "SG", "grassland"), myc = structure(c(2L, 2L,
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("am", "ecm",
"ecm+am"), class = "factor")), row.names = c(NA, -11L), class = "data.frame", vars = list(
SITE, SPECIES, myc), .Names = c("SITE", "SPECIES", "myc")), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), .Names = c("SITE", "SPECIES",
"FRr", "Nupr", "myc", "SITE_Sps"))
I want to draw the same background as in the attached figure, added to my current ggplot code:
ggplot(dfw, aes(FRr, Nupr, group=myc, label = SITE_Sps)) +
geom_point(aes(fill=myc),size=4,shape = 21) +
geom_text() +
geom_hline(yintercept=0) + geom_vline(xintercept = 0) +
geom_abline(intercept = 0, slope = 1, linetype = "longdash")
I guess I should use the function geom_polygon, but I don't really know how to create a dataset to draw all the required segments, including the colour gradient from dark grey to light grey and white.
Perhaps this could be a start?
nlines <-
phis <- seq( 0, 2*pi, by=2*pi/nlines )
rad <- 999
xs <- rad * cos( phis )
ys <- rad * sin( phis )
Here is a way using geom_polygon:
nlines <- 25
inc <- pi/(nlines)
phis <- seq( -pi/2, by=inc, length.out = nlines )
rad <- 1
#Create the triangles
points <- lapply(phis, function(a) {
x <-c(0, rad*cos(a), rad*cos(a+inc),0, -rad*cos(a), -rad*cos(a+inc))
y <-c(0, rad*sin(a), rad*sin(a+inc),0, rad*sin(a), rad*sin(a+inc))
g <-c(a,a,a,a,a,a) # used for grouping
data.frame(x,y,g)
})
#Create a data.frame to be used on ggplot
bckg <- do.call(rbind,points)
#You need to set the data for each geometry as we have more than one dataset
ggplot(mapping=aes(FRr, Nupr, group=myc)) +
#Draw the background
geom_polygon(data=bckg,aes(x=x,y=y,group=g,alpha=g), fill = "gray50")+
geom_point(data=dfw, aes(FRr, Nupr, group=myc, fill=myc),size=4,shape = 21) +
geom_text(data=dfw, aes(FRr, Nupr, group=myc, label = SITE_Sps), nudge_y = -0.02) +
geom_hline(data=dfw,yintercept=0) + geom_vline(data=dfw,xintercept = 0) +
geom_abline(data=dfw,intercept = 0, slope = 1, linetype = "longdash")+
#We need to define a scale in ourder to deal with out of boundary points on the background
scale_x_continuous(limits = c(-0.2,0.4), oob=function(x, rg) x)+
scale_y_continuous(limits = c(-0.2,0.4), oob=function(x, rg) x)+
scale_alpha_continuous(guide="none", range=c(1.0,0))+
theme(panel.background = element_blank())
Here is the plot:

Resources