2016-07-24 50 views
0

我绘制了11条曲线,程序波纹管运行良好。但我不能够在两个变化百搭的颜色绘制11条黑色曲线qplot(ggplot2):具有相同颜色的更多功能的绘图

library(ggplot2) 
#library(latex2exp) 
library(reshape) 
fn <- "img/plot.eps" 
fct1 <- function(x0){ 
    return(1/sin(x0)+1/tan(x0)) 
} 
fct2 <- function(beta, t){ 
    return(2*atan(exp(t)/beta)) 
} 
t<-seq(from=0,to=10,by=0.01) 
s1<-cbind(t, fct2(fct1(-pi+0.0001),t), 
      fct2(fct1(-1.5),t), 
      fct2(fct1(-0.5),t), 
      fct2(fct1(-0.05),t), 
      fct2(fct1(-0.01),t), 
      fct2(fct1(0),t), 
      fct2(fct1(0.01),t), 
      fct2(fct1(0.05),t), 
      fct2(fct1(0.5),t), 
      fct2(fct1(1.5),t), 
      fct2(fct1(pi),t)) 
colnames(s1)<-c("time","y1","y2","y3","y4","y5","y6","y7","y8","y9","y10","y11") 
s2 <- melt(as.data.frame(s1), id = "time") 
q <- ggplot(s2, aes(x = time, y = value, color = variable)) 
q <- q + geom_line() + ylab("y") + xlab("t")+ ylab("x(t)")+ 
    theme_bw(base_size = 7) + guides(colour = FALSE) 
ggsave(file = fn, width = 2, height = 1) 
q 

编辑现在

+0

[使这个问题可重现...](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – shayaa

+0

@shayaa现在代码应该是可重复的 – LiPo

回答

1

您需要的变量分组映射代码应该是重复性好,它会默认产生黑线。

q <- ggplot() + 
    geom_line(data = s2, aes(x = time, y = value, 
        group = variable)) + 
       xlab("t")+ ylab("x(t)") + 
    theme_bw(base_size = 7) + guides(colour = FALSE) 
q 

要十分清楚,可以对颜色的变量,其可产生黑色的行映射,但并非没有改变图例。以下是如何在事实之后修改颜色(如果需要),如果已将颜色映射到变量。

q <- ggplot() + 
    geom_line(data = s2, aes(x = time, y = value, 
          color = variable)) + 
    xlab("t")+ ylab("x(t)") + 
    theme_bw(base_size = 7) + guides(colour = FALSE) + 
    scale_color_manual(values = rep("black",11)) 
q 
+0

非常感谢你。第一个代码块就是我正在寻找的。注:第二个代码块似乎不工作,如果我把''“红色”''',线条保持黑色。 – LiPo

+0

编辑工作。我相信在这种情况下分层会导致问题。 – shayaa

+0

完美,谢谢 – LiPo

相关问题