R 如何在绘图之前子查询数据

R 如何在绘图之前子查询数据,r,R,我有一个像这样的数据集 col1 - desc, col2 - value, col3 - count (header) value1, descriptor 1, 6 value1, descriptor 2, 3 value1, descriptor 3, 1 value2, descriptor 7, 8 value2, descriptor 8, 6 value2, descriptor 9, 2 我需要对数据进行分区,以便在

我有一个像这样的数据集

col1 - desc, col2 - value, col3 - count (header)
value1,      descriptor 1, 6
value1,      descriptor 2, 3
value1,      descriptor 3, 1
value2,      descriptor 7, 8
value2,      descriptor 8, 6
value2,      descriptor 9, 2

我需要对数据进行分区,以便在x轴上使用col2,col3作为y值,对于col1中的每个唯一值。

不清楚您计划创建什么样的图。但我认为关键是基于
col1
分割数据帧。之后,为每个分离的数据帧创建绘图

这里有一个例子。最终输出为
p_lst
,包含
col1
中所有唯一值的条形图,使用
col2
作为x轴,使用
col3
作为y轴。请注意,我还在每个绘图的标题中添加了
col1

# Load package
library(ggplot2)

# Create example data frame
dt <- read.csv(text = "col1 - desc, col2 - value, col3 - count (header)
value1,      descriptor 1, 6
value1,      descriptor 2, 3
value1,      descriptor 3, 1
value2,      descriptor 7, 8
value2,      descriptor 8, 6
value2,      descriptor 9, 2",
               header = TRUE, stringsAsFactors = FALSE)

# Rename dt
colnames(dt) <- c("col1", "col2", "col3")

# Split dt based on col1, save results to lst
lst <- split(dt, dt$col1)

# Create a function to produce bar plot
bar_fun <- function(dt){
  p <- ggplot(dt, aes(x = col2, y = col3)) +
    geom_col() +
    ggtitle(as.character(unique(dt$col1)))

  return(p)
}

# Plot all data frames in lst
p_lst <- lapply(lst, bar_fun)

# You can access individual plot by index or name
p_lst[[1]]
p_lst$value2
#加载包
图书馆(GG2)
#创建示例数据帧

dt嗨-你正确地猜测到我要做一个条形图。非常感谢你的例子!。简明扼要,正是我所需要的。将其添加到我的知识工具包中。