0

I'm plotting 11 curves and the program bellow works well. BUT I'm not able two change the wild colors to plot 11 black curves:

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

EDIT Now the code should be reproducible

PeptideChain
  • 543
  • 3
  • 16

1 Answers1

1

You need to map the variable to the grouping, and it will produce black lines by default.

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

To be perfectly clear, it is possible to map the color to the variable, which can produce black lines, but not without changing the legend. Here is how you would amend the colors after the fact, if you wanted to, having already mapped the color to the variable.

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
shayaa
  • 2,787
  • 13
  • 19
  • thank you very much. The first code block is exactly what I was looking for. Note: The second code block does not seem to work, if I put ```"red"```, the lines remain black. – PeptideChain Jul 24 '16 at 06:28
  • Edited to work. The layering causes problems in this case, I believe. – shayaa Jul 24 '16 at 07:08