r - Multiple curves in ggplot2 with same independent variable -



r - Multiple curves in ggplot2 with same independent variable -

i have sequence of points in x-axis each of there 2 points in y-axis.

x<-seq(8.5,10,by=0.1) y<-c(0.9990276914, 0.9973015358, 0.9931704801, 0.9842176288, 0.9666471511, 0.9354201700, 0.8851624615, 0.8119131899, 0.7152339504, 0.5996777045, 0.4745986612, 0.3519940258, 0.2431610835, 0.1556738744, 0.0919857178, 0.0500000000, 0.0249347645, 0.0113838852, 0.0047497169, 0.0018085048, 0.0006276833) y1<-c(9.999998e-01,9.999980e-01,9.999847e-01,9.999011e-01,9.994707e-01,9.976528e-01,9.913453e-01, 9.733730e-01, 9.313130e-01, 8.504646e-01, 7.228116e-01, 5.572501e-01,3.808638e-01,2.264990e-01, 1.155286e-01, 5.000000e-02, 1.821625e-02, 5.554031e-03, 1.410980e-03, 2.976926e-04, 5.203069e-05)

i create 2 curves in ggplot2. quite easy accomplish in normal way in r. result in plot below. not sure, however, how in ggplot2. 1 curve, can use

library(ggplot2) p<-qplot(x,y,geom="line")

could please help me generalise above? help appreciated, give thanks you.

as @roland pointed out first should prepare length of x. possible solution using reshape2 package:

library(reshape2) library(ggplot2) x<-seq(8.5,10,length.out = 21) y<-c(0.9990276914, 0.9973015358, 0.9931704801, 0.9842176288, 0.9666471511, 0.9354201700, 0.8851624615, 0.8119131899, 0.7152339504, 0.5996777045, 0.4745986612, 0.3519940258, 0.2431610835, 0.1556738744, 0.0919857178, 0.0500000000, 0.0249347645, 0.0113838852, 0.0047497169, 0.0018085048, 0.0006276833) y1<-c(9.999998e-01,9.999980e-01,9.999847e-01,9.999011e-01,9.994707e-01,9.976528e-01,9.913453e-01, 9.733730e-01, 9.313130e-01, 8.504646e-01, 7.228116e-01, 5.572501e-01,3.808638e-01,2.264990e-01, 1.155286e-01, 5.000000e-02, 1.821625e-02, 5.554031e-03, 1.410980e-03, 2.976926e-04, 5.203069e-05) df <- data.frame(x, y, y1) df <- melt(df, id.var='x') ggplot(df, aes(x = x, y = value, color = variable))+geom_line()

edit: changing linetype , legend:

g <- ggplot(df, aes(x = x, y = value, color = variable, linetype=variable)) + geom_line() g <- g + scale_linetype_discrete(name="custom legend name", labels=c("curve1", "curve2")) g <- g + guides(color=false) print(g)

r ggplot2

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -