📜  R中的ggplot2多密度图和变量着色

📅  最后修改于: 2022-05-13 01:54:49.372000             🧑  作者: Mango

R中的ggplot2多密度图和变量着色

在本文中,我们将讨论如何在 R 编程语言中使用变量着色制作多个密度图。

为了使用 ggplot2 在 R 中通过变量着色制作多个密度图,我们首先制作一个带有值和类别的数据框。然后我们使用 geom_desnity()函数绘制 ggplot2 密度图。为了根据变量给它们上色,我们在 ggplot()函数中添加了 fill 属性作为一个类别。

按变量着色的多密度图

我们在 ggplot 中得到多个密度图,对应于第二个分类变量的两个级别/值。如果我们的分类变量有 n 个级别,那么 ggplot2 将制作具有 n 个密度/颜色的多个密度图。

R
# set seed
set.seed(1234)
  
# create dataframe
df <- data.frame( category=factor(rep(c("category1", 
                                        "category2"), 
                                      each=500)),
                    value=round(c(rnorm(500, mean=95, sd=5),
                                rnorm(500, mean=105, sd=7))))
# load library ggplot2 package
library(ggplot2)
  
# Basic density plot with custom color
ggplot(df, aes(x=value, color=category)) + 
  
  # color property for changing color of plot
  # geom_density() function plots the density plot
  geom_density()


R
set.seed(1234)
df < - data.frame(
    category=factor(rep(c("category1", "category2"), each=500)),
    value=round(c(rnorm(500, mean=95, sd=5),
                  rnorm(500, mean=105, sd=7)))
)
# load library
library(ggplot2)
  
# Basic density plot with custom color
# color property to determine the color of plot
# fill property to determine the color beneath plot
ggplot(df, aes(x=value, color=category, fill=category)) +
geom_density(alpha=0.3)


R
# set seed
set.seed(1234)
  
# create dataframe
df < - data.frame(
    category=factor(rep(c("category1", "category2"),
                        each=5000)),
    value=round(c(rnorm(5000, mean=90005, sd=50000),
                  rnorm(5000, mean=70005, sd=70000))))
  
# load library ggplot2
library(ggplot2)
  
# Basic density plot
# color property is used to determine the color of plot
ggplot(df, aes(x=value, color=category)) +
geom_density()+  # geom_density() creates density plot
scale_x_log10()  # converts x-axis values in log scale


输出:

颜色定制

要在密度图下方添加颜色,我们使用 fill 属性。我们还可以使用 alpha 属性自定义绘图的透明度。我们在 ggplot 中得到多个密度图,其中填充了两种颜色,对应于第二个分类变量的两个级别/值。如果我们的分类变量有 n 个级别,那么 ggplot2 将制作具有 n 个密度/颜色的多个密度图。 alpha 值 0.3 为绘图填充提供 30% 的透明度。

电阻

set.seed(1234)
df < - data.frame(
    category=factor(rep(c("category1", "category2"), each=500)),
    value=round(c(rnorm(500, mean=95, sd=5),
                  rnorm(500, mean=105, sd=7)))
)
# load library
library(ggplot2)
  
# Basic density plot with custom color
# color property to determine the color of plot
# fill property to determine the color beneath plot
ggplot(df, aes(x=value, color=category, fill=category)) +
geom_density(alpha=0.3)

输出:

对数刻度定制

当 x 或 y 轴数据非常大时,我们可以用对数刻度绘制绘图。在 ggplot2 中,我们可以使用 scale_x_log10()函数将 x 轴值转换为对数刻度。

现在我们的密度图是以对数比例绘制的。当我们的数据集倾斜时,这变得更加有效。它有助于纠正情节的偏斜。

电阻

# set seed
set.seed(1234)
  
# create dataframe
df < - data.frame(
    category=factor(rep(c("category1", "category2"),
                        each=5000)),
    value=round(c(rnorm(5000, mean=90005, sd=50000),
                  rnorm(5000, mean=70005, sd=70000))))
  
# load library ggplot2
library(ggplot2)
  
# Basic density plot
# color property is used to determine the color of plot
ggplot(df, aes(x=value, color=category)) +
geom_density()+  # geom_density() creates density plot
scale_x_log10()  # converts x-axis values in log scale

输出: