在 R 中格式化和绘图的函数
Function for formatting and plotting in R
我目前正在尝试创建一个函数来正确地格式化我的数据和 return 一个排序的条形图。但出于某种原因,我不断收到此错误:
Error in `$<-.data.frame`(`*tmp*`, "Var1", value = integer(0)) :
replacement has 0 rows, data has 3
我试过调试它,但没有成功。我在底部有一个我期望的例子。谁能发现我做错了什么?
x <- rep(c("Mark","Jimmy","Jones","Jones","Jones","Jimmy"),2)
y <- rnorm(12)
df <- data.frame(x,y)
plottingfunction <- function(data, name,xlabel,ylabel,header){
newDf <- data.frame(table(data))
order <- newDf[order(newDf$Freq, decreasing = FALSE), ]$Var1
newDf$Var1 <- factor(newDf$Var1,order)
colnames(newDf)[1] <- name
plot <- ggplot(newDf, aes(x=name, y=Freq)) +
xlab(xlabel) +
ylab(ylabel) +
ggtitle(header) +
geom_bar(stat="identity", fill="lightblue", colour="black") +
coord_flip()
return(plot)
}
plottingfunction(df$x, "names","xlabel","ylabel","header")
一些意见,你的功能没有起作用,因为这部分不正确:
order <- newDf[order(newDf$Freq, decreasing = FALSE), ]$Var1
因为我们不知道 data
中是否会有列名称为 Var1
的列。看起来发生的事情是当你尝试你的代码时 运行:
newDf <- data.frame(table(df$x))
立即将您的列重命名为 Var1
,但是当您 运行 您的函数时,名称发生了变化。因此,为了解决这个问题,我建议您明确使用您的列名。在这个例子中,我使用 dplyr
库来让我的生活更轻松。所以按照你的代码和逻辑,它看起来像这样:
newDf <- data %>% group_by_(col_name) %>% tally
order <- newDf[order(newDf$n, decreasing = FALSE), col_name][[col_name]]
data[,col_name] <- factor(data[,col_name], order)
然后在您的 ggplot
中,我们可以使用 aes_string
来引用数据框的列名。那么整个函数看起来像这样:
plottingFunction <- function(data, col_name, xlabel, ylabel, header) {
#' create a dataframe with the data that we're interested in
#' make sure that you preserve the anme of the column that you're
#' counting on...
newDf <- data %>% group_by_(col_name) %>% tally
order <- newDf[order(newDf$n, decreasing = FALSE), col_name][[col_name]]
data[,col_name] <- factor(data[,col_name], order)
plot <- ggplot(data, aes_string(col_name)) +
xlab(xlabel) +
ylab(ylabel) +
ggtitle(header) +
geom_bar(fill="lightblue", colour="black") +
coord_flip()
return(plot)
}
plottingFunction(df, "x", "xlabel","ylabel","header")
输出如下:
我认为你的情节有 stat="identity"
是多余的,因为你可以只使用你的原始数据框而不是有一个 t运行 变形的。
我目前正在尝试创建一个函数来正确地格式化我的数据和 return 一个排序的条形图。但出于某种原因,我不断收到此错误:
Error in `$<-.data.frame`(`*tmp*`, "Var1", value = integer(0)) :
replacement has 0 rows, data has 3
我试过调试它,但没有成功。我在底部有一个我期望的例子。谁能发现我做错了什么?
x <- rep(c("Mark","Jimmy","Jones","Jones","Jones","Jimmy"),2)
y <- rnorm(12)
df <- data.frame(x,y)
plottingfunction <- function(data, name,xlabel,ylabel,header){
newDf <- data.frame(table(data))
order <- newDf[order(newDf$Freq, decreasing = FALSE), ]$Var1
newDf$Var1 <- factor(newDf$Var1,order)
colnames(newDf)[1] <- name
plot <- ggplot(newDf, aes(x=name, y=Freq)) +
xlab(xlabel) +
ylab(ylabel) +
ggtitle(header) +
geom_bar(stat="identity", fill="lightblue", colour="black") +
coord_flip()
return(plot)
}
plottingfunction(df$x, "names","xlabel","ylabel","header")
一些意见,你的功能没有起作用,因为这部分不正确:
order <- newDf[order(newDf$Freq, decreasing = FALSE), ]$Var1
因为我们不知道 data
中是否会有列名称为 Var1
的列。看起来发生的事情是当你尝试你的代码时 运行:
newDf <- data.frame(table(df$x))
立即将您的列重命名为 Var1
,但是当您 运行 您的函数时,名称发生了变化。因此,为了解决这个问题,我建议您明确使用您的列名。在这个例子中,我使用 dplyr
库来让我的生活更轻松。所以按照你的代码和逻辑,它看起来像这样:
newDf <- data %>% group_by_(col_name) %>% tally
order <- newDf[order(newDf$n, decreasing = FALSE), col_name][[col_name]]
data[,col_name] <- factor(data[,col_name], order)
然后在您的 ggplot
中,我们可以使用 aes_string
来引用数据框的列名。那么整个函数看起来像这样:
plottingFunction <- function(data, col_name, xlabel, ylabel, header) {
#' create a dataframe with the data that we're interested in
#' make sure that you preserve the anme of the column that you're
#' counting on...
newDf <- data %>% group_by_(col_name) %>% tally
order <- newDf[order(newDf$n, decreasing = FALSE), col_name][[col_name]]
data[,col_name] <- factor(data[,col_name], order)
plot <- ggplot(data, aes_string(col_name)) +
xlab(xlabel) +
ylab(ylabel) +
ggtitle(header) +
geom_bar(fill="lightblue", colour="black") +
coord_flip()
return(plot)
}
plottingFunction(df, "x", "xlabel","ylabel","header")
输出如下:
我认为你的情节有 stat="identity"
是多余的,因为你可以只使用你的原始数据框而不是有一个 t运行 变形的。