使用R绘制方框图的选择方法出错
我正在使用ggplot显示多变量的方框图,我使用了select方法,出现了此错误,因此如何解决此问题使用R绘制方框图的选择方法出错,r,ggplot2,dplyr,boxplot,R,Ggplot2,Dplyr,Boxplot,我正在使用ggplot显示多变量的方框图,我使用了select方法,出现了此错误,因此如何解决此问题 library(dplyr) p <- df select(heart[,1], trestbps, chol, thalach, oldpeak, ca, target) gather(key = "key", value
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')"
库(dplyr)
p基于代码,心脏[,1]
意味着我们正在对第一列进行子集设置,如果它是data.frame
,它将被强制为向量,因为默认情况下drop=TRUE
,因此select
无法应用于向量。如果df
是数据集对象,而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
库(dplyr)
p%
选择(心形、树形、,
乔,
塔拉赫,
老峰,
ca,
目标)%>%
聚集(key=“key”,
value=“value”,
-目标)%>%
ggplot(aes(y=值))+
几何箱线图(aes(填充=目标),
α=0.6,
肥肉=.7)+
实验室(x=“”,
y=“”,
title=“数值变量的方框图”)+
刻度填充手册(
值=c(“#fde725ff”、“#20a486ff”),
name=“Heart\nDisease”,
标签=c(“无HD”、“有HD”))+
主题(
axis.text.x=元素_blank(),
axis.ticks.x=元素_blank()+
面_包装(~key,
scales=“free”,
ncol=2)
P
什么是心脏[,1]
。你是说df%>%select