R histogram from frequency table

后端 未结 4 1499
别那么骄傲
别那么骄傲 2021-01-13 17:00

So I\'ve figured out how to drill my data down to a frequency table -

        Overall.Cond Freq
235            1    0
236            2    0
237            3          


        
相关标签:
4条回答
  • 2021-01-13 17:42

    I think it should be a barplot, like so:

    barplot(dt$Freq, names.arg = dt$Overall.Cond)

    0 讨论(0)
  • 2021-01-13 17:46

    You can simply do

    myfreq=table(df$columnofinterest)
    plot(myfreq)
    
    0 讨论(0)
  • 2021-01-13 17:53

    Rebuild your data frame:

    df= as.data.frame(cbind(Overall.Cond= 1:9, Freq= c(0,0,1,1,9,1,1,1,1)))
    df
    

    Result:

      Overall.Cond Freq
    1            1    0
    2            2    0
    3            3    1
    4            4    1
    5            5    9
    6            6    1
    7            7    1
    8            8    1
    9            9    1
    

    Then make a vector of observations and plot it:

    df.freq= as.vector(rep(df$Overall.Cond, df$Freq))
    hist(df.freq)
    

    enter image description here

    0 讨论(0)
  • 2021-01-13 18:05

    Do you really want a histogram or a bar chart? If you insist on a histogram, you are lacking the upper boundary of your topmost bin; I will assume it is 10.

    The solution provided by user2030503 is somewhat wasteful, as it re-creates the data set from the frequency table. Since you already have your frequency table computed, you can use it directly in construction of your histogram object. The latter is essentially a list in R.

    Overall.Cond <- 1:10
    Freq <- c(0,0,1,1,9,1,1,1,1)
    myhist <-list(breaks=Overall.Cond, counts=Freq, density=Freq/diff(Overall.Cond),
                  xname="Overall Cond")
    class(myhist) <- "histogram"
    plot(myhist)
    

    As the bin width is 1, calculation of density could be simplified in this case; I just put it for the sake of generality.

    0 讨论(0)
提交回复
热议问题