Stacked Histograms Using R Base Graphics

后端 未结 1 1048
栀梦
栀梦 2021-01-21 06:24

Using ggplot2 it is very easy to create stacked histograms:

library(ggplot2)

ggplot(data = iris, aes(x = Sepal.Length, fill = Species)) + 
  geom_         


        
1条回答
  •  囚心锁ツ
    2021-01-21 07:03

    You can generate both plots with barplot(), based on a frequency table of Species and Sepal.Length.

    # Create frequency table
    tab <- table(iris$Species, iris$Sepal.Length)
    
    # Stacked barplot
    barplot(tab)
    

    # Stacked percent barplot
    barplot(prop.table(tab, 2)) # Need to convert to marginal table first
    

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