[英]How can I construct Histogram, Bar plot, frequency curve and Ogive curve in R for this problem?
[英]How to plot frequency as step curve in R
我有一個數據框,如下所示:
ID AC AF Type
1 60 1 0.00352113 1
2 48 1 0.00352113 2
3 25 1 0.00352113 1
4 98 1 0.00352113 2
5 24 1 0.00352113 1
6 64 2 0.00704225 1
我需要在 X 軸上繪制 AF 的步進曲線,其頻率在 Y 軸上由 TYPE 着色。 我設法使用以下代碼獲得直方圖:
ggplot(data, aes(x = AF,fill=TYPE))+geom_histogram(aes(y = ..count..),bins=40)
但是,我需要一個如下所示的曲線圖而不是直方圖:
有什么建議可以實現這一目標嗎?
我們可以將geom_line
與stat = 'count'
:
首先我生成一些虛擬數據:
set.seed(123)
df1 <- data.frame(Type = sample(1:3, 100, replace = T),
AF = sample(1:10, 100, replace = T,
prob = seq(.8, .2, length.out = 10)))
然后我們制作情節:
ggplot(df1, aes(x = AF))+
geom_line(stat = 'count', aes(group = Type, colour = factor(Type)))
這是一個替代方案(HT 到@eipi)
set.seed(123)
df1 <- data.frame(Type = sample(1:3, 1000, replace = T),
AF = round(rnorm(1000), 3))
ggplot(df1, aes(x = AF))+
geom_step(stat = 'bin', aes(group = Type, colour = factor(Type)),
bins = 35)
在常規graphics
庫中,您可以執行以下操作:
set.seed(1)
AF<-sample(1:20,1000,replace=TRUE)
set.seed(2)
TYPE<-sample(c(1:2),1000,replace = TRUE)
plot(table(AF[which(TYPE==1)])/length(AF[which(TYPE==1)]),type="l",col="blue",
ylab="Frequency of AF",xlab="AF")
points(table(AF[which(TYPE==2)])/length(AF[which(TYPE==2)]),type="l")
legend("bottomright",c("Type1","Type2"),lty=1,lwd=3,col=c("blue","black"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.