How to apply histogram on dependent data in R?

后端 未结 1 1590
栀梦
栀梦 2020-12-18 17:20

I want to visualise the proportional data (Nij/n) about the sinus (independent) and arr/AHB (dependent variable) cases in females and males by R. ggplot2 appro

相关标签:
1条回答
  • 2020-12-18 18:00

    Your data looks like this:

    > data.female
            N11.1 N22.1 N33.1 N44.1 N21.1 N31.1 N32.1
    Sinus     1.0   0.0   0.0   0.0   0.0   0.0  12.0
    Arr/AHB   1.0   0.0   0.0   0.1   0.0   0.0  20.9
    > data.male
            N11.1 N22.1 N33.1 N44.1 N21.1 N31.1 N32.1
    Sinus     1.0   0.0   0.0   0.0   0.0   0.0   4.0
    Arr/AHB   1.0   0.0   0.0   0.0   0.0   0.0  24.0
    

    and you want to draw histograms of each row over multiple columns (like here) so the below demostrating.

    1. Histogram for each row where Sinus and ArrAHB groups separated

    You want to make a common identifier for Sinus and Arr/AHB so we create a new ID column for that. We use this method here with lattice pkg.

    require(lattice)
    Sinus<-c(1,0,0,0,0,0,12)
    ArrAHB<-c(1,0,0,0.1,0,0,20.9)
    Labels<-c("N11.1","N22.1","N33.1","N44.1","N21.1","N31.1","N32.1")
    ID<-c("Sinus","Arr/AHB")
    data.female<-data.frame(Sinus,ArrAHB,row.names=Labels)
    data.female<-as.data.frame(t(data.female))
    data.female$ID<-ID
    
    barchart(N11.1+N22.1+N33.1+N44.1+N21.1+N31.1+N32.1 ~ ID,
             data=data.female,
             auto.key=list(space='right')
             )
    

    and in comparison this is the chart for Man:

    1.2. Your Factor data must be converted to vectors or better: read your original files directly into vectors, not factors!

    Your input data is malformated as factor data, bad here, that is probably result of misusing read.csv such as missing hte flag na.strings="." or some malformated elements. More:

    "Sometimes when a data frame is read directly from a file, a column you’d thought would produce a numeric vector instead produces a factor. This is caused by a non-numeric value in the column, often a missing value encoded in a special way like . or -. To remedy the situation, coerce the vector from a factor to a character vector, and then from a character to a double vector. (Be sure to check for missing values after this process.) Of course, a much better plan is to discover what caused the problem in the first place and fix that; using the na.strings argument to read.csv() is often a good place to start.*

    In order to use this malformated data, the factor elements must be turnt into numeric values. The class commands reveal your mistake in reading your original data into R such that

    > class(data.female$N22.1)
    [1] "factor"
    > as.double(as.character(data.female$N22.1))
    [1] 0 0
    

    where the as.double(as.character(...)) allows use to maniputlate the data object again correctly. So the code

    require(lattice)
    data.female <- structure(list(N11.1 = structure(c(3L, 3L), .Label = c("", "0.0", "1.0", "N11"), class = "factor"),
                                  N22.1 = structure(c(2L, 2L), .Label = c("", "0.0", "2.0", "N22"), class = "factor"),
                                  N33.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N33"), class = "factor"),
                                  N44.1 = structure(2:3, .Label = c("", "0.0", "0.1", "0.2", "N44"), class = "factor"),
                                  N21.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N21"), class = "factor"),
                                  N31.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N31"), class = "factor"),
                                  N32.1 = structure(c(5L, 7L), .Label = c("", "0.0", "10.8", "11.0", "12.0", "17.0", "20.9", "22.8", "24.0", "3.0", "4.0", "44.0", "N32"),
                                                    class = "factor")), .Names = c("N11.1", "N22.1", "N33.1", "N44.1", "N21.1", "N31.1", "N32.1"),
                             row.names = c("Sinus", "Arr/AHB"), class = "data.frame")
    data.female$ID<-c("Sinus","Arr/AHB")
    data.female<-as.data.frame(data.female)
    
    f<-function(x) as.double(as.character(x))   #factors converted to vectors
    
    barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
             data=data.female,
             auto.key=list(space='right')
             )
    

    where the function f does the conversion from factors to vectors, alas factors are special kinds of vectors with class object and attribute value, more here.

    where you need to manipulate the legend yourself.

    2. Barchart again showing proportions

    The data input changed to readable format (not output of some CSZ file): values in N32.1 is far larger than any other data in other columns.

    require(lattice)
    Sinus<-c(1,0,0,0,0,0,12)
    ArrAHB<-c(1,0,0,0.1,0,0,20.9)
    Labels<-c("N11.1","N22.2","N33.1","N44.1","N21.1","N31.1","N32.1")
    ID<-c("Sinus","Arr/AHB")
    data.female<-data.frame(Sinus,ArrAHB,row.names=Labels)
    data.female<-t(data.female)
    
    barchart(data.female,auto.key=list(space='right'))
    
    > data.female
           N11.1 N22.2 N33.1 N44.1 N21.1 N31.1 N32.1
    Sinus      1     0     0   0.0     0     0  12.0
    ArrAHB     1     0     0   0.1     0     0  20.9
    

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