ggplot graphing of proportions of observations within categories

后端 未结 2 720
别那么骄傲
别那么骄傲 2020-12-08 22:59

I am looking for advice on better ways to plot the proportion of observations in various categories.

I have a dataframe that looks something like this:



        
相关标签:
2条回答
  • 2020-12-08 23:24

    This might be a bit late for you and it is not involving ggplot, BUT:

    I think mosaicplots are the way forward to visualise the interaction of two factors:

    cat1 <- c("high", "low", "high", "high", "high", "low", "low", "low", "high", "low", "low")
    cat2 <- c("1-young", "3-old", "2-middle-aged", "3-old", "2-middle-aged", "2-middle-aged", "1-young", "1-young", "3-old", "3-old", "1-young")
    df <- as.data.frame(cbind(cat1, cat2))
    
    mosaicplot(cat2 ~ cat1, data = df, col = c(lightskyblue2', 'tomato'))
    

    In this plot, boxes for each value pair are scaled according o the number of observations in that category. You can provide a colour vector to aid with visualisation.

    0 讨论(0)
  • 2020-12-08 23:35

    I will understand if this isn't really what you're looking for, but I found your description of what you wanted very confusing until I realized that you were simply trying to visualize your data in a way that seemed very unnatural to me.

    If someone asked me to produce a graph with the proportions within each category, I'd probably turn to a segmented bar chart:

    ggplot(df,aes(x = cat2,fill = cat1)) + 
        geom_bar(position = "fill")
    

    enter image description here

    Note the y axis records proportions, not counts, as you wanted.

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