Question: Hi,

I am trying to the plot the grid plot using the `ggplot2` library, however, I am getting the error as given below. Please assist me with this.

Error in `[.data.frame`(Group_plot, , virus_strain) :
undefined columns selected

Thank you,

Toufiq

EDIT by @RamRS:
---

OP edited their question after it was answered. This was the content before they edited it:

----

Hi,

I am trying to the plot the grid plot using the ggplot2 library, however, I am getting the error as given below. Please assist me with this.

> dput(head(res.mods.group))
structure(list(none = c(0, 0, 0, 0, 0, 0), Pandemic.influenza..A.H1N1.new.subtype. = c(-30.1204819277108,
34.1935483870968, 41.7910447761194, -33.3333333333333, 17.0731707317073,
0)), row.names = c("M16.14", "M16.2", "M16.26", "M15.73", "M15.21",
"M14.72"), class = "data.frame")

> dput(head(Gen3_ann))
structure(list(Module = c("M4.1", "M5.1", "M6.2", "M7.3", "M8.1",
"M9.9"), Cluster = c("A29", "A29", "A34", "A28", "A2", "A37"),
Cluster_location = c(1L, 4L, 4L, 6L, 1L, 1L), Function_New = c("Bio",
"TBD", "Cell", "Pathogenesis", "Toxicity",
"Erythrocytes"), position = c("A29.1", "A29.4", "A34.4",
"A28.6", "A2.1", "A37.1")), row.names = c("M4.1", "M5.1", "M6.2", "M7.3", "M8.1",
"M9.9"), class = "data.frame")

library(reshape2)
library(ggplot2)

# Set parameters
GSE_ID = "GSE21802"
platform = "GPL6102"

## prepared cluter position
Group_plot = res.mods.group
Group_plot <-Group_plot[rownames(Gen3_ann),]
rownames(Group_plot)==rownames(Gen3_ann) # check if rownames is the same
rownames(Group_plot) <- Gen3_ann$position
Group_plot <- as.data.frame(Group_plot)

head(Group_plot)

# creat new grid with all filtered cluster##
mod.group1 <- matrix(nrow=38,ncol=42)
rownames (mod.group1) <- paste0("A",c(1:38))
colnames (mod.group1) <- paste0("",c(1:42))
##

virus_strain = colnames(Group_plot)
N.virus_strain = length(virus_strain)

i=1
for (i in 1:N.virus_strain){
virus_strain = virus_strain[i]
for (i in 1 : nrow(Group_plot)){
Mx <- as.numeric(gsub(x = strsplit (rownames(Group_plot)[i],"\\.")[[1]][[1]],pattern = "A",replacement = ""))
My <- as.numeric(strsplit (rownames(Group_plot)[i],"\\.")[[1]][[2]])
mod.group1[Mx,My] <- Group_plot[,virus_strain][i]
}
mod.group <- mod.group1[-c(9:14,19:23),]
melt_test <- melt(mod.group,id.var=c("row.names"))
colnames(melt_test) = c("Aggregate","Sub_aggregate","%Response")
pdf(paste0(GSE_ID, "_", platform, "_Group_comparison_to_control", virus_strain, "_Grid.pdf"), height = 5.5, width = 8.5)
plot = ggplot(melt_test, aes(Aggregate, as.factor(Sub_aggregate))) +
geom_tile(color="#E6E6E6" , size = 0.2, fill=color )+
geom_point(aes(colour=`%Response`),size=4.5)+
ylab("") +
xlab("") +
labs(title= "Pandemic Infleunza vs Control")+
theme(axis.text.x = element_text(angle = -90, hjust = 0))+
scale_color_gradient2(low = "blue", mid="white", high = "red",limits=c(-100,100), na.value = "#E6E6E6", guide = "colourbar")+
theme_light() +
theme(panel.grid.minor = element_line(colour="black", size=0.9))+
coord_flip() +
scale_x_discrete(limits = rev(levels(melt_test$Aggregate))) +
theme(panel.border = element_rect(color = "black",size = 0.5),
axis.text.x = element_text(colour="black",size=9,angle=0,hjust=0.5,vjust=2,face="plain"),
axis.text.y = element_text(colour="black",size=9,angle=0,hjust=0.5,vjust=0.5,face="plain"))

plot(plot)
dev.off()
}

Error in `[.data.frame`(Group_plot, , virus_strain) :
undefined columns selected

Thank you,

Toufiq

----

Answer: no, then your code won't work any more, as you reference `virus_strain` two more times. Rename the single element and all occurrences to something like `this_strain`

this_strain = virus_strain[i]