r - 在 R 中调整帕累托图上的第二个 y 轴

标签 r ggplot2 pareto-chart

我在 R 中创建了两个帕累托图,都使用相同的数据。一个使用 ggplots stat_pareto,另一个使用 qcc 库中的 pareto.chart 函数。

ggplot(DT4, aes(x = reorder(sap_object_type_desc, -sum_duration), y = 
  sum_duration)) + 
  geom_bar(stat="identity") +
  theme(axis.text.x=element_text(angle=90,hjust=1)) + 
  stat_pareto(point.color = "red",   
              point.size = 2,        
              line.color = "black",  
              #size.line = 1,        
              bars.fill = c("blue", "orange"))

ggplot plot stat_pareto

或者使用pareto.chart函数

pareto.chart(avector, 
             ylab = "Sum", 
             # xlab = "Objective Type Description", 
             main = "Avector Pareto Chart",
             cumperc = c(20,40,60,80,100)) # or = seq(0, 100, by =25)

pareto_chart function

我想做的是调整上面两个图中的第二个 y 轴,使 100% 累积百分比与最高条对齐,就像第三个示例一样。有什么建议吗?

example plot

最佳答案

这是一个完整的示例,说明如何执行此操作,包括所有数据处理和标题等。希望对您有所帮助:)

counts <- c(0.2, 1.3,4.2,9.0,1.0,1.7,1.0,1.0,13.1)
tags <- c("BL11-a","BL-11b","BL-12","BL-13","BL-15","BL-16","BL-17","BL-18","Everything Else")

df <- data.frame(counts=counts,tags=tags,stringsAsFactors = FALSE)

df <- df[order(df$counts,decreasing=TRUE), ]

df$tags <- factor(df$tags, levels=df$tags)

df$cumulative <- cumsum(df$counts)

df$cumulative <- 100 * df$cumulative/tail(df$cumulative, n=1)

scaleRight <- tail(df$cumulative, n=1)/head(df$counts, n=1)

library(ggplot2)
ggplot(df, aes(x=df$tags)) +
  geom_bar(aes(y=df$counts), fill='deepskyblue4', stat="identity") +
  geom_path(aes(y=df$cumulative/scaleRight, group=1),colour="red", size=0.9) +
  geom_point(aes(y=df$cumulative/scaleRight, group=1),colour="red") +
  scale_y_continuous(sec.axis = sec_axis(~.*scaleRight, name = "Cumulative (%)")) +
  theme(axis.text.x = element_text(angle=90, vjust=0.6)) +
  labs(title="Pareto Chart", subtitle="SNS Hyspec Background Contributions", x="Background Source", y=expression(Counts(~mu~Ah/~mu~s)))

ggsave("snsParetoChart.png")

产生:

This pareto chart with scaled axes

关于r - 在 R 中调整帕累托图上的第二个 y 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53716675/

相关文章:

r - 模拟二元帕累托分布

r - 如何在shinyWidgets包的pickerInput()中更改组标题的粗体字体大小?

r - 将矩阵保存到具有正确对齐的 .txt 文件

r - 抖动 geom_line()

r - 结合 ggplot 和 plotrix

qlikview - 放大组合图表 (Pareto) 在 QlikView 11 中不起作用

r - 如何应用操作 data.table 并使用 2 个或更多值作为参数 R 的函数

r - 每行第一个字加上双引号

r - 具有连续彩虹颜色的热图

r - 计算多列上的多个 Pareto 图