I am trying to produce a plot using the ggnet2 package in R. I have created a network (net.bg)
library(igraph)
library(GGally)
library(network)
library(sna)
library(intergraph)
direction <- c(2,1,3,1,4,1,5,1,3,2,4,2,5,2,4,3,5,3,5,4)
gr <- matrix(direction, nrow = 2)
x1 <- 10.59
x2 <- 15.74
x3 <- 5
x4 <- 18
x5 <- 7
RImp <- data.frame(x1,x2,x3,x4,x5)
nam <- names(RImp)
RImp <- as.numeric(RImp)
Rint <- c(2.96, 1.34, 1.27, 1.1, 2.22, 1.24, 3.11,
2.52, 0.96, 1.08)
net.bg <- make_graph(gr, 5)
and I am using ggnet2 and ggplot2 to plot it like so:
library(RColorBrewer)
library(ggnewscale)
library(ggplot2)
colfunction <- colorRampPalette(c("floralwhite", "firebrick1"))
nodeCol <- colfunction(5)
p <- ggnet2(net.bg,
mode = "circle",
size = 0,
#color = RImp,
label = nam,
edge.size = Rint,
edge.label = Rint,
edge.color = "grey") +
theme(legend.text = element_text(size = 10)) +
geom_label(aes(label = nam),nudge_y = 0.08) +
geom_point(aes(fill = RImp), size = RImp, col = nodeCol) +
scale_fill_continuous(name = "Variable\nImportance",
limits=c(0, 20), breaks=seq(0, 20, by= 5),
low = "floralwhite" ,high = "firebrick1")
p
Im using ggplot2
to actually plot the nodes, instead of ggnet2
, as this allows me to add a legend.
The above code produces a plot similar to this:
As can be seen,the nodes are being coloured as a gradient, however, they are being coloured in a clock-wise manner... I am trying to colour the nodes, based on their size (or in this case, the values contained within RImp
).
Any suggestions as to how I would achieve this?
The problem is, that the shape of the points is solid and geom_point
uses "nodeCol" as colour for the whole point (or rather circle). If you're using shape = 21
, you have the chance to change both the outline of the point (I'm using grey here and the fill - which is now correctly controlled by the scale_fill_continuous
):
ggnet2(net.bg,
mode = "circle",
size = 0,
#color = RImp,
label = nam,
edge.size = Rint,
edge.label = Rint,
edge.color = "grey") +
theme(legend.text = element_text(size = 10)) +
geom_label(aes(label = nam),nudge_y = 0.08) +
geom_point(aes(fill = RImp), size = RImp, col = "grey", shape = 21) +
scale_fill_continuous(name = "Variable\nImportance",
limits=c(0, 20), breaks=seq(0, 20, by= 5),
low = "floralwhite" ,high = "firebrick1")
If you scroll down on https://ggplot2.tidyverse.org/reference/geom_point.html you'll find examples using different point shapes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.