使用R基本图形的堆积直方图 [英] Stacked Histograms Using R Base Graphics
本文介绍了使用R基本图形的堆积直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用ggplot2
可以很容易地创建堆叠的直方图:
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')
我想知道如何仅使用 R基本图形来创建两个直方图.
I would like to know how to create both histograms using only R base graphics.
推荐答案
您可以基于Species
和Sepal.Length
的频率表使用barplot()
生成两个图.
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
这篇关于使用R基本图形的堆积直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文