在ggplot2中生成包含函数的多个图

时间:2018-06-27 14:33:13

标签: r plot ggplot2

我正在尝试使用ggplot2和ggpubr软件包在R中制作合成图。 除了每个图都有特定于该数据集的正态分布曲线外,制作合成图没有任何问题。当我生成合成图时,两个图具有与最后一个数据集相同的曲线。

如何生成每个具有自己特定的正态分布曲线的合成图?

代码和输出图

## PLOT 1 ##

results_matrix_C <- data.frame(matrix(rnorm(20), nrow=20))
colnames(results_matrix_C) <- c("X")

m <- mean(results_matrix_C$X)
sd <- sd(results_matrix_C$X)
dnorm_C <- function(x){
  norm_C <- dnorm(x, m, sd)
  return(norm_C)
}

e = 1
dnorm_one_sd_C <- function(x){
  norm_one_sd_C <- dnorm(x, m, sd)
  # Have NA values outside interval x in [e]:
  norm_one_sd_C[x <= e] <- NA
  return(norm_one_sd_C)
}


C <- ggplot(results_matrix_C, aes(x = results_matrix_C$X)) +
  geom_histogram(aes(y=..density..), bins = 10, colour = "black", fill = "white") +
  stat_function(fun = dnorm_one_sd_C, geom = "area", fill = "#CE9A05", color = "#CE9A05", alpha = 0.25, size = 1) +
  stat_function(fun = dnorm_C, colour = "#CE0539", size = 1) +
  theme_classic()

enter image description here

## PLOT 2 ##

results_matrix_U <- data.frame(matrix(rnorm(20)+1, nrow=20))
colnames(results_matrix_U) <- c("X")

m <- mean(results_matrix_U$X)
sd <- sd(results_matrix_U$X)
dnorm_U <- function(x){
  norm_U <- dnorm(x, m, sd)
  return(norm_U)
}

e = 2
dnorm_one_sd_U <- function(x){
  norm_one_sd_U <- dnorm(x, m, sd)
  # Have NA values outside interval x in [e]:
  norm_one_sd_U[x <= e] <- NA
  return(norm_one_sd_U)
}


U <- ggplot(results_matrix_U, aes(x = results_matrix_U$X)) +
  geom_histogram(aes(y=..density..), bins = 10, colour = "black", fill = "white") +
  stat_function(fun = dnorm_one_sd_U, geom = "area", fill = "#CE9A05", color = "#CE9A05", alpha = 0.25, size = 1) +
  stat_function(fun = dnorm_U, colour = "#CE0539", size = 1) +
  theme_classic()

enter image description here

library(ggpubr)

ggarrange(C, U,
          nrow = 1, ncol = 2)

enter image description here

您可以在合成图中看到,第一个曲线是第二个曲线的正态分布曲线,而不是我最初的曲线(曲线1)中的正态分布曲线。

更新

变量“ e”是指与分布曲线有关的阴影区域。 m =数据集的平均值 sd =数据集的标准偏差 m和sd用于生成正态分布曲线

1 个答案:

答案 0 :(得分:1)

已解决

通过将函数完全插入到ggplot2代码的stat_function部分中,就可以了

即:

## PLOT 1 ##

results_matrix_C <- data.frame(matrix(rnorm(20), nrow=20))
colnames(results_matrix_C) <- c("X")

mean <- mean(results_matrix_C$X)
sd <- sd(results_matrix_C$X)
e = 1


C <- ggplot(results_matrix_C, aes(x = results_matrix_C$X)) +
  geom_histogram(aes(y=..density..), bins = 10, colour = "black", fill = "white") +
  stat_function( 
    fun = function(x, mean, sd, e){ 
      norm_one_sd_C <- dnorm(x, mean, sd)
      norm_one_sd_C[x <= e] <- NA
  return(norm_one_sd_C)}, 
    args = c(mean = mean, sd = sd, e = e), geom = "area", fill = "#CE9A05", color = "#CE9A05", alpha = 0.25, size = 1) +
  stat_function( 
    fun = function(x, mean, sd){ 
      dnorm(x = x, mean = mean, sd = sd)}, 
    args = c(mean = mean, sd = sd), colour = "#CE0539", size = 1) +
  theme_classic()

enter image description here

## PLOT 2 ##

results_matrix_U <- data.frame(matrix(rnorm(20)+1, nrow=20))
colnames(results_matrix_U) <- c("X")

mean <- mean(results_matrix_U$X)
sd <- sd(results_matrix_U$X)
e = 2


U <- ggplot(results_matrix_U, aes(x = results_matrix_U$X)) +
  geom_histogram(aes(y=..density..), bins = 10, colour = "black", fill = "white") +
  stat_function( 
    fun = function(x, mean, sd, e){ 
      norm_one_sd_U <- dnorm(x, mean, sd)
      norm_one_sd_U[x <= e] <- NA
  return(norm_one_sd_U)}, 
    args = c(mean = mean, sd = sd, e = e), geom = "area", fill = "#CE9A05", color = "#CE9A05", alpha = 0.25, size = 1) +
  stat_function( 
    fun = function(x, mean, sd){ 
      dnorm(x = x, mean = mean, sd = sd)}, 
    args = c(mean = mean, sd = sd), colour = "#CE0539", size = 1) +
  theme_classic()

enter image description here

library(ggpubr)

ggarrange(C, U,
          nrow = 1, ncol = 2)

enter image description here