r - 如何使用已计算的值在 ggplot2 图上绘制 95 百分位数和 5 百分位数?

标签 r ggplot2 percentile

我有this数据集并使用此 R 代码:

library(reshape2)
library(ggplot2)
library(RGraphics)
library(gridExtra)

long <- read.csv("long.csv")
ix <- 1:14

ggp2 <- ggplot(long, aes(x = id, y = value, fill = type)) +
    geom_bar(stat = "identity", position = "dodge") +
    geom_text(aes(label = numbers), vjust=-0.5, position = position_dodge(0.9), size = 3, angle = 0) +
    scale_x_continuous("Nodes", breaks = ix) +
    scale_y_continuous("Throughput (Mbps)", limits = c(0,1060)) +
    scale_fill_discrete(name="Legend",
                        labels=c("Inside Firewall (Dest)",
                                 "Inside Firewall (Source)",
                                 "Outside Firewall (Dest)",
                                 "Outside Firewall (Source)")) +
    theme_bw() +
    theme(legend.position="right") +
    theme(legend.title = element_text(colour="black", size=14, face="bold")) +
    theme(legend.text = element_text(colour="black", size=12, face="bold")) +
    facet_grid(type ~ .) +
plot(ggp2)

得到以下结果: enter image description here

现在我需要将 95 百分位数和 5 百分位数添加到图中。这些数字是在 this 中计算的。数据集(NFPnumbers(95%)和 FPnumbers(5%)列)。

似乎 boxplot() 可能在这里工作,但我不确定如何将它与 ggplot 一起使用。 stat_quantile(quantiles = c(0.05,0.95)) 也可以工作,但该函数会自行计算数字。我可以在这里使用我的电话号码吗?

我也尝试过:

geom_line(aes(x = id, y = long$FPnumbers)) +
geom_line(aes(x = id, y = long$NFPnumbers))

但结果看起来不够好。

geom_boxplot() 也不起作用:

geom_boxplot(aes(x = id, y = long$FPnumbers)) +
geom_boxplot(aes(x = id, y = long$NFPnumbers))

最佳答案

当您想要设置箱线图的参数时,您还需要 yminymax 值。由于它们不在数据集中,所以我计算了它们。

ggplot(long, aes(x = factor(id), y = value, fill = type)) +
  geom_boxplot(aes(lower = FPnumbers, middle = value, upper = NFPnumbers, ymin = FPnumbers*0.5, ymax = NFPnumbers*1.2, fill = type), stat = "identity") +
  xlab("Nodes") +
  ylab("Throughput (Mbps)") +
  scale_fill_discrete(name="Legend",
                      labels=c("Inside Firewall (Dest)", "Inside Firewall (Source)",
                               "Outside Firewall (Dest)", "Outside Firewall (Source)")) +
  theme_bw() +
  theme(legend.position="right",
        legend.title = element_text(colour="black", size=14, face="bold"),
        legend.text = element_text(colour="black", size=12, face="bold")) +
  facet_grid(type ~ .)

结果:

enter image description here

<小时/>

在您提供的数据集中,您提供了 valueFPnumbersNFPnumbers 变量。由于 FPnumbersNFPnumbers 代表 5 和 95 百分位数,我认为平均值由 value 表示。为了使此解决方案发挥作用,您需要每个“节点”的 minmax 值。我猜你的原始数据中有它们。

但是,由于数据集中没有提供它们,所以我通过根据 FPnumbersNFPnumbers 计算它们来弥补它们。 0.51.2 的乘法因子是任意的。这只是创建虚构的 minmax 值的一种方法。

关于r - 如何使用已计算的值在 ggplot2 图上绘制 95 百分位数和 5 百分位数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22830188/

相关文章:

R中的实时自动递增ggplot

r - 是什么导致了这个 ggplot2 方面的错误?

r - 如何将字符串转换为聚合分位数?

mysql - 有什么方法可以在同一个查询中获得第 95 个百分位数和总和?

使用 tm() 从 R 中的语料库中删除非英语文本

r - 用R中的dlnorm叠加数据的密度直方图,ggplot

r - ggplot2中的scale_colour_gradient2不能准确显示低或高的颜色?

r - seq_len(x) 总是比 1 快吗 :x in R? 一个比另一个更受欢迎吗?

r - 如何在knitr R代码块中间插入markdown?

python - 使用另一列的滚动值的数据框百分位