简体   繁体   中英

Stacked Histograms Using R Base Graphics

Using ggplot2 it is very easy to create stacked histograms:

library(ggplot2)

ggplot(data = iris, aes(x = Sepal.Length, fill = Species)) + 
  geom_histogram(colour = 'white') 

在此处输入图片说明

ggplot(data = iris, aes(x = Sepal.Length, fill = Species)) + 
  geom_histogram(colour = 'white', position = 'fill')

在此处输入图片说明

I would like to know how to create both histograms using only R base graphics .

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM