hist() 来自列表名称的主标题
hist() main title from list name
我正在从一个列表中绘制 7 个直方图,并希望根据列表的位置更新直方图的标题。
这是我目前得到的,但我在执行脚本时遇到以下错误。
Error in item_df[df] : invalid subscript type 'list'
full_df <- read.csv("filename")
item_df <- split(full_df, full_df$PIA_ITEM)
par(mfrow = c(3,3)) # 3 rows and 3 columns
for (df in item_df) {
hist(df[[7]], breaks=9, main = paste("Histogram of", names(item_df[df])))
}
错误指向我尝试使用循环索引绘制列表名称位置
main = paste("Histogram of", names(item_df[df]))
改编这个例子:
item_df <- split(mtcars[, 1], mtcars$cyl)
par(mfrow = c(1, 3))
for (i in names(item_df)){
hist(item_df[[ i ]], main = paste("Histogram of", i))
}
由于未提供数据,我添加了一个带有虚拟数据的示例,然后为您的问题添加了一个可能的草图。根据您的代码,我认为您正在尝试将 PIA_ITEM
的值添加为标题并在位置 7 绘制变量。这个虚拟示例适用于 iris
:
full_df <- iris
item_df <- split(full_df, full_df$Species)
par(mfrow = c(3,3)) # 3 rows and 3 columns
for (i in 1:length(item_df))
{
hist(item_df[[i]]$Sepal.Length, breaks=9,
main = paste("Histogram of", unique(item_df[[i]]$Species)),
xlab = 'x')
}
并产生:
你的问题的代码应该是这样的:
full_df <- read.csv("filename.csv")
item_df <- split(full_df, full_df$PIA_ITEM)
par(mfrow = c(3,3)) # 3 rows and 3 columns
for (i in 1:length(item_df))
{
hist(item_df[[i]][,7], breaks=9,
main = paste("Histogram of", unique(item_df[[i]]$PIA_ITEM)),
xlab = 'x')
}
我正在从一个列表中绘制 7 个直方图,并希望根据列表的位置更新直方图的标题。
这是我目前得到的,但我在执行脚本时遇到以下错误。
Error in item_df[df] : invalid subscript type 'list'
full_df <- read.csv("filename")
item_df <- split(full_df, full_df$PIA_ITEM)
par(mfrow = c(3,3)) # 3 rows and 3 columns
for (df in item_df) {
hist(df[[7]], breaks=9, main = paste("Histogram of", names(item_df[df])))
}
错误指向我尝试使用循环索引绘制列表名称位置
main = paste("Histogram of", names(item_df[df]))
改编这个例子:
item_df <- split(mtcars[, 1], mtcars$cyl)
par(mfrow = c(1, 3))
for (i in names(item_df)){
hist(item_df[[ i ]], main = paste("Histogram of", i))
}
由于未提供数据,我添加了一个带有虚拟数据的示例,然后为您的问题添加了一个可能的草图。根据您的代码,我认为您正在尝试将 PIA_ITEM
的值添加为标题并在位置 7 绘制变量。这个虚拟示例适用于 iris
:
full_df <- iris
item_df <- split(full_df, full_df$Species)
par(mfrow = c(3,3)) # 3 rows and 3 columns
for (i in 1:length(item_df))
{
hist(item_df[[i]]$Sepal.Length, breaks=9,
main = paste("Histogram of", unique(item_df[[i]]$Species)),
xlab = 'x')
}
并产生:
你的问题的代码应该是这样的:
full_df <- read.csv("filename.csv")
item_df <- split(full_df, full_df$PIA_ITEM)
par(mfrow = c(3,3)) # 3 rows and 3 columns
for (i in 1:length(item_df))
{
hist(item_df[[i]][,7], breaks=9,
main = paste("Histogram of", unique(item_df[[i]]$PIA_ITEM)),
xlab = 'x')
}