【问题标题】:Combine 2 plots (ggplot) into one plot and differenciate them将 2 个图(ggplot)合并为一个图并区分它们
【发布时间】:2015-06-04 12:50:05
【问题描述】:

我有一个类似于以下示例的问题。我想区分不同组的线条;例如,我想区分第 1 组中的“m”sexe cdf 和第 2 组中的“m”sexe 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

【问题讨论】:

  • 您的代码似乎无法重现:Error in structure(list(call = match.call(), aesthetics = aesthetics, : object 'percent' not found 请您将其剥离回一个最小的可重现示例。另外,请不要设置options(digits = 1),因为这会破坏人们的 R 环境。最后,显式调用您需要的任何包 (library("ggplot2"))。我的第一个想法是尝试as.factor() 换成sexe
  • 它对我来说很好,我不明白......我会在那个事件中改变它。
  • 尝试保存文件,清除 R 环境,然后从头开始运行整个代码。
  • 是的,你是对的,谢谢!只需删除 scale_y_continuous(limits=c(0,1), labels= percent) 选项。我将删除对数刻度选项,我认为没有它会更清晰。
  • 菲尔你知道我的问题吗?

标签: r ggplot2


【解决方案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

【讨论】:

    猜你喜欢
    • 2014-02-07
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    相关资源
    最近更新 更多