S12000
S12000

Reputation: 3396

Force ggplot to show 2 curve function

I have two function y1 and y2.

If I plot them individually no problem (cf. two first figures).

However, if I combine them, the shape looks linear (cf. figure 3) .

How can I solve that?

x  <- seq(0, 50, 1) ;x
y1 <- exp(8.9191)+exp^(0.03307*x)
y2 <- exp(9.9191)+exp^(0.06307*x)
df <- data.frame(x,y1,y2)

require(ggplot2)

ggplot(df, aes(x)) +                  
  geom_line(aes(y=y2), colour="red")  #Looks nice (curvy)

ggplot(df, aes(x)) +                  
  geom_line(aes(y=y1), colour="blue") #Looks nice (curvy)

ggplot(df, aes(x)) +                    
  geom_line(aes(y=y1), colour="blue") +  
  geom_line(aes(y=y2), colour="red")  #Looks not nice linear

enter image description here

enter image description here

enter image description here

Upvotes: 1

Views: 108

Answers (1)

marbel
marbel

Reputation: 7714

Perhaps you could use facet_grid() and the scales option. You will have two facets, each with a different y scale.

require(reshape2)
mdf <- melt(data.frame(y1, y2))
mdf$x <- x

ggplot(mdf, aes(x = x)) +
  geom_line(aes(y = value)) +
  facet_grid(variable ~ ., scales = "free_y")

enter image description here

Upvotes: 3

Related Questions