qplot(ggplot2):具有相同颜色的更多函数的图

时间:2016-07-24 03:47:33

标签: r ggplot2

我正在绘制11条曲线并且程序波纹管效果很好。但是我不能两个改变野性颜色来绘制11 黑色曲线

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

编辑现在代码应该可以重现

1 个答案:

答案 0 :(得分: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