将2个图(ggplot)组合成一个图并区分它们

时间:2015-06-04 12:50:05

标签: r ggplot2

我遇到类似以下示例的问题。我想区分不同组的线;例如,我想区分第1组的“m”性别cdf和第2组的“m”性别cdf。

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

1 个答案:

答案 0 :(得分:1)

如果按组更改linetype该怎么办?

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