Adjusting the second y axis on a pareto chart in R

时光怂恿深爱的人放手 提交于 2020-08-08 05:43:11

问题


I have created two pareto charts in R, both using the same data. One using ggplots stat_pareto and the other with pareto.chart function from qcc library.

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

Or using pareto.chart function

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)

What I would like to do is adjust the second y axis on both of the above plots so that the 100% cumulative percentage aligns with the highest bar, like the third example. Any suggestions?


回答1:


Here is a full example on how to do this, including all data treatment and titles etc. I hope it helps :)

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

Which produces: This pareto chart with scaled axes

(I'm not allowed to post images yet apparently!)



来源:https://stackoverflow.com/questions/53716675/adjusting-the-second-y-axis-on-a-pareto-chart-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!