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 6200765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:25:41+00:00 2026-05-24T04:25:41+00:00

I’m new to ggplot2 and would like to plot lines in an existing plot

  • 0

I’m new to ggplot2 and would like to plot lines in an existing plot dynamicly, which doesn’t work. It just take the last count while plotting the whole picture.

I’ve 2 matrixes:
“clVrd” with

            [,1]        [,2]
 [1,]  0.6618725 -0.04065907
 [2,]  0.4646620  0.09859806
 [3,]  0.9388307  0.05681554
 [4,]  1.1809942  0.12906415
 [5,]  1.5476428  0.49644973
 [6,] -0.1855485  0.30445869
 [7,]  0.4525888  0.49559198
 [8,] -0.4004534 -0.06419374
 [9,] -1.0669191  0.17292748
[10,] -0.9372038  0.02601539
[11,]  0.5617849 -5.21857716
[12,] -0.9370099 -0.05539107
[13,]  0.6803453  0.21223368
[14,]  1.3040601  0.47598799

and “mid” with

            [,1]       [,2]
 [1,] -0.1958772  0.3012428
 [2,]  0.5115807  0.4142237
 [3,] -0.6585965  0.2623573
 [4,]  0.4680863 -1.4964873
 [5,] -1.2431780  0.2383014
 [6,] -2.3507773  0.0954886
 [7,] -0.5547284 -2.1393520
 [8,]  0.1314092  0.3408999
 [9,]  0.7592055 -0.8161825
[10,]  0.8247861  0.5152814
[11,] -1.8667328  0.1344475
[12,] -0.4825223 -4.0975561

and a matrix “active” that tells the loop if to draw a line (1) or not (0)

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
 [1,]    1    1    1    1    1    1    1    1    1     1     0     1     1
 [2,]    1    1    1    1    1    1    1    1    1     1     0     1     1
 [3,]    1    1    1    1    1    1    1    1    1     1     0     1     1
 [4,]    1    1    1    1    1    1    1    1    1     1     1     1     1
 [5,]    1    1    1    1    0    1    1    1    1     1     0     1     1
 [6,]    0    0    0    0    0    0    0    0    1     1     0     1     0
 [7,]    1    1    1    1    1    1    1    1    1     1     1     1     1
 [8,]    1    1    1    1    1    1    1    1    1     1     0     1     1
 [9,]    1    1    1    1    1    1    1    1    1     1     1     1     1
[10,]    1    1    1    1    1    1    1    1    1     1     0     1     1
[11,]    0    0    0    0    0    1    0    1    1     1     0     1     0
[12,]    1    1    1    1    1    1    1    1    1     1     1     1     1

This call produces the main plot

g <- ggplot() + layer(data=data.frame(clUrd), mapping=aes(x=clUrd[,1], y=clUrd[,2]), geom = "point", stat="identity", size = I(1), alpha = I(0.2))

which works well. Now i want to draw a line from each entry y (row) to each x (col) in the corresponding matrices clVrd and mid if in active is a 1.

I tried the following loop:

for (i in 1:13){ # Draw the lines between middlepoints and infrastructures
    for (j in 1:maxcl){
        if (active[j,i]==1){
          g <- g + geom_segment(aes(x=clVrd[i,1], y=clVrd[i,2], xend=mid[j,1], yend=mid[j,2]), color='grey')
        }
    }
}

which doesn’t work. It just draw the lines for i=13 and j=12. If I look into g with head(g) I can see that he adds the lables with

$layers[[132]]
mapping: x = clVrd[i, 1], y = clVrd[i, 2], xend = mid[j, 1], yend = mid[j, 2] 
geom_segment: colour = grey 
stat_identity:  
position_identity: (width = NULL, height = NULL)

which explains why he just draw the lines for i=13 and j=12. But how I can fix that? And shouldn’t there be 157 layers? Here there are only 132 or do I understand something wrong?

Thanks for your help.

Dominik

  • 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-05-24T04:25:42+00:00Added an answer on May 24, 2026 at 4:25 am

    After I eat dinner and rest a bit, I found the answer. I don’t know if it is the best solution, but it works for me.

    We generate a matrix “lines”

    lines <- numeric()
    

    and put the coordinates for the lines from the different matrices in our new one if active[x,y] is 1 (see above).

    for (i in 1:13){
        for (j in 1:maxcl){
            if (active[j,i]==1){
              lines <- rbind(lines, c(clVrd[i,1], clVrd[i,2], mid[j,1], mid[j,2]))
            }
        }
    }
    

    Now we can plot the plot with lines with

    g <- ggplot() + layer(data=data.frame(clUrd), mapping=aes(x=clUrd[,1], y=clUrd[,2], col=factor(clUrd[,3])), geom = "point", stat="identity", size = I(1), alpha = I(0.2))
    

    But I still have to check if everything is right, because it seems to be a bit of, but it should

    The whole picture is now generated with the following call:

    g <- ggplot() + layer(data=data.frame(clUrd), mapping=aes(x=clUrd[,1], y=clUrd[,2], col=factor(clUrd[,3])), geom = "point", stat="identity", size = I(1.5), alpha = I(0.2)) +
      scale_colour_brewer(palette="Paired") +
      geom_segment(data=data.frame(test), aes(x=lines[,1], y=lines[,2], xend=lines[,3], yend=lines[,4]), color='grey', alpha = I(0.2)) +
      layer(data=data.frame(clVrd), mapping=aes(x=clVrd[,1], y=clVrd[,2]), geom = "point", stat="identity", size = I(4), color='black', shape=2) +
      layer(data=data.frame(mid), mapping=aes(x=mid[,1], y=mid[,2]), geom = "point", stat="identity", size = I(4), color='black', shape=22) +
      opts(legend.position = "none") +
      scale_x_continuous('Dimension 1') +
      scale_y_continuous('Dimension 2') +
      opts(axis.title.y = theme_text(vjust=0.2, angle=90)) +
      opts(axis.title.x = theme_text(vjust=0.2, angle=0))
    

    with clUrd is a 3×5982 matrix like

    [1,] -0.1454078  0.26175441    1
    [2,]  0.4567384  0.46036161    2
    [3,] -0.9269251  0.39196787    3
    [4,] -0.8959093  0.06884224    3
    [5,]  0.1618442 -1.12006536    4
    [6,] -1.4021955  0.34594349    5
    

    The result is the following picture! Where the dots are respondents, the triangle the infrastructures, the squares the midpoints of the clusters and the color the different clusters.

    • Sorry can’t post a picture, I’m not allowed, because I have not enough points… That’s stupid.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

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.