ggplot2中的重叠行 [英] Overlapping Lines in ggplot2

查看:120
本文介绍了ggplot2中的重叠行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为两个模型拟合了500条曲线。我根据模型绘制每个拟合以检查相似之处。麻烦在于一套线将覆盖另一套。在下面的例子中,物流线完全由样条线覆盖。

I fit 500 curves for two models. I plot each of the fits based on model to check for similarities. The trouble is one set of lines will overlay the other. In the example below the logistic lines are completely covered by the spline lines.

有没有一种方法可以绘制两种拟合并防止重叠,因此我仍然可以看到两组线条?也许通过改变颜色或使用不同的几何?

Is there a way I can plot both fits and prevent the overlapping so I can still see both sets of lines? Perhaps by changing colors or by using a different geom?

ggplot(data=fullData,aes(X,Y,color=Model,group=id))+geom_line()

> dput(head(fullData))
structure(list(X = c(-6, -5.97595190380762, -5.95190380761523, 
-5.92785571142285, -5.90380761523046, -5.87975951903808), Model = c("Logistic", 
"Logistic", "Logistic", "Logistic", "Logistic", "Logistic"), 
    Y = c(40.2327812336246, 40.2062250618146, 40.1837765087578, 
    40.1613100197852, 40.1387156930829, 40.1159930605682), id = c(1L, 
    1L, 1L, 1L, 1L, 1L)), .Names = c("X", "Model", "Y", "id"), row.names = c(NA, 
6L), class = "data.frame")


推荐答案

很高兴为您制定了阿尔法。如果您希望能够接受+关闭请求,您可以点击添加的alpha示例(包含一些模拟数据):

Glad the alpha worked out for you. If you'd like to be able to accept + close the request you can just click on this example of added alpha (with some simulated data):

library(ggplot2)

#function to generate some data
makeLine <- function(x){
  set.seed(x)
  Y <- c(runif(1,38,42),runif(1,34,38),runif(1,28,32),runif(1,23,27),runif(1,10,20))
  X <- c(-6,-3,0,3,6)
  if(x > 40){
    model <- "Spline"
  } else {
    model <- "Logistic"
  }
  data.frame(X=X,Y=Y,id=x,model=model)
}

#make a data set
dat <- do.call(rbind,lapply(1:100,makeLine))

#add alpha to your plot
ggplot(data=dat,aes(X,Y,color=model,group=id)) + geom_line(alpha=0.15)

这篇关于ggplot2中的重叠行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆