[英]frequency plot for binned data
我已將數據分類為各種大小,並希望為給定的花瓣長度范圍內的每個物種制作一個刻面頻率 plot(每個大小箱的直方圖類似於所附圖片)。
library(ggplot2)
br = seq(1,6,by=0.4)
df1 = iris
df = as.data.frame(with(df1, table(spe = df1$Species,
pl =cut(df1$Petal.Length, br, include.lowest = TRUE))))
ggplot(df, aes(x = pl, y = Freq)) + geom_bar(stat = "identity")
我怎樣才能做到這一點? 我在設置 x 軸時遇到問題,因為我希望它是連續的,而 plot 它被裝箱了。
這是一種使用facet_wrap
的方法。 我們可以使用stringr
中的str_replace_all
來改善軸標簽的外觀。
library(ggplot2)
library(stringr)
ggplot(df, aes(x = pl, y = Freq, fill = spe)) +
geom_bar(stat = "identity") +
scale_x_discrete(labels = function(x)
str_replace_all(x, regex(c("[\\(\\[]" = "", "," = " - ", "\\]" = "")))) +
facet_wrap(.~spe, ncol = 1) +
theme(axis.text.x=element_text(angle = 45, hjust = 1))
另一種方法是使用geom_histogram
:
ggplot(df1, aes(x = Petal.Length, fill = Species)) +
geom_histogram(color = "white", bins = 20) +
facet_wrap(.~Species, ncol = 1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.