【问题标题】:How to combine two graphs in R studio using ggplot?如何使用 ggplot 在 R studio 中组合两个图形?
【发布时间】:2017-06-21 19:03:15
【问题描述】:

我正在尝试使用 ggplot 组合两个分布式图。但不成功。

【问题讨论】:

  • 试试facet_wrap( ~ variable, scales = "free_x")。还提供一个可重现的例子link
  • ggplot(X,aes(x=rating, fill = cond)) + geom_density(alpha = .3)。尝试此操作时出现错误,例如 Error: Mapping should be created with aes() 或 aes_()

标签: r ggplot2 rstudio


【解决方案1】:

如果您提供了一个可重现的示例,它会更容易,但除了重现发行版之外,下面的代码需要您想要的:

ggplot(NULL, aes(as.numeric(BIN))) + 
    geom_bar(aes(fill = "0.2"), data = bin1, alpha = 0.5) +
    geom_bar(aes(fill = "0.8"), data = bin2, alpha = 0.5)

这会给我们:

数据:

  #some fake data
  bin1<-rbinom(1000,100,.2)
  bin2<-rbinom(1000,100,.8)


  bin1 <- data.frame(cbind(bin1,"0.2"))
  bin2 <- data.frame(cbind(bin2,"0.8"))
  colnames(bin1)[1] <- "BIN"
  colnames(bin2)[1] <- "BIN"
  bin1[,1] <- as.numeric(bin1[,1])
  bin2[,1] <- as.numeric(bin2[,1])
  bin <- rbind(bin1,bin2)
  bin <-data.frame(bin)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 2022-07-07
    相关资源
    最近更新 更多