如何在ggplot2中制作频率分布条图?

时间:2014-07-02 18:03:50

标签: r plot 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

我想像这个问题制作一个Percentage Bar情节/直方图:RE: Alignment of numbers on the individual bars with ggplot2

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

但我无法调整代码,因为我没有Freq专栏,而且我有一个系列。

我制作了一个我想要制作的人物的模型。enter image description here

你可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

这会对你有用吗?

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")
    ) 

enter image description here

答案 1 :(得分:0)

我认为这很容易,但结果令人沮丧。所以也许&#34;对&#34;方法是在使用ggplot之前转换数据,因为它看起来像@DavidArenburg。但是,如果你觉得自己喜欢被黑客攻击ggplot,那么这就是我最终要做的事情。

首先,一些样本数据。

set.seed(15)
dd<-data.frame(x=sample(1:25, 100, replace=T, prob=25:1))
br <- seq(0,25, by=5) # break points

我的第一次尝试是

library(ggplot2)
ggplot(dd, aes(x)) + 
    stat_bin(position="stack", breaks=br) + 
    geom_text(aes(y=..count.., label=..density..*..width.., ymax=..count..+1), 
        vjust=-.5, breaks=br, stat="bin")

但那并没有制作漂亮的标签&#34;

enter image description here

所以我想我会使用percent()包中的scales函数来使其漂亮。但是,愚蠢的ggplot并不能真正使用..()..变量的函数,因为它只在data.frame中计算它们(然后是空baseenv())。它没有办法找到你使用的功能。所以这就是我转向黑客的时候。首先,我将提取&#34; Layer&#34;来自ggplot的定义和来自它的map_statistic。 (注意:这是使用&#34; ggplot2_1.0.0&#34;并且特定于该版本;这是一个私有函数,可能会在将来的版本中更改)

orig.map_statistic <- ggplot2:::Layer$map_statistic
new.map_statistic <- orig.map_statistic
body(new.map_statistic)[[9]]
# stat_data <- as.data.frame(lapply(new, eval, data, baseenv()))

这里引起悲伤的线路我更喜欢这个函数解决了在map.frame中找不到的绘图环境中的其他名称。所以我决定用

来改变它
body(new.map_statistic)[[9]] <- quote(stat_data <- as.data.frame(lapply(new, eval, data, plot$plot_env)))
assign("map_statistic", new.map_statistic, envir=ggplot2:::Layer)

所以现在我可以使用带有..()..变量的函数。所以我可以做到

library(scales)
ggplot(dd, aes(x)) + 
    stat_bin(position="stack", breaks=br) + 
    geom_text(aes(y=..count.., ymax=..count..+2, 
        label=percent(..density..*..width..)), 
        vjust=-.5, breaks=br, stat="bin")

获取

enter image description here

所以我不确定为什么ggplot有此默认行为。可能有一些很好的理由,但我不知道它是什么。这确实会改变ggplot对会话其余部分的行为方式。您可以使用

更改回默认值
assign("map_statistic", orig.map_statistic, envir=ggplot2:::Layer)
相关问题