Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8792129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:56:14+00:00 2026-06-13T22:56:14+00:00

I have the following data frame: structure(list(X1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L,

  • 0

I have the following data frame:

structure(list(X1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L
), .Label = c("1", "2", "3", "4", "5", "6"), class = "factor"), 
    X2 = structure(c(1L, 6L, 8L, 10L, 12L, 13L, 3L, 4L, 1L, 6L, 
    7L, 9L, 10L, 12L, 13L, 3L, 4L, 5L, 10L, 12L, 13L, 4L, 1L, 
    6L, 12L, 13L, 3L, 1L, 6L, 7L, 8L, 10L, 11L, 12L, 13L, 2L, 
    3L, 11L, 12L, 13L), .Label = c("I1", "I10", "I11", "I12", 
    "I13", "I2", "I3", "I4", "I5", "I6", "I7", "I8", "I9"), class = "factor")), .Names = c("X1", 
"X2"), row.names = c(NA, -40L), class = "data.frame")

Where in X1 is the person number and in X2 the group to which the person belongs to. One person can be in different groups.
Now I want to draw a line from each person to each group he belongs to. With plot() I solved it this way:

plot(0, xlim=c(0,1), ylim=c(0,1), type="n", axes=FALSE, xlab="", ylab="")

factor.to.int <- function(f) {
  (as.integer(f) - 1) / (length(levels(f)) - 1)
}

segments(factor.to.int(data$X1), 0, factor.to.int(data$X2), 1, col=data$X1)
axis(1, at = seq(0, 1, by = 1 / (length(levels(data$X1)) - 1)), labels = levels(data$X1))
axis(3, at = seq(0, 1, by = 1 / (length(levels(data$X2)) - 1)), labels = levels(data$X2))

The result looks like this:
bipartite graph

Now I wondering how I can do this with ggplot2?

Thanks for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:56:15+00:00Added an answer on June 13, 2026 at 10:56 pm

    A simple ggplot2 version of your graph is possible by using geom_segment() and transforming the data much like you did for the base graphics version. I’ve also included an arguably more polished version, using some of the more advanced customization options in ggplot2.

    # Using ggplot2 version 0.9.2.1
    library(ggplot2)
    
    dat$x1_norm = rangeTransform(as.integer(dat$X1))
    dat$x2_norm = rangeTransform(as.integer(dat$X2))
    
    dat$y1 = 0
    dat$y2 = 1
    
    # Simple version.
    p1 = ggplot(dat, aes(x=x1_norm, xend=x2_norm, y=y1, yend=y2, colour=X1)) +
         geom_segment(size=1.2) +
         scale_colour_brewer(palette="Set1", name="Person")
    
    ggsave(plot=p1, filename="plot_1.png", height=3.5, width=6)
    

    enter image description here

    # Fancy version.
    # Create separate data.frames to manually specify axis ticks and axis text.
    axis_1 = data.frame(x=rangeTransform(as.integer(unique(dat$X1))),
                        y=0, label=as.character(unique(dat$X1)))
    
    axis_2 = data.frame(x=rangeTransform(as.integer(unique(dat$X2))),
                        y=1, label=as.character(unique(dat$X2)))
    
    p2 = ggplot(data=dat) +
         theme_bw() +
         theme(axis.title=element_blank()) +
         theme(axis.text=element_blank()) +
         theme(axis.ticks=element_blank()) +
         theme(panel.grid=element_blank()) +
         geom_segment(aes(x=x1_norm, xend=x2_norm, y=y1, yend=y2, colour=X1),
                      size=1.2) +
         geom_segment(x=0, xend=1, y=0, yend=0, size=0.7) +
         geom_segment(x=0, xend=1, y=1, yend=1, size=0.7) +
         scale_colour_brewer(palette="Set1", name="Person") +
         scale_y_continuous(limits=c(-0.2, 1.2), expand=c(0, 0)) +
         geom_segment(data=axis_1, aes(x=x, xend=x, y=y, yend=y-0.025), size=0.7) +
         geom_segment(data=axis_2, aes(x=x, xend=x, y=y, yend=y+0.025), size=0.7) +
         geom_text(data=axis_1, aes(label=label, x=x, y=y - 0.075)) +
         geom_text(data=axis_2, aes(label=label, x=x, y=y + 0.075))
    
    ggsave(plot=p2, filename="plot_2.png", height=3.5, width=6)
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a similar data frame as follows: mapDF <- structure(list(var = c(11L, 3L,
Say I have the following R data.frame ZZZ : ( ZZZ <- structure(list(n =
I have the following data frame: id,property1,property2,property3 1,1,0,0 2,1,1,0 3,0,0,1 4,1,1,1 d.f <- structure(list(id
I have the following data frame: structure(list(Species = 1:4, Ni = c(1, NA, 1,
I have the following (dummy) data: d <- structure(list(group = structure(c(1L, 1L, 1L, 1L,
I have following data ct<-structure(list(Conc = c(50L, 100L, 150L, 50L, 100L, 150L, 50L, 100L,
Suppose you have a data frame with the following structure: df <- data.frame(a=c(1,2,3,4), b=c(job1;job2,
I have written the following code: library(ggplot2) data <- structure(list(x = c(1L, 6L, 3L,
I have a data.frame : df <- structure(list(sample = structure(c(1L, 1L, 1L, 1L, 2L,
Suppose I have the following data frame: Data1 X1 X2 1 15 1 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.