Antonio D'angelo
Antonio D'angelo

Reputation: 19

Plot two curves and their confidence interval

I have two similar data.frame's and i can plot each one of them on ggplot2 with its confidence interval. my question is how i can plot both of them on the same graph? Here is the script which allows me to plot the first dataframe, the second is basically similar to this one, same scales and close values.

xlabel <- "Frequency [Hz]"
ylabel <- "|Z|[Ohm]"
plotdata <- data.frame(x=x6, y=mediaprobes, lower = (mediaprobes_m), upper = (mediaprobes_M))
library(ggplot2)
ggplot(plotdata) + geom_line(aes(y= y, x= x6, colour = "Mean probes"))+
  geom_ribbon(aes(ymin=lower, ymax=upper, x=x6, fill = "st.err"), alpha = 0.5)+
  scale_colour_manual("",values="blue")+
  scale_fill_manual("",values="grey12")+ 
  xlab(xlabel) + 
  ylab(ylabel)+
  scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x)))+
  scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x)))

second dataframe

plotdatas <- data.frame(x=x6, y=mediaprobes, lower = (mediaprobes_m), upper = (mediaprobes_M))

this is how the dataframes looks like:

      x           y      lower    upper
      1000000.0  2175.0  2127.042  2222.958
      932600.0   2290.0  2239.668  2340.332


 x           y2         lower2     upper2
 1000000.0  2454.593   2435.686  2473.500
 932600.0   2580.815   2561.698   2599.932

Upvotes: 0

Views: 261

Answers (1)

pogibas
pogibas

Reputation: 28339

Add group status to each table and then bind them using rbind. In ggplot specify color/fill by the group ("A" or "B") you have added.

# We have to change column names to match plotdata
colnames(plotdatas) <- c("x", "y", "lower", "upper")
plotdata$group <- "A"
plotdatas$group <- "B"
pd <- rbind(plotdata, plotdatas)

library(ggplot2)
ggplot(pd, aes(x, y, fill = group)) + 
    geom_line(aes(color = group)) +
    geom_ribbon(aes(ymin = lower, ymax = upper), 
                alpha = 0.5) +
    scale_colour_manual(values = c("blue", "black")) +
    scale_fill_manual(values = c("grey40", "grey1"))

Result:

enter image description here

Upvotes: 1

Related Questions