R 中的箱线图,美学的长度必须为 1 或与数据的长度相同
boxplot in R, aesthetics must be either length 1 or the same length as data
我正在对来自 UCI 网站的每加仑汽车里程数据进行一些分析:
https://archive.ics.uci.edu/ml/datasets/Auto+MPG
我将第一列分为高里程或低里程:
mpg01 = I(auto1$mpg >= median(auto1$mpg))
Auto = data.frame(mpg01, auto1[,-1])
head(Auto)
mpg01 cylinders displacement horsepower weight acceleration year origin
1 FALSE 8 307 130 3504 12.0 70 1
2 FALSE 8 350 165 3693 11.5 70 1
3 FALSE 8 318 150 3436 11.0 70 1
4 FALSE 8 304 150 3433 12.0 70 1
5 FALSE 8 302 140 3449 10.5 70 1
6 FALSE 8 429 198 4341 10.0 70 1
现在我想为数据框中的每一列制作箱线图,由第一列分解。
vars <- c("cylinders", "displacement", "horsepower", "weight", "acceleration", "year", "origin")
ggplot(Auto) + geom_bar(aes(y=vars, fill=factor(mpg01)))
我收到错误 "Aes must be either length 1 or the same as data"
"Auto" 数据框的维度是 392x8
我可以只对每一列使用箱线图,但想知道是否有办法将它们合并为一个。谢谢!
我想也许你应该整理你的数据,然后绘制箱线图。
我从网站下载数据:
> head(df)
mpg01 cylinders displacement horsepower weight acceleration year origin
1 18 8 307 130 3504 12.0 70 1
2 15 8 350 165 3693 11.5 70 1
3 18 8 318 150 3436 11.0 70 1
4 16 8 304 150 3433 12.0 70 1
5 17 8 302 140 3449 10.5 70 1
6 15 8 429 198 4341 10.0 70 1
使用gather{tidyr}整理数据。
library("tidyr")
library("dplyr")
library("ggplot2")
tidy_df <- df %>% gather("vars","values",-mpg01)
而tidy_df是:
> head(tidy_df)
mpg01 vars values
1 18 cylinders 8
2 15 cylinders 8
3 18 cylinders 8
4 16 cylinders 8
5 17 cylinders 8
6 15 cylinders 8
然后就可以画箱线图了
ggplot(data=tidy_df,aes(vars,values)) + geom_boxplot(aes(fill=vars))
看起来是这样的:
已更新以解释生成的错误: 生成错误是因为需要定义 aes(x, y...)
来描述应如何将数据框变量映射到 geom 中。在您的例子中,没有为 geom_boxplot
定义 x
变量。为了将 x
变量定义为 df 的每一列,需要将 df 重塑为长格式(例如使用 reshape2::melt
或 tidyr::gather
)
以下是基于 mtcars 而不是您的数据的应该有效的解决方案。如果没有,我们可以在您 dput(Auto)
为我解决一次。您生成的图应该看起来像我附加的那个。首先,重塑您的数据。
library(reshape2)
library(ggplot2)
mtcars_melt <- melt(mtcars)
我现在可以在 aes
中定义 x
。注意:请注意以下两种情况与 facet_wrap
.
一起使用时的区别
# First with no facet_wrap
ggplot(mtcars_melt, aes(x=variable, y=value, fill=variable)) + geom_boxplot()
# Case 1 with facet_wrap
ggplot(mtcars_melt, aes(x=variable, y=value, fill=variable)) + geom_boxplot() + facet_wrap(~variable)
# Case 2 with facet_wrap
ggplot(mtcars_melt, aes(x="", y=value, fill=variable)) + geom_boxplot() + facet_wrap(~variable)
在情况 1 中,我在 aes
中定义了 x=variable
,但是使用 facet_wrap
它会强制每个方面都存在所有 x 变量,但是如果我设置 x=""
,它允许每个方面仅包含 1 个 x 变量。
现在让y轴有独立刻度,我可以设置scales="free_y"
ggplot(mtcars_melt, aes(x="", y=value, fill=variable)) + geom_boxplot() + facet_wrap(~variable, scales="free_y")
或者,我可以设置 scales="free"
以应用于 x 轴和 y 轴,并将其与 x=variable
一起使用以获得类似的解决方案。
ggplot(mtcars_melt, aes(x=variable, y=value, fill=variable)) + geom_boxplot() + facet_wrap(~variable, scales="free")
已编辑: 下面的代码应该适用于您的特定数据集:
library(reshape2)
library(ggplot2)
vars <- c("cylinders", "displacement", "horsepower", "weight", "acceleration", "year", "origin")
Auto_melt <- melt(Auto[, vars])
ggplot(Auto_melt, aes(x="", y=value, fill=variable)) + geom_boxplot() + facet_wrap(~variable, scales="free_y")
根据要求用代码编辑以按 mpg 分隔:
通过包含 "mpg01" 重新定义 vars,并通过 mpg id 融化数据。使用 mpg01 作为 aes x 值。
Auto <- structure(list(mpg01 = structure(c(2L, 1L, 1L, 1L, 1L), .Label = c("FALSE", "TRUE"), class = "factor"), cylinders = c(8L, 8L, 8L, 8L, 8L), displacement = c(307, 350, 318, 304, 302), horsepower = c(130L, 165L, 150L, 150L, 140L), weight = c(3504L, 3693L, 3436L, 3433L, 3449L), acceleration = c(12, 11.5, 11, 12, 10.5), year = c(70L, 70L, 70L, 70L, 70L), origin = c(1L, 1L, 1L, 1L, 1L)), .Names = c("mpg01", "cylinders", "displacement", "horsepower", "weight", "acceleration", "year", "origin"), row.names = c(NA, 5L), class = "data.frame")
vars <- c("mpg01", "cylinders", "displacement", "horsepower", "weight", "acceleration", "year", "origin")
Auto_melt <- melt(Auto[, vars], id.vars="mpg01")
ggplot(Auto_melt, aes(x=mpg01, y=value, fill=variable)) + geom_boxplot() + facet_wrap(~variable, scales="free_y")
我正在对来自 UCI 网站的每加仑汽车里程数据进行一些分析:
https://archive.ics.uci.edu/ml/datasets/Auto+MPG
我将第一列分为高里程或低里程:
mpg01 = I(auto1$mpg >= median(auto1$mpg))
Auto = data.frame(mpg01, auto1[,-1])
head(Auto)
mpg01 cylinders displacement horsepower weight acceleration year origin
1 FALSE 8 307 130 3504 12.0 70 1
2 FALSE 8 350 165 3693 11.5 70 1
3 FALSE 8 318 150 3436 11.0 70 1
4 FALSE 8 304 150 3433 12.0 70 1
5 FALSE 8 302 140 3449 10.5 70 1
6 FALSE 8 429 198 4341 10.0 70 1
现在我想为数据框中的每一列制作箱线图,由第一列分解。
vars <- c("cylinders", "displacement", "horsepower", "weight", "acceleration", "year", "origin")
ggplot(Auto) + geom_bar(aes(y=vars, fill=factor(mpg01)))
我收到错误 "Aes must be either length 1 or the same as data"
"Auto" 数据框的维度是 392x8
我可以只对每一列使用箱线图,但想知道是否有办法将它们合并为一个。谢谢!
我想也许你应该整理你的数据,然后绘制箱线图。 我从网站下载数据:
> head(df)
mpg01 cylinders displacement horsepower weight acceleration year origin
1 18 8 307 130 3504 12.0 70 1
2 15 8 350 165 3693 11.5 70 1
3 18 8 318 150 3436 11.0 70 1
4 16 8 304 150 3433 12.0 70 1
5 17 8 302 140 3449 10.5 70 1
6 15 8 429 198 4341 10.0 70 1
使用gather{tidyr}整理数据。
library("tidyr")
library("dplyr")
library("ggplot2")
tidy_df <- df %>% gather("vars","values",-mpg01)
而tidy_df是:
> head(tidy_df)
mpg01 vars values
1 18 cylinders 8
2 15 cylinders 8
3 18 cylinders 8
4 16 cylinders 8
5 17 cylinders 8
6 15 cylinders 8
然后就可以画箱线图了
ggplot(data=tidy_df,aes(vars,values)) + geom_boxplot(aes(fill=vars))
看起来是这样的:
已更新以解释生成的错误: 生成错误是因为需要定义 aes(x, y...)
来描述应如何将数据框变量映射到 geom 中。在您的例子中,没有为 geom_boxplot
定义 x
变量。为了将 x
变量定义为 df 的每一列,需要将 df 重塑为长格式(例如使用 reshape2::melt
或 tidyr::gather
)
以下是基于 mtcars 而不是您的数据的应该有效的解决方案。如果没有,我们可以在您 dput(Auto)
为我解决一次。您生成的图应该看起来像我附加的那个。首先,重塑您的数据。
library(reshape2)
library(ggplot2)
mtcars_melt <- melt(mtcars)
我现在可以在 aes
中定义 x
。注意:请注意以下两种情况与 facet_wrap
.
# First with no facet_wrap
ggplot(mtcars_melt, aes(x=variable, y=value, fill=variable)) + geom_boxplot()
# Case 1 with facet_wrap
ggplot(mtcars_melt, aes(x=variable, y=value, fill=variable)) + geom_boxplot() + facet_wrap(~variable)
# Case 2 with facet_wrap
ggplot(mtcars_melt, aes(x="", y=value, fill=variable)) + geom_boxplot() + facet_wrap(~variable)
在情况 1 中,我在 aes
中定义了 x=variable
,但是使用 facet_wrap
它会强制每个方面都存在所有 x 变量,但是如果我设置 x=""
,它允许每个方面仅包含 1 个 x 变量。
现在让y轴有独立刻度,我可以设置scales="free_y"
ggplot(mtcars_melt, aes(x="", y=value, fill=variable)) + geom_boxplot() + facet_wrap(~variable, scales="free_y")
或者,我可以设置 scales="free"
以应用于 x 轴和 y 轴,并将其与 x=variable
一起使用以获得类似的解决方案。
ggplot(mtcars_melt, aes(x=variable, y=value, fill=variable)) + geom_boxplot() + facet_wrap(~variable, scales="free")
已编辑: 下面的代码应该适用于您的特定数据集:
library(reshape2)
library(ggplot2)
vars <- c("cylinders", "displacement", "horsepower", "weight", "acceleration", "year", "origin")
Auto_melt <- melt(Auto[, vars])
ggplot(Auto_melt, aes(x="", y=value, fill=variable)) + geom_boxplot() + facet_wrap(~variable, scales="free_y")
根据要求用代码编辑以按 mpg 分隔: 通过包含 "mpg01" 重新定义 vars,并通过 mpg id 融化数据。使用 mpg01 作为 aes x 值。
Auto <- structure(list(mpg01 = structure(c(2L, 1L, 1L, 1L, 1L), .Label = c("FALSE", "TRUE"), class = "factor"), cylinders = c(8L, 8L, 8L, 8L, 8L), displacement = c(307, 350, 318, 304, 302), horsepower = c(130L, 165L, 150L, 150L, 140L), weight = c(3504L, 3693L, 3436L, 3433L, 3449L), acceleration = c(12, 11.5, 11, 12, 10.5), year = c(70L, 70L, 70L, 70L, 70L), origin = c(1L, 1L, 1L, 1L, 1L)), .Names = c("mpg01", "cylinders", "displacement", "horsepower", "weight", "acceleration", "year", "origin"), row.names = c(NA, 5L), class = "data.frame")
vars <- c("mpg01", "cylinders", "displacement", "horsepower", "weight", "acceleration", "year", "origin")
Auto_melt <- melt(Auto[, vars], id.vars="mpg01")
ggplot(Auto_melt, aes(x=mpg01, y=value, fill=variable)) + geom_boxplot() + facet_wrap(~variable, scales="free_y")