1
votes

I have a problem with using ggplot2 to generate histograms. I'm using the following function, which receives input, a data.frame. The function only has to create a data.frame as you can see. But I get an error indicating that the number of rows in each column is different. Any idea? I am using RStudio, Is it a problem of the library ggplot2? if so, is there any other library that serves to make histograms? I tested with barplot but I get an error margins ...

my function:

HIST_EPC_list<-function(DF_TAG_PHASE_EPC_counter){
    require(ggplot2)
    ggplot(DF_TAG_PHASE_EPC_counter, aes(x=DF_TAG_PHASE_EPC_counter$Tag_PHASE, y=DF_TAG_PHASE_EPC_counter$Num_EPC))+xlab("PHASE")+ylab("Number of EPC's")+ ggtitle("Histogram of Number of EPC/PHASE")+geom_histogram(stat="identity")

    #barplot(DF_TAG_PHASE_EPC_counter$Num_EPC, names.arg = DF_TAG_PHASE_EPC_counter$Tag_PHASE, xlab = "Tag_PHASE", ylab = "Num_EPC", main="Histograma Num tags/PHASE:", width=10)
    #par(mar=c(4,4,4,4))
}

example of data.frame DF_TAG_PHASE_EPC_counter

  Tag_PHASE Num_EPC
1      101.0       1
2      120.0       1
3      146.0       1
4       16.0       1
5      163.0       1
6       25.0       1
7       42.0       1
8       53.0       2
9       56.0       1
10      61.0       1
11      64.0       3
12      75.0       1

THE ERROR:

Error in data.frame(x = 1:8, y = c(1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L), PANEL = c(1L, : arguments imply differing number of rows: 8, 12

1
That code, with that example data, with the same names, works for me.Spacedman
every time I ran it gives me error ... any idea what could it be?Error in data.frame(x = 1:8, y = c(1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L), PANEL = c(1L, : arguments imply differing number of rows: 8, 12Alex
somewhere its getting a data frame with only 8 elements, and a PANEL column. Where's that come from? You've not passed the right thing to the function. Try again.Spacedman

1 Answers

1
votes

You should not use the table name when specifying the aes if you want to use the data parameter that you are passing in to the plot. You should use

HIST_EPC_list<-function(DF_TAG_PHASE_EPC_counter){
    require(ggplot2)
    ggplot(DF_TAG_PHASE_EPC_counter, aes(x=Tag_PHASE, y=Num_EPC))+
      xlab("PHASE")+ylab("Number of EPC's")+ 
      ggtitle("Histogram of Number of EPC/PHASE")+
      geom_histogram(stat="identity")
}