Add Title and Axis Labels to Riverplot?

Add Title and Axis Labels to Riverplot?

I'm trying to make a Sankey diagram using the riverplot package, and was wondering if it's possible to add a title and axis labels to the plot. To keep it simple I'll use one of the sample riverplots from the package:

nodes <- c( LETTERS[1:3] )
edges <- list( A= list( C= 10 ), B= list( C= 10 ) )
r <- makeRiver( nodes, edges, node_xpos= c( 1,1,2 ),
                node_labels= c( A= "Node A", B= "Node B", C= "Node C" ),
                node_styles= list( A= list( col= "yellow" )) )
plot( r , srt = 0)

I've tried using labs() and get the following error message: 'non-numeric argument to binary operator' (which wasn't unexpected), but since the package itself doesn't seem to have arguments for the title or axis labels, I'm at a bit of a loss. Any help is much appreciated!

2 Answers

You need function title

library(riverplot)


nodes <- c( LETTERS[1:3] )
edges <- list( A= list( C= 10 ), B= list( C= 10 ) )
r <- makeRiver( nodes, edges, node_xpos= c( 1,1,2 ),
                node_labels= c( A= "Node A", B= "Node B", C= "Node C" ),
                node_styles= list( A= list( col= "yellow" )) )

plot(r, srt = 0,
     main = "My Title")

title(main = "My title",
      xlab = "My label for axis X",
      ylab = "My label for axis Y")

This code gives you

Try creating an empty plot first with your labels and then riverplot::riverplot has an argument add. Set that to True to plot on your empty initial plot. You can customize the initial plot as you see fit with the base facility.

# empty plot
plot(0, 
     type = "n",
     xlab = "x-axis", 
     ylab = "y-axis", 
     frame.plot = F,
     xaxt = "n",
     yaxt = "n",
     main = "TITLE")
riverplot::riverplot(r,
                     add = T) # add to empty plot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Maya Lin-Takahashi
Author

Maya Lin-Takahashi

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.