如何将线图添加到 R 中的直方图
How to add line plot to histogram in R
我在 R 中绘制了一个直方图,代码如下所示。我正在尝试做两件事:
- 如何在每个柱上方显示百分比[%]?
- 在现有直方图的顶部添加一个线图。这显示了从左到右的百分比 [%] 累积。举例见附图。线图从 12.5% 开始,然后将下一个柱 (~22.92%) 添加到 12.5%。因此,它会绘制在 ~35.42%。它将从左到右添加每个条形百分比。有没有办法在我现有的 R 直方图上绘制类似的线图?
非常感谢任何帮助或指导。谢谢!
library(tidyverse)
HoursfromSLA <- c("-100","-100","-100","-100","-100","-100","-100","-100","-100","-100","-100","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-50","-50","-50","-50","-50","-50","-50","-50","-50","-50","-20","-20","-20","-20","-20","-20","-20","-20","-20","-20","20","20","20","20","50","50","50","50","50","50","50","50","75","75","75","75","75","75","100","100","100","100","135","135","135","135","225","225","225","225","310","310","350","350","400","400","500","500","500","500","675","675")
data <- data.frame(HoursfromSLA)
data$group <- ifelse(data$HoursfromSLA<0, "Green", "Red")
data$HoursfromSLA <- as.numeric(data$HoursfromSLA)
ggplot(data, aes(x=data, fill = group)) +
geom_vline(xintercept = 0, colour="black") +
geom_histogram(mapping = aes(x=HoursfromSLA, y=..count../sum(..count..)*100), col=I("white"), show.legend=FALSE, bins=25) +
scale_fill_manual(values = c("Green" = "darkgreen", "Red" = "darkred")) +
scale_x_continuous(name = "Time to SLA", breaks = seq(-150, 720, 30)) +
scale_y_continuous(name = "[%]")
可能有更好的方法使用分箱尺度来执行此操作,但您可以制作每列百分比的数据框并使用它:
data$HoursfromSLA2 <- as.numeric(as.character(cut(data$HoursfromSLA, breaks=seq(-120,900,30),labels = seq(-120,900-30,30)+15)))
data2 <- aggregate(data=data, HoursfromSLA~HoursfromSLA2+group, length )
data2$perc <- 100*data2$HoursfromSLA/sum(data2$HoursfromSLA)
ggplot(data2, aes(x=HoursfromSLA2, y=perc)) +
geom_col(aes(fill=group),width =30) +
geom_text(aes(vjust=-.5,label=round(perc,1))) +
geom_line(aes(x=HoursfromSLA2-15,y=cumsum(perc))) +
geom_point(aes(x=HoursfromSLA2-15,y=cumsum(perc))) +
geom_text(vjust=-1,hjust=1,aes(x=HoursfromSLA2-15,y=cumsum(perc), label=round(cumsum(perc),1))) +
theme_bw()+ scale_fill_manual(values = c("Green" = "darkgreen", "Red" = "darkred")) +
scale_x_continuous(name = "Time to SLA", breaks = seq(-150, 720, 30)) +
scale_y_continuous(name = "[%]") +
geom_vline(xintercept=0) +
theme(legend.position = "none")
我在 R 中绘制了一个直方图,代码如下所示。我正在尝试做两件事:
- 如何在每个柱上方显示百分比[%]?
- 在现有直方图的顶部添加一个线图。这显示了从左到右的百分比 [%] 累积。举例见附图。线图从 12.5% 开始,然后将下一个柱 (~22.92%) 添加到 12.5%。因此,它会绘制在 ~35.42%。它将从左到右添加每个条形百分比。有没有办法在我现有的 R 直方图上绘制类似的线图?
非常感谢任何帮助或指导。谢谢!
library(tidyverse)
HoursfromSLA <- c("-100","-100","-100","-100","-100","-100","-100","-100","-100","-100","-100","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-50","-50","-50","-50","-50","-50","-50","-50","-50","-50","-20","-20","-20","-20","-20","-20","-20","-20","-20","-20","20","20","20","20","50","50","50","50","50","50","50","50","75","75","75","75","75","75","100","100","100","100","135","135","135","135","225","225","225","225","310","310","350","350","400","400","500","500","500","500","675","675")
data <- data.frame(HoursfromSLA)
data$group <- ifelse(data$HoursfromSLA<0, "Green", "Red")
data$HoursfromSLA <- as.numeric(data$HoursfromSLA)
ggplot(data, aes(x=data, fill = group)) +
geom_vline(xintercept = 0, colour="black") +
geom_histogram(mapping = aes(x=HoursfromSLA, y=..count../sum(..count..)*100), col=I("white"), show.legend=FALSE, bins=25) +
scale_fill_manual(values = c("Green" = "darkgreen", "Red" = "darkred")) +
scale_x_continuous(name = "Time to SLA", breaks = seq(-150, 720, 30)) +
scale_y_continuous(name = "[%]")
可能有更好的方法使用分箱尺度来执行此操作,但您可以制作每列百分比的数据框并使用它:
data$HoursfromSLA2 <- as.numeric(as.character(cut(data$HoursfromSLA, breaks=seq(-120,900,30),labels = seq(-120,900-30,30)+15)))
data2 <- aggregate(data=data, HoursfromSLA~HoursfromSLA2+group, length )
data2$perc <- 100*data2$HoursfromSLA/sum(data2$HoursfromSLA)
ggplot(data2, aes(x=HoursfromSLA2, y=perc)) +
geom_col(aes(fill=group),width =30) +
geom_text(aes(vjust=-.5,label=round(perc,1))) +
geom_line(aes(x=HoursfromSLA2-15,y=cumsum(perc))) +
geom_point(aes(x=HoursfromSLA2-15,y=cumsum(perc))) +
geom_text(vjust=-1,hjust=1,aes(x=HoursfromSLA2-15,y=cumsum(perc), label=round(cumsum(perc),1))) +
theme_bw()+ scale_fill_manual(values = c("Green" = "darkgreen", "Red" = "darkred")) +
scale_x_continuous(name = "Time to SLA", breaks = seq(-150, 720, 30)) +
scale_y_continuous(name = "[%]") +
geom_vline(xintercept=0) +
theme(legend.position = "none")