3

I'm having trouble adding a title and legend to the inside of my graph. Is it not possible with tkplot OR plot?

    dff <- data.frame(a = c(0,1,2,3,4),b = c(3,4,5,6,7))
    nod <- data.frame(node = c(0:7),wt = c(1:8))
    pg <- graph_from_data_frame(d = dff, vertices = nod,directed = F)

    # plot function with edge.label added
    tkplot(pg, edge.label = nod$wt, main = "this is my graph")
    legend('topleft',legend= degree(pg)*5,pt.cex=20,col='white',
    pch=21, pt.bg='white')

It doesn't seem to want to work the way I want it to. I just want the legend to show the vertices adjusted to be bigger for a higher degree.

I was also wondering if there is a way to plot only certain vertices from the graph? For example, you choose one vertex and plot only the vertices that form a path back to it?

hmnoidk
  • 545
  • 6
  • 20

1 Answers1

0

Based on the help for tkplot, here's an example of how to add a title to an interactive plot:

library(tcltk)

id = tkplot(pg, edge.label = nod$wt, main = "this is my graph")
canvas = tk_canvas(id)

width = as.numeric(tkcget(canvas, "-width"))
height = as.numeric(tkcget(canvas, "-height"))
tkcreate(canvas, "text", width/2, 25, text="My title",
         justify="center", 
         font=tkfont.create(family="helvetica", size=20, weight="bold"))

enter image description here

For the standard base R plot, your code works as is:

plot(pg, edge.label = nod$wt, main = "this is my graph")
legend('topleft',legend= degree(pg)*5,pt.cex=20,col='white',
       pch=21, pt.bg='white')

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285