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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:06:44+00:00 2026-06-03T19:06:44+00:00

(this follows ggplot2 loess Q for which I got a nice answer) — leading

  • 0

(this follows ggplot2 loess Q for which I got a nice answer) — leading to this plot:

answer image to first question

My R knowledge is quite limited (sorry!)

I plot a scatter using data from a table data1.

data1<-NaRV.omit(data[,c(2,3,7,10)]) #(2=start, 3=end, 7=value, 10=type)
ylabs='E / A - ratio'
p1<-ggplot(data1, aes(x=start, y=value)) +
ylim(0,5) +
geom_point(shape=points, col=pointcol1, na.rm=T) +
geom_hline(aes(yintercept=1, col=linecol)) +
geom_smooth(method="loess", span=spanv, fullrange=F, se=T, na.rm=T) +
#
xlab(xlabs) +
ylab(ylabs)

Some regions have no-data (incl one big region in the middle but also smaller discrete regions) where I would like to draw a colored segments at y=0 to illustrate this fact

I combined both data types into one table with a label column#10=’type’ (content for the scatter data =’cnv’ and for the no-data=’nregion’). nregions have 0 in the value column.

How can I take only ‘cnv’ data for the scatter and only ‘nregion’ data to draw the segments; both on the same plot?

I found geom_segment:

+ geom_segment(aes(x=data1$start, y=0, xend=data1$end, yend=0))

BUT I did not find a way to subset for each ggplot sub-plot.

Thanks

#### follow up on @gauden solution

Hi @gauden
I tried your approach and it partly worked.
My problem is that I cannot divide my data as nicely as you do using ]-1; 0] because my nregions are scattered (represented by the blue dots and lines in the picture) and are different for each new graph, as in this image:

target image with multiple segments

Consequently, the loess goes through the large nregion as before. How can I prevent loess in nregions?

#############################
## plot settings (edit below)
spanv<-0.1
pointcol1="#E69F00"
pointcol2="#56B4E9"
pointcol3="#009E73"
points=20
onecol="green"
colnreg="blue"
xlabs=paste(onechr, " position", " (loess-span=", spanv, ")", sep="")

##### end edit ##############

########################################################
## using the center coordinate of each segment and points

## prepare plot #1
# plot E / A - ratio
## draw loess average for cnv
## draw line for nregion
ylabs='E / A - ratio'
p1<-ggplot(chrdata, aes(x=start+1000, y=E.R, group=type, label=type)) +
ylim(0,5) +
geom_hline(aes(yintercept=1, col=onecol)) +
geom_point(data = chrdata[chrdata$type != 'nregion',], shape=points, col=pointcol2) +
geom_smooth(data = chrdata[chrdata$type != 'nregion',], method="loess", span=spanv) +
geom_point(data = chrdata[chrdata$type == 'nregion',], col=colnreg) +
geom_segment(data = chrdata[chrdata$type == 'nregion',], aes(x=start, y=E.R, xend=end, yend=E.R), colour=colnreg, linetype=1, size=1) +
xlab(xlabs) +
ylab(ylabs)
  • 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-03T19:06:46+00:00Added an answer on June 3, 2026 at 7:06 pm

    EDIT: Complete revision to allow for clarified request

    Here is my target plot:
    multiple-segment scatter plot

    And here is the code that produces it:

    library("ggplot2")
    
    # CREATE DATA FRAME
    # This is the sort of data that I understand you to have
    start <- rnorm(200)
    value <- rnorm(200) 
    df <- data.frame( cbind(start, value) )
    df[ df$start > -0.6 & df$start <= 0, "value"] <- 0
    df[ df$start > -1.6 & df$start <= -1.3, "value"] <- 0
    df[ df$start > 0.9 & df$start <= 1.2, "value"] <- 0
    
    df$type <- rep('cnv', 200)
    df[ df$value == 0, "type"] <- 'nregion'
    df[ df$value != 0, "type"] <- 'cnv'
    
    # SORT the data frame by value so that the 'cnv' and 
    # 'nregion' chunks become contiguous
    df <- df[order(start),]
    
    # See note below. 
    r <- rle(df$type)
    df$label <- rep(seq(from=0, length=length(r$lengths)), times=r$lengths)
    
    # set up plot with colour aesthetic to distinguish the three regions
    # playing around with colour and group produces different effects
    p <- ggplot(df, aes(x = start, 
                        y= value,
                        colour=type,
                        group = label)
                )
    p <- p + theme_bw()
    # draw points outside the 'nregion'
    p <- p + geom_point( data = df[df$type != 'nregion',] )
    
    # draw smoothed lines outside the 'nregion'
    p <- p + geom_smooth( data = df[df$type != 'nregion',] )
    
    
    # plot zero points inside the 'nregion' 
    p <- p + geom_smooth( data = df[df$type == 'nregion',], size = 2 )
    p
    

    The use of rle is further explained in an answer to a supplementary question

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

Sidebar

Related Questions

This follows this question which was solved using this answer . Long story short:
This question follows on from a previous question which can be found here: Need
This follows this prior question, which was answered. I actually discovered I could remove
This question follows this other question about C declarations . Reading the answer to
This question follows on from a previous question, that has raised a further issue.
This problem follows on from a previous question . When I run the following
This question follows on from a previous question. However stackoverflow presents me from commenting
I am creating a DynamicMultiMock as follows: this.serviceClient = this.mocks.DynamicMultiMock<ISlippyPlateProcedureService>(typeof(ICommunicationObject)); Then setting the following
I am working with an extension of the DefaultTableModel as follows: This is the
I'm using this plugin: http://www.jeremymartin.name/projects.php?project=kwicks And my code follows this example: http://www.jeremymartin.name/examples/kwicks.php?example=7 I'm using

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.