2

How can I plot a degree graoh like that? enter image description here

The picture is only indicative, the result may not be identical to the image. The important thing is that on the X axis there are labels of the nodes and on the Y axis the degree of each node.

Then the degree can be represented as a histogram (figure), with points, etc., it is not important.

This is what I tried to do and did not come close to what I want:

d = degree(net, mode="all")
hist(d)

or

t = table(degree(net))
plot(t, xlim=c(1,77), ylim=c(0, 40), xlab="Degree", ylab="Frequency")

I think it is a trivial thing but it's the first time I use R.

Thank you


This is what I have now: enter image description here I would like a graph that was more readable (I have 77 bars). That is, with more space between the bars and between the labels.

My aim is to show how a node (Valjean) has higher value than the other, I don't know if I am using the right graphic..

1 Answers1

2

You can just use a barplot and specify the row names. For example,

net = rgraph(10)
rownames(net) = colnames(net) = LETTERS[1:10]
d = degree(net)
barplot(d, names.arg = rownames(net))
csgillespie
  • 59,189
  • 14
  • 150
  • 185
  • Thanks for the reply, but in my case I can't use `LETTERS[1:10]` but I had to take label of each node. –  Mar 26 '16 at 12:52
  • I used letters to create the node names. – csgillespie Mar 26 '16 at 12:54
  • Ok, but I don't know how to modify your code to change `LETTERS` to my node labels.. –  Mar 26 '16 at 12:59
  • Mmm no, it prints NULL –  Mar 26 '16 at 13:06
  • So how did you generate the `net` object? – csgillespie Mar 26 '16 at 13:07
  • In this way `net <- read.graph("lesmiserables.gml",format=c("gml"))`. I'm wrong? –  Mar 26 '16 at 13:08
  • If you are using `igraph` (rather than `sna`) use `V(net)$name` – user20650 Mar 26 '16 at 14:06
  • @user20650 Thanks, I solved using `names.arg=V(net)@label` and `las=2`. Now the problem is that labels and rectangles are too attached, so it does not read anything. –  Mar 26 '16 at 15:45
  • @lon; can you edit your question with an example to show what you mean please: eg use an example graph to show the problem: `g <- erdos.renyi.game(10, 0.5)` – user20650 Mar 26 '16 at 16:02
  • @user20650 Thanks, I edit my main message. I hope it is more clear what I want. –  Mar 26 '16 at 16:07
  • okay, thanks. So the problem is that you have many labels - so you are limited in how you can deal with this. You could try 1) decrease text size (but this will likely make it difficult to read. 2) put the text at an angle 3) increase the `width` of your output device 4) You could `order` the bars from high to low, for example, and then only plot a subset of the nodes , say, those that have a degree greater than five – user20650 Mar 26 '16 at 16:15
  • ps. @csgillespie; answered your original question (use a barplot), so it would be good to mark it as answered (and upvote) – user20650 Mar 26 '16 at 16:20
  • @user20650 Thanks to you! I think the best choice is increase the `with` of output device, so I try `dev.new(width=10, height=4) plot(...)` but I don't' see changes to the image. Where am I wrong? –  Mar 26 '16 at 16:30
  • I would change the width on the output device (not the graphics window): for example, `pdf("test.pdf", width=20) ; barplot(degree(g), names.arg=V(g)$name, las=2) ; dev.off()`. Also for a bit more control over the labels see http://stackoverflow.com/questions/10286473/rotating-x-axis-labels-in-r-for-barplot – user20650 Mar 26 '16 at 16:40