0

We are working on some ggplot2 graphs. Each graph contains multiple lines. However, when our line number exceeds more than 3 lines, there are only 3 lines showing in the graph, and the rest lines are gone. The weird thing is that the legends are all there.

enter image description here

Here is the snapshot of the code:

     tailStretchPlot[[i]] <- ggplot() +
        geom_step(data=tmp, aes(x=SizeCumPercent-SizeCntPercent/2,
        y=TailStretch, width=SizeCntPercent, colour=TransportType), size=4)

    plotTitle <- paste(append(unlist(strsplit(plotTitle, split='')), '\n',
        as.integer(nchar(plotTitle)/2)), sep='', collapse='')

    xIntervals <- findInterval(c(0)+seq(2,102,10),
        tmp[tmp$TransportType=='homa',]$SizeCumPercent)

    tailStretchPlot[[i]] <- tailStretchPlot[[i]] +
        theme(text = element_text(size=1.5*textSize),
            axis.text = element_text(size=1.3*textSize),
            axis.text.x = element_text(angle=75, vjust=0.5),
            strip.text = element_text(size = textSize),
            plot.title = element_text(size = 1.2*titleSize,
            color='darkblue'), plot.margin=unit(c(2,2,2.5,2.2),"cm"),
            legend.position = c(0.1, 0.85),
            legend.text = element_text(size=1.5*textSize)) +
        guides(colour = guide_legend(override.aes = list(size=textSize/4)))+
        scale_x_continuous(limits = c(0,101),
            breaks = tmp[xIntervals,]$SizeCumPercent,
            labels=tmp[xIntervals,]$MsgSizeRange, expand = c(0, 0)) +
        scale_y_log10(breaks=c(1,2,3,4,5,10,15)) +
        coord_cartesian(ylim=c(1, yLimit)) +
        labs(title = plotTitle, y = yLab, x = xLab)

The GitHub link is the complete script and sample data txt file:

https://github.com/Stanley131/ggplot2_issue

Thanks in advance:)

  • 3
    please make your question reproducible by including a sample of your data. Also your image hasn't worked – morgan121 Feb 27 '20 at 23:41
  • 4
    Does it work correctly if you make the plot outside of a loop? It's going to be easier for folks to help if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – aosmith Feb 27 '20 at 23:41
  • 7
    Be careful setting limits on `scale_x_continuous()`, any data outside those limits will get dropped. – Marius Feb 27 '20 at 23:52
  • Hey, guys. Sorry for providing enough information for the question. I just add the complete script link and sample dataset to my GitHub. https://github.com/Stanley131/ggplot2_issue. Thanks again. – Stanley Ye Mar 01 '20 at 03:44

0 Answers0