我正在使用ggplot来显示包含多个变量的框图,我使用了select方法,出现了这个错误,那么如何解决这个问题呢
library(dplyr)
p <- df
select(heart[,1],
trestbps,
chol,
thalach,
oldpeak,
ca,
target)
gather(key = "key",
value = "value",
-target)
ggplot(aes(y = value)) +
geom_boxplot(aes(fill = target),
alpha = .6,
fatten = .7) +
labs(x = "",
y = "",
title = "Boxplots for Numeric Variables") +
scale_fill_manual(
values = c("#fde725ff", "#20a486ff"),
name = "Heart\nDisease",
labels = c("No HD", "Yes HD")) +
theme(
axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
facet_wrap(~ key,
scales = "free",
ncol = 2)
plot(p)
Error in UseMethod("select_") : no applicable method for 'select_' applied to an object of class "c('integer', 'numeric')"
发布于 2020-11-24 23:50:27
根据代码,heart[,1]
意味着我们正在对第一列进行子集设置,如果它是一个data.frame
,它将被强制转换为一个向量,因为默认情况下drop = TRUE
,因此select
不能应用于向量。如果df
是dataset对象,而heart
是其中一列,那么我们需要%>%
来连接这两个列。同样,还需要连接到gather
library(dplyr)
p <- df %>%
select(heart, trestbps,
chol,
thalach,
oldpeak,
ca,
target) %>%
gather(key = "key",
value = "value",
-target) %>%
ggplot(aes(y = value)) +
geom_boxplot(aes(fill = target),
alpha = .6,
fatten = .7) +
labs(x = "",
y = "",
title = "Boxplots for Numeric Variables") +
scale_fill_manual(
values = c("#fde725ff", "#20a486ff"),
name = "Heart\nDisease",
labels = c("No HD", "Yes HD")) +
theme(
axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
facet_wrap(~ key,
scales = "free",
ncol = 2)
p
https://stackoverflow.com/questions/64996512
复制相似问题