I am currently drawing a qgraph (spring) in R, but I wonder if it is possible to vary the size of the nodes in the graph?
the vsize argument should do the trick
qgraph(data, vsize = 2)
from ?qgraph:
vsize
A value indicating the size of the nodes (horizontal if shape is "rectangle". Can also be a vector of length 2 (nodes are scaled to degree) or a size for each node. Defaults to 8*exp(-nNodes/80)+1
vsize2
A value indicating the vertical size of the nodes where the shape is "rectangle". Can also be a vector of length 2 (nodes are scaled to degree) or a size for each node. Defaults to the value of 'vsize'. If 'vsize' is not assigned this value is used as a scalar to 'vsize' (e.g., vsize2 = 1/2 would result in rectangled nodes where the height is half the default width)
Related
I want to plot a simple star graph in which the size of the edges depends on a score representing a difference of perception between the central node (e.g.,a leader) and the other nodes (e.g., its employees).
I succeeded in modifying the colors, the size of the node, the width of the edges but not the size of the latter.
How would you do?
library(igraph)
nodes <- read.csv("exemple_nodes.csv", header=T, as.is=T)
links <- read.csv("exemple_edges.csv", header=T, as.is=T)
st <- graph_from_data_frame(d=links, vertices=nodes, directed=T)
plot(st, vertex.color=V(st)$perception.type)
With the ggraph package and one of the geom_edge_ func' (e.g., geom_edge_arc, geom_edge_diagonal), in order to use the edge_width parameter, depending on a numeric value associated with the edges, in the edges-list (hereafter "value"). For example:
ggraph::ggraph(st) +
ggraph::geom_edge_diagonal(aes(edge_width = as.numeric(value)) )
In addition, ggraph allow you to specify other edges-parameters inside the geom_edge_ func', for example edge_alpha = as.numeric(value).
I think that what you want is to position the vertices so that you can control the length of the edges. If that is not what you want, then please explain what you mean by the "size" of the edges.
You do not provide your data so that we cannot use exactly your graph. I will use a generic star graph as an example. In order to control the placement of the vertices, you need to use the layout parameter. The basic function layout_as_star will place the first vertex at the center and the other vertices equally spaced around it at the same distance. Because this layout function places the center vertex at (0,0) and the remaining nodes on a unit circle around the center, it is easy to adjust it so that the distance of the outer vertices is controlled by a parameter. Just multiply the coordinates by the parameter and it will proportionally change the distance. I just make something up for the distances, but you can use your parameter.
## Make up perception parameter
set.seed(271828)
Perception = sample(4, 9, replace=T)
Perception
[1] 2 3 4 4 1 4 2 2 1
Now there is one weight for every outer vertex, but we need a weight for the central vertex. We don't want it to move so we use a weight of 1.
Weight = c(1, Perception)
LO = layout_as_star(S10)
LO = LO*Weight
plot(S10, layout=LO)
Thanks for your help in advance!
My question is, given a list of sets, how can I visualize the overlap of any of the two sets using the network plot as shown below?
Please feel free to generate any sets for demonstration. Or you can use the following simple sets.
set.seed(123456)
A <- sample(1:100, 60)
B <- sample(1:100, 50)
C <- sample(1:100, 75)
In ggraph we must use scale_size() for nodes and scale_edge_width() for edges to harmonize proportions. Point sizes in ggplot are scaled by their radius already:
Does size for ggplot2::geom_point() refer to radius, diameter, area, or something else?
so no transformations are necessary, unless you want the point size to be proportional to the edge width by area.
Build a tbl_graph with your samples
#edges are determined by length of intersection
edges <- data.frame('from'=c('A','B','C'),'to'=c('B','C','A'),
'weight'=c(length(intersect(A,B)),length(intersect(B,C)),length(intersect(C,A))))
#nodes are weighted by the length of the sample
nodes <- data.frame('name'=c('A','B','C'),size=c(length(A),length(B),length(C)))
tbl_graph <- tbl_graph(nodes=nodes,edges=edges)
Now, if you build the network directly with these sizes, the distances between nodes will be decided automatically, and most ggraph layouts set distances between nodes between 0 and 1, resulting in a crowded graph with oversized edges and nodes. If the distance between nodes is not important, we can simply use a scaling factor to scale the node sizes and edge widths down to fit the graph.
In order to harmonize width and sizes, we scale the range of the edge widths to the min and max of the edge widths, and scale node sizes to the min and max of the nodes' sizes, multiplied by 2, as the nodes are scaled by diameter. This way, node sizes and edge widths are scaled to their actual values, rather than decided by the layout. I also include here additional annotation methods to show the sizes of the nodes and edges. node_point shape=21 is the empty circle. Good luck!
scale_factor = 0.1
ggraph(tbl_graph) + geom_edge_link(aes(width=weight*scale_factor,label=weight),label_dodge=unit(-4,'mm'),angle_calc='along') +
scale_edge_width(range=c(min(edges$weight)*scale_factor,max(edges$weight)*scale_factor)) +
geom_node_point(aes(size=size * scale_factor),shape=21) + scale_size(range=c(min(nodes$size)*scale_factor*2,max(nodes$size)*scale_factor*2)) +
theme_linedraw() + geom_node_text(aes(label=paste(name,':',size)),nudge_x=-0.1)
resulting ggraph
I need to create small multiples (like the one shown in the picture) using ggplot2 where each circle occupies 70% of the total size of it's own small multiple and the remaining 30% is empty space.
I know the center and the radius of the circle.
Question: Is it possible to do this, and if so, how?
Area of a circle = C = π * r²
Area of your square = S = width * length = side²
So just calculate C, so you can calculate how big S must be to fullfill your criteria. When you know S you know the side, which translates to how you have to set the axis limits for x and y.
I am plotting a food web ecological network with igraph and I am trying to avoid having my nodes to overlap.
The y-axis depends on the attributes of the nodes (i.e. the species trophic levels, varying between 1-3.52, no nodes between [1,2] by convention).
To do so, I calculate how many nodes there are within certain intervals of the y-axis range (1 to 4 with increments of 0.5).
If all nodes have the same size (defined by the user), I calculate the length of x-axis at each levels of y, as the sum of the size of the nodes (as if the nodes would be next to each other on a single axis):
xl <- (n nodes + 1 + 2 ) * size
I add 1 here since the nodes should be plotted from the certain of the nodes, so there is half a node on the left of the minima and right of the maxima of the x-axis. (The +2 is just to give a little space, and yes I know 1+2 =3 :) just to make the explanation easier).
I then calculate the break points on the x-axis calculating a sequence from half of the length I calculated above, centered around 0.
seq(-xl/2, xl/2,length.out = nnodes)
Doing so, each center of the nodes should be at a breakpoint on the x-axis with some distance between the nodes.
If the nodes have different sizes (defined by the user), I use the maximum size in the formula above so that I have enough space between each center of the nodes.
I combine the x-axis and y-axis coordinates as a matrix to use as my layout.
coordsp <- cbind(xcoord, TLsp)
I use rescale within the parameters of the plotting function.
plot(network, layout= coorsp,
rescale=TRUE,...)
I still get some overlap.
I tried using rescale = FALSE, specifying the x-axis and y-axis limits (range of the coordinates in coordsp) but the graph end up looking weird or the plotting window turns completely white.
I think the issue when rescale = TRUE is that it rescales the coordinates but not the size of the nodes, so that if they are too big for a window [-1,1]x[-1,1], the nodes end up overlapping.
Any guess on how to avoid overlap?
Thanks in advance
There is this interesting game, that has numbers in a grid, where each number has progressively smaller font. The player's task is to click on the numbers in succession.
I'm interested in the algorithm that creates the boxes for the numbers, I cannot think of a way it works.
Apparently the grid has N numbers (apart from the 1.88 in the picture), number 1 has the biggest font and succesively the font size decreases. Then the numbers are somehow placed on the grid and boxes grow around them. But it doesn't seem totally random, as there are some horizotal lines that go across the whole grid.
Do you have an idea on how this might work?
It looks to me as though the boxes have been generated by successive division. That is, starting with the full rectangle, a dividing line (horizontal or vertical) was placed, and then the two resulting rectangles were subdivided in turn, until there were enough rectangles for the game.
Here's a sketch of the first algorithm I would try. N is the number of rectangles I want to divide the original rectangle into, and A is a critical aspect ratio used to stop the small rectangles getting too narrow. (Perhaps A = 1.5 would be a good start.)
Create an empty priority queue and add the full rectangle to it.
If the length of the priority queue is greater than or equal to N, stop.
Remove the largest rectangle, R, from the priority queue.
Choose whether to divide it horizontally or vertically: if its aspect ratio (width/height) is greater than A, divide it vertically; if less than 1/A, divide it horizontally, otherwise choose at random.
Decide where to put the dividing line. (Perhaps randomly between 40% and 60% along the chosen dimension.)
This divides R into two smaller rectangles. Add both of them to the priority queue. Go to step 2.
When this algorithm completes, there are N rectangles in the queue. Put the number 1 in the largest of them, the number 2 in the second largest, and so on.
It turns out that putting the numbers into the boxes is not quite as straightforward as I assumed in my first attempt. The area metric works well for subdividing the rectangles nicely, but it doesn't work for putting the numbers into the boxes, because for fitting text into a box, the height and width both have to be taken into account (the area is not so useful).
Instead of explaining the algorithm for putting numbers into the boxes, I will just give you some sample code in Python and let you reverse-engineer it!
import heapq, itertools, random
import Image, ImageDraw, ImageFont
# ALGORITHM PARAMETERS
aspect_max = 1.5 # Above this ratio, always divide vertically
aspect_min = 1.0 # Below this ratio, always divide horizontally
div_min = 0.4 # Minimum position for dividing line
div_max = 0.6 # Maximum position for dividing line
digit_ratio = 0.7 # Aspect ratio of widest digit in font
label_margin = 2 # Margin around label (pixels)
class Rectangle(object):
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.w = w
self.h = h
self.area = self.w * self.h
self.aspect = float(self.w) / self.h
def __le__(self, other):
# The sense of this comparison is inverted so we can put
# Rectangles into a min-heap and be able to pop the largest.
return other.area <= self.area
def __repr__(self):
return 'Rectangle({0.x}, {0.y}, {0.w}, {0.h})'.format(self)
def divide(self, n):
"""
Divide this rectangle into `n` smaller rectangles and yield
them in order by area (largest first).
"""
def division(l):
return random.randrange(int(l * div_min), int(l * div_max))
queue = [self]
while len(queue) < n:
r = heapq.heappop(queue)
if (r.aspect > aspect_max
or r.aspect > aspect_min
and random.random() < 0.5):
# Vertical division
w = division(r.w)
heapq.heappush(queue, Rectangle(r.x, r.y, w, r.h))
heapq.heappush(queue, Rectangle(r.x + w, r.y, r.w - w, r.h))
else:
# Horizontal division
h = division(r.h)
heapq.heappush(queue, Rectangle(r.x, r.y, r.w, h))
heapq.heappush(queue, Rectangle(r.x, r.y + h, r.w, r.h - h))
while queue:
yield heapq.heappop(queue)
def font_height(self, n):
"""
Return the largest font height such that we can draw `n`
digits in this rectangle.
"""
return min(int((self.w - label_margin * 2) / (digit_ratio * n)),
self.h - label_margin * 2)
def draw_rectangles(rectangles, fontfile):
"""
Create and return an Image containing `rectangles`. Label each
rectangle with a number using the TrueType font in `fontfile`.
"""
rectangles = list(rectangles)
im = Image.new('RGBA', (1 + max(r.x + r.w for r in rectangles),
1 + max(r.y + r.h for r in rectangles)))
draw = ImageDraw.Draw(im)
for digits in itertools.count(1):
rectangles = sorted(rectangles,
key = lambda r: r.font_height(digits),
reverse = True)
i_min = 10 ** (digits - 1)
i_max = 10 ** digits
i_range = i_max - i_min
for i in xrange(i_range):
if i >= len(rectangles): return im
r = rectangles[i]
draw.line((r.x, r.y, r.x + r.w, r.y, r.x + r.w, r.y + r.h,
r.x, r.y + r.h, r.x, r.y),
fill = 'black', width = 1)
label = str(i + i_min)
font = ImageFont.truetype(fontfile, r.font_height(digits))
lw, lh = font.getsize(label)
draw.text((r.x + (r.w - lw) // 2, r.y + (r.h - lh) // 2),
label, fill = 'black', font = font)
rectangles = rectangles[i_range:]
Here's a sample run:
>>> R = Rectangle(0, 0, 400, 400)
>>> draw_rectangles(R.divide(30), '/Library/Fonts/Verdana.ttf').save('q10009528.png')
The pattern of the cuts looks recursive. That is, the process of dividing the region into rectangles consists of cutting a rectangle in two, over and over. There are two cuts that divide the whole rectangular region (the horizontal cuts above and below 1), so we can't tell which cut came first, but we see the cuts as a kind of tree: the cut that separates 1 from 10 produced a large rectangle below it (20, 21, 4, 10, etc.), which was then divided by the vertical cut between 21 and 4, the rectangle containing 4 was later divided by the cut that separates 4 and 14, and so on. There are N cuts that produce N regions plus one leftover ("1.88") which is not necessary but which might give us a clue.
Now we just have to figure out the order of the cuts, the choice of proportion and the choice of orientation.
Consecutive numbers are rarely neighbors, so it looks as if numbers are not assigned as the cutting progresses. Instead, the region is chopped into rectangles, the rectangles are sorted by size and then numbers are assigned (notice that 20 and 21 are neighbors, but they were formed by other cuts after the one that divides them).
A plausible hypothesis for the order of the cuts is that the algorithm always cuts the largest rectangle. If that were not true, we might see, e.g., 14 bigger than 15 and 18 combined, and I see no example of that.
Proportion... With careful measurement we could see the actual distribution of proportions, but I don't feel like doing that much work. We see no very long, thin rectangles and no 50/50 cuts, so at a guess I'd say the algorithm chooses randomly, in some range like [0.6, 0.8]. Maybe it tries to avoid making a rectangle very close to the size of a rectangle that already exists. After all the cuts, the rectangle chosen to be left over ("1.88") is neither the biggest nor the smallest; maybe it's random, maybe it's the second-biggest, maybe it's something else-- more examples would be useful.
The orientation seems to be strongly biased towards cutting rectangles across their narrow width, rather than "lengthwise". This has the effect of producing rectangles more like squares and less like books on a shelf. The only possible exception I can see is the 1-9 cut, which might divide the block whose lower-right number is 1 lengthwise. But that depends on the order of cuts above and below 1, so it leads to a hypothesis: the algorithm always cuts a rectangle along its shorter dimension, and the 1-9 cut was actually the first.
That's about as far as I can go, short of breaking out a ruler and calculator.