如何使用管道绘制密度图?
How to do a density plot using pipes?
我正在尝试使用 R 中可用的 weatherAUS 数据集绘制密度图。我尝试使用以下代码。但是,我无法绘制出降雨分布的完美密度图。
使用下面的代码我的情节看起来像
用于数据集使用 -> View(weatherAUS) # 这是 R 内置数据集。
那么,我怎样才能完美地绘制密度图呢?我希望我的情节如下所示:
cities <- c("Canberra", "Darwin", "Melbourne", "Sydney")
# Build the required dataset and plot it.
weatherAUS %>%
filter(Location %in% cities) %>%
filter(Rainfall %>% is.na() %>% not()) %>%
ggplot(aes(x=Rainfall, colour=Location, fill=Location)) +
geom_density(alpha=0.55) +
labs(title="Distribution of Rainfall",
subtitle="Selective Locations",
caption="Source: Australian Bureau of Meteorology",
x="Rainfall",
y="Density")
当你说“完美”时,我不确定你在追求什么,但这可能会更好地传达信息:对 x 轴进行对数变换以避免出现长尾。但是,请注意,您最终会从 0 毫米降水量的 log10 中得到数千个无限值。
cities <- c("Canberra", "Darwin", "Melbourne", "Sydney")
library(rattle.data)
library(dplyr)
library(ggplot2)
weatherAUS %>%
filter(Location %in% cities,
!is.na(Temp3pm)) %>%
ggplot(aes(x = Rainfall, colour = Location, fill = Location)) +
geom_density(alpha = 0.3) +
scale_x_log10() + # distribution is very skewed, so log-transform the axis
labs(title = "Distribution of Rainfall",
subtitle = "Selective Locations",
caption = "Source: Australian Bureau of Meteorology",
x = "Daily rainfall (mm)",
y = "Density")
#> Warning: Transformation introduced infinite values in continuous x-axis
#> Warning: Removed 8338 rows containing non-finite values (stat_density).
由 reprex package (v0.3.0)
于 2021-02-03 创建
我正在尝试使用 R 中可用的 weatherAUS 数据集绘制密度图。我尝试使用以下代码。但是,我无法绘制出降雨分布的完美密度图。 使用下面的代码我的情节看起来像
用于数据集使用 -> View(weatherAUS) # 这是 R 内置数据集。
那么,我怎样才能完美地绘制密度图呢?我希望我的情节如下所示:
cities <- c("Canberra", "Darwin", "Melbourne", "Sydney")
# Build the required dataset and plot it.
weatherAUS %>%
filter(Location %in% cities) %>%
filter(Rainfall %>% is.na() %>% not()) %>%
ggplot(aes(x=Rainfall, colour=Location, fill=Location)) +
geom_density(alpha=0.55) +
labs(title="Distribution of Rainfall",
subtitle="Selective Locations",
caption="Source: Australian Bureau of Meteorology",
x="Rainfall",
y="Density")
当你说“完美”时,我不确定你在追求什么,但这可能会更好地传达信息:对 x 轴进行对数变换以避免出现长尾。但是,请注意,您最终会从 0 毫米降水量的 log10 中得到数千个无限值。
cities <- c("Canberra", "Darwin", "Melbourne", "Sydney")
library(rattle.data)
library(dplyr)
library(ggplot2)
weatherAUS %>%
filter(Location %in% cities,
!is.na(Temp3pm)) %>%
ggplot(aes(x = Rainfall, colour = Location, fill = Location)) +
geom_density(alpha = 0.3) +
scale_x_log10() + # distribution is very skewed, so log-transform the axis
labs(title = "Distribution of Rainfall",
subtitle = "Selective Locations",
caption = "Source: Australian Bureau of Meteorology",
x = "Daily rainfall (mm)",
y = "Density")
#> Warning: Transformation introduced infinite values in continuous x-axis
#> Warning: Removed 8338 rows containing non-finite values (stat_density).
由 reprex package (v0.3.0)
于 2021-02-03 创建