Mostafa90
Mostafa90

Reputation: 1706

Combine 2 plots (ggplot) into one plot and differenciate them

I have a problem similar to the following example. I want to differentiate the lines from different group; for example, I want distinguish the "m" sexe cdf from group 1 and the "m" sexe cdf from group 2.

library(ggplot2)
sexe <- rep(c("m", "w", "x"), 50)
weight1 <- runif(150, 30, 90)
weight2 <- runif(150, 30, 90)
visual1 = data.frame(sexe = sexe, weight = weight1)
visual2 = data.frame(sexe = sexe, weight = weight2)
visual1$group <- 1
visual2$group <- 2

visual12 <- rbind(visual1, visual2)


p <- ggplot(dat = visual12, aes(x = as.numeric(weight), group = interaction(group, sexe), col = sexe)) + 
  #   geom_point(dat = dat2, aes(x = as.numeric(dura), col = TYPE_DE_TERMINAL)) +
  stat_ecdf(geom = "step") +
  #   scale_colour_discrete(guide = guide_legend(override.aes = list(alpha = 1))) +
  scale_colour_brewer(name = "sexe", palette = "Set1") +
  theme(axis.text = element_text(size = 15), legend.justification = c(1, 0),
        legend.position = c(1, 0), axis.text.x = element_text(angle = 45, vjust = 1, hjust=1)) + 
  ylab("CDF") + xlab("...") + theme_bw() +
  #    scale_y_continuous(limits=c(0,1), labels= percent) +
  ggtitle("Cumulative distribution function of ...") 
  #    scale_x_log10(limits = c(1,1e3), breaks = c(10 , 100)) 

p

Upvotes: 2

Views: 242

Answers (1)

cory
cory

Reputation: 6659

What if you change the linetype by group?

p <- ggplot(dat = visual12, aes(x = as.numeric(weight), 
            group = interaction(group, sexe), 
            linetype=factor(group), col = sexe)) +       
     stat_ecdf(geom = "step") +      
     scale_colour_brewer(name = "sexe", palette = "Set1") +
     theme(axis.text = element_text(size = 15), 
           legend.justification = c(1, 0),
           legend.position = c(1, 0), 
           axis.text.x = element_text(angle = 45, vjust = 1, hjust=1)) + 
     ylab("CDF") + xlab("...") + theme_bw() +      
     ggtitle("Cumulative distribution function of ...")     

p

enter image description here

Upvotes: 1

Related Questions