"name pattern" 上的 R 散点图调节

R scatterplot conditioning on "name pattern"

) 我正在尝试生成三个散点图,其组取决于列矩阵的名称,以便第一个图包含列名上以 A 开头的所有值,第二个以 B 开头,第三个以 C 开头。 我找到了一种方法如何根据起始字母(分配给变量"condition")提取数据矩阵的值。不幸的是,我不知道如何只提取变量名以便生成散点图。下面列出了最小的示例:

library(lattice)
library(latticeExtra)

data <- matrix(data= 1:9, nrow = 5, ncol = 9) #produces my matrix

colnames(data) <- c("A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3")
p <- c(0.1, 0.2, 0.3) #my probabilities
#data contains now my final matrix
mean <- colMeans(data) #taking the mean of the columns
xyplot(mean ~ p  , type = "l", lwd=2, xlab = "error rate", ylab = "error magnitude", groups = grep("A", colnames(data))) #scatterplot via lattice-package

有人能帮帮我吗?

你可以试试

library(tidyverse)
cbind.data.frame(M=mean,P=p) %>% 
  rownames_to_column() %>% 
  mutate(rowname=gsub("[[:digit:]]","", rowname)) %>% 
  ggplot(aes(M,P)) + 
      geom_point() + 
      facet_grid(~rowname)