Error while plotting in ggplot2
1
1
Entering edit mode
4.0 years ago

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


ggplot2 data.frame grid plot geom_point • 1.0k views
ADD COMMENT
0
Entering edit mode

The error message indicates that one (or more) iterations of virus_strain lead to values that are not actually found in Group_plot.

I would wager that this line is the problem:

virus_strain = virus_strain[i]

You're overwriting virus_strain in the first iteration with virus_strain[i].

ADD REPLY
0
Entering edit mode

Hi @Friederike,

Thank you very much for looking into this.

So, should I remove this code?

virus_strain = virus_strain[i]
ADD REPLY
3
Entering edit mode
4.0 years ago
Carambakaracho ★ 3.2k

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]
ADD COMMENT
0
Entering edit mode

Hi @ Carambakaracho,

Thank you, this is resolved.

ADD REPLY
3
Entering edit mode

Hi, it looks like you changed your question after it was resolved. Please do not do that - it is extremely bad etiquette.You can add content if you absolutely need to, but do not remove content - the whole post loses meaning if you do that.

I have restored the content that was previously there. Fortunately, Google cache had a copy of it. If not, this would not have been possible.

Also, if an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one answer if they all work.

Upvote|Bookmark|Accept

ADD REPLY
0
Entering edit mode

Hi @RamRS ,

Apologies. I had two browser tabs opened, most likely I might have deleted content from one tab.

ADD REPLY

Login before adding your answer.

Traffic: 2321 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6