我需要在数据框上使用ggplot,并相应地指定每个绘制列的名称。我为这个范围构建了几个函数,但是我的main函数中的循环删除了列的名称。这里的代码到目前为止
随机生成的数据集
library(tidyverse)
library(ggplot2)
library(scales)
level=c("Strongly Agree", "Agree", "Neither agree or disagree","Disagree", "Strongly disagree",NA)
df <- data.frame(pre_1=as.character(sample(c("Yes","No", NA), 20, replace = T)),
pre_2=as.character(sample(level, 20, replace = T)),
post_1=as.character(sample(level, 20, replace = T)),
post_2=as.character(sample(level, 20, replace = T)),
stringsAsFactors=T)
虚假功能,以避免为yes-no items排序因子
dummy <- function(v, naVal = NULL) {
v1 <- na.omit(v)
# remove NA's
v2 <- as.integer(as.factor(v1))
if( !is.numeric(v2) ) stop("Only numeric vectors are accepted.")
# get unique values
v_unique <- unique(v2)
# remove 'naVal's
v_unique2 <- v_unique[! v_unique %in% naVal]
# count number of unique values and check whether all values are integers
if ( length(unique(v_unique2)) > 2L ||
any(as.integer(v_unique2) != v_unique2) ) FALSE else TRUE
}
绘图功能
dfplot_pre <- function(df){
select(df, starts_with("pre_")) %>%
for (col in .){
name =names(col)
if (dummy(as.character(col)) == TRUE) {
data.frame(col) %>%
na.omit() %>%
ggplot(.,aes(x=.)) +
geom_bar(aes(y = (..count..)/sum(..count..)), stat="count") +
geom_text(aes( label =paste(round((..count..)/sum(..count..)*100),"%"), y= (..count..)/sum(..count..)), stat= "count", vjust = -.5)+
scale_y_continuous(labels=percent,limits = c(-0, 1)) +
scale_x_discrete(drop=FALSE) +
ylab("Relative Frequencies (%)") +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) -> dummyplot
print(dummyplot)
print(name)}
else {
factor(col, c("Strongly Agree", "Agree", "Neither agree or disagree","Disagree", "Strongly disagree"),ordered = T ) %>%
data.frame() %>%
na.omit() %>%
ggplot(.,aes(x=.)) +
geom_bar(aes(y = (..count..)/sum(..count..)), stat="count") +
geom_text(aes( label =paste(round((..count..)/sum(..count..)*100),"%"), y= (..count..)/sum(..count..)), stat= "count", vjust = -.5)+
scale_y_continuous(labels=percent,limits = c(-0, 1)) +
scale_x_discrete(drop=FALSE) +
ylab("Relative Frequencies (%)")+
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)
) -> contplot
print(contplot)
}}
}
if语句末尾的print应返回列的名称但返回NULL。这是因为for (col in .){}
删除了数据框列的名称。
感谢任何建议
答案 0 :(得分:0)
求助:
需要使用seq
更改每个函数 dfplot_post <- function(df){
select(data, starts_with("pre_")) %>%
length() %>%
seq(1,.,1) %>%
for (i in .){
if (dummy(as.character(select(data, starts_with("pre_"))[[i]])) == TRUE) {
data.frame(select(data, starts_with("pre_"))[[i]]) %>%
na.omit() %>%
ggplot(.,aes(x=.)) +
geom_bar(aes(y = (..count..)/sum(..count..)), stat="count") +
geom_text(aes( label =paste(round((..count..)/sum(..count..)*100),"%"), y= (..count..)/sum(..count..)), stat= "count", vjust = -.5)+
scale_y_continuous(labels=percent,limits = c(-0, 1)) +
scale_x_discrete(drop=FALSE) +
ylab("Relative Frequencies (%)") +
ggtitle(names(select(data, starts_with("post_")))[i]) +
theme_light(base_size = 18) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(plot.title = element_text(hjust = 0.5)) -> dummyplot
print(dummyplot)}
else {
factor(select(data, starts_with("pre_"))[[i]], c("Strongly Agree", "Agree", "Neither agree or disagree","Disagree", "Strongly disagree"),ordered = T ) %>%
data.frame() %>%
na.omit() %>%
ggplot(.,aes(x=.)) +
geom_bar(aes(y = (..count..)/sum(..count..)), stat="count") +
geom_text(aes( label =paste(round((..count..)/sum(..count..)*100),"%"), y= (..count..)/sum(..count..)), stat= "count", vjust = -.5)+
scale_y_continuous(labels=percent,limits = c(-0, 1)) +
scale_x_discrete(drop=FALSE) +
ylab("Relative Frequencies (%)")+
ggtitle(names(select(data, starts_with("pre_")))[i]) +
theme_light(base_size = 18) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(plot.title = element_text(hjust = 0.5))-> contplot
print(contplot)
}}
}