我如何在ggplot2中创建频率分布条图? [英] How can I make a Frequency distribution bar plot in ggplot2?

查看:1414
本文介绍了我如何在ggplot2中创建频率分布条图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据集示例

  nq 
0.140843018
0.152855833
0.193245919
0.156860105
0.171658019
0.186281942
0.290739146
0.162779517
0.164694042
0.171658019
0.195866609
0.166967913
0.136841748
0.108907644
0.264136384
0.356655651
0.250508305

我想制作百分比条形图/直方图就像这个问题:> RE:使用ggplot2对齐单条上的数字



完整数据集的NQ最大值为21,最小值为0.00005



但我无法修改代码,因为我没有Freq列,而且有一个系列。

我制作了一张我正在尝试制作的人物模型。



你能帮忙吗?

解决方案

请问这是否适合您?

  nq < -  read.table(text =
0.140843018
0.152855833
0.193245919
0.156860105
0.171658019
0.186281942
0.290739146
0.162779517
0.164694042
0.171658019
0.195866609
0.166967913
0.136841748
0.108907644
0.264136384
0.356655651
您的数据

nq $ V2 < - cut(nq $ V1,5,include.lowest = T)
nq2 < - 集合(V1〜 V2,nq,长度)
nq2 $ V3 <-nq2 $ V1 / sum(nq2 $ V1)
library(ggplot2)
ggplot()+ geom_bar(data = nq2,aes V2 = V1),stat =identity,width = 1,fill =white,col =black,size = 2)+
geom_text(vjust = 1,fontface =bold,data = nq2,aes(label = paste(sprintf(%1f,V3 * 100),%,sep =),x = V2,y = V1 + 0.4),size = 5)+
theme_bw()+
scale_x_discrete(expand = c(0,0),labels = sprintf(%。3f,seq(min(nq $ V1),max(nq $ V1) max(nq $ V1)/ 6)))+
ylab(No。 )+ xb()+
scale_y_continuous(expand = c(0,0))+
theme(
axis.title.y = element_text(size = 20,face =bold,angle = 0),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(color ='black',size = 2),
axis.text.x = element_text(face =bold ),
axis.text.y = element_text(face =bold)


Sample of the dataset.

nq
0.140843018
0.152855833
0.193245919
0.156860105
0.171658019
0.186281942
0.290739146
0.162779517
0.164694042
0.171658019
0.195866609
0.166967913
0.136841748
0.108907644
0.264136384
0.356655651
0.250508305

I would like to make a Percentage Bar plot/Histogram like this question: RE: Alignment of numbers on the individual bars with ggplot2

The max value of NQ for full dataset is 21 and minimum value is 0.00005

But I am unable to adapt the code as I don't have a Freq column and I have one series.

I have made a mockup of the figure I am trying to make.

Could you please help?

解决方案

Would that work for you?

nq <- read.table(text = "
0.140843018
0.152855833
0.193245919
0.156860105
0.171658019
0.186281942
0.290739146
0.162779517
0.164694042
0.171658019
0.195866609
0.166967913
0.136841748
0.108907644
0.264136384
0.356655651
0.250508305", header = F) # Your data

nq$V2 <- cut(nq$V1, 5, include.lowest = T)
nq2 <- aggregate(V1 ~ V2, nq, length)
nq2$V3 <- nq2$V1/sum(nq2$V1)
library(ggplot2)
ggplot() + geom_bar(data = nq2, aes(V2, V1), stat = "identity", width=1, fill = "white", col = "black", size = 2) + 
  geom_text(vjust=1, fontface="bold", data = nq2, aes(label = paste(sprintf("%.1f", V3*100), "%", sep=""), x = V2,  y = V1 + 0.4), size = 5) + 
  theme_bw() +
  scale_x_discrete(expand = c(0,0), labels = sprintf("%.3f",seq(min(nq$V1), max(nq$V1), by = max(nq$V1)/6))) +
  ylab("No. of Cases") + xlab("") +
  scale_y_continuous(expand = c(0,0)) +
  theme(
    axis.title.y = element_text(size = 20, face = "bold", angle = 0), 
    panel.grid.major = element_blank() ,
    panel.grid.minor = element_blank() ,
    panel.border = element_blank() ,
    panel.background = element_blank(),
    axis.line = element_line(color = 'black', size = 2),
    axis.text.x = element_text(face="bold"),
    axis.text.y = element_text(face="bold")
    ) 

这篇关于我如何在ggplot2中创建频率分布条图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆