题
我们一直在努力与如何使用GGPLOT2包做出帕累托图 R中。在许多情况下使我们想通过X轴排序项的条形图或直方图时。 Pareto图我们想要的物品下令在Y轴的数值下降。有没有办法让ggplot绘制在Y轴上的值进行排序的项目?我第一次尝试对数据进行排序框架,但它似乎ggplot重新排列它们。
示例:
val <- read.csv("http://www.cerebralmastication.com/wp-content/uploads/2009/11/val.txt")
val<-with(val, val[order(-Value), ])
p <- ggplot(val)
p + geom_bar(aes(State, Value, fill=variable), stat = "identity", position="dodge") + scale_fill_brewer(palette = "Set1")
数据帧VAL被排序,但输出看起来像这样:
结果
<子>(来源: cerebralmastication.com ) 子>
哈德利正确地指出,这将产生一个好得多的图形用于显示实际值与预测的:
ggplot(val, aes(State, Value)) + geom_bar(stat = "identity", subset = .(variable == "estimate"), fill = "grey70") + geom_crossbar(aes(ymin = Value, ymax = Value), subset = .(variable == "actual"))
它返回:
结果
<子>(来源: cerebralmastication.com ) 子>
但它仍然不是一个帕累托图。任何提示?
解决方案
在GGPLOT2杆通过在因子水平的顺序进行排序。
val$State <- with(val, factor(val$State, levels=val[order(-Value), ]$State))
其他提示
子集和排序数据;
valact <- subset(val, variable=='actual')
valsort <- valact[ order(-valact[,"Value"]),]
从那里它只是一个标准boxplot()
在顶部具有非常手动累积函数:
op <- par(mar=c(3,3,3,3))
bp <- barplot(valsort [ , "Value"], ylab="", xlab="", ylim=c(0,1),
names.arg=as.character(valsort[,"State"]), main="How's that?")
lines(bp, cumsum(valsort[,"Value"])/sum(valsort[,"Value"]),
ylim=c(0,1.05), col='red')
axis(4)
box()
par(op)
其中应该是这样的
结果
<子>(来源: eddelbuettel.com )子>
和它甚至不需要的overplotting特技作为lines()
愉快地诠释初始曲线图。
一个传统的帕累托图中GGPLOT2 .......
读取之后开发 卡诺,E.L。,Moguerza,J.M.,&Redchuk,A。(2012)。六西格玛与R.(G.罗伯特,K. Hornik,&G.帕玛编)斯普林格。
library(ggplot2);library(grid)
counts <- c(80, 27, 66, 94, 33)
defects <- c("price code", "schedule date", "supplier code", "contact num.", "part num.")
dat <- data.frame(count = counts, defect = defects, stringsAsFactors=FALSE )
dat <- dat[order(dat$count, decreasing=TRUE),]
dat$defect <- factor(dat$defect, levels=dat$defect)
dat$cum <- cumsum(dat$count)
count.sum<-sum(dat$count)
dat$cum_perc<-100*dat$cum/count.sum
p1<-ggplot(dat, aes(x=defect, y=cum_perc, group=1))
p1<-p1 + geom_point(aes(colour=defect), size=4) + geom_path()
p1<-p1+ ggtitle('Pareto Chart')+ theme(axis.ticks.x = element_blank(), axis.title.x = element_blank(),axis.text.x = element_blank())
p1<-p1+theme(legend.position="none")
p2<-ggplot(dat, aes(x=defect, y=count,colour=defect, fill=defect))
p2<- p2 + geom_bar()
p2<-p2+theme(legend.position="none")
plot.new()
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 1)))
print(p1, vp = viewport(layout.pos.row = 1,layout.pos.col = 1))
print(p2, vp = viewport(layout.pos.row = 2,layout.pos.col = 1))
通过一个简单的例子:
> data
PC1 PC2 PC3 PC4 PC5 PC6 PC7 PC8 PC9 PC10
0.29056 0.23833 0.11003 0.05549 0.04678 0.03788 0.02770 0.02323 0.02211 0.01925
barplot(data)
正确做事
ggplot等效 “应为”:qplot(x=names(data), y=data, geom='bar')
但是,不正确地重新排序/字母顺序排列的条...因为这是levels(factor(names(data)))
将如何排序。
解决方案:qplot(x=factor(names(data), levels=names(data)), y=data, geom='bar')
呼!
此外,请参阅包 QCC 具有功能pareto.chart()
。看起来像它使用碱图形也一样,所以启动恩惠用于GGPLOT2溶液: - )
要简化起见,我们只考虑只估计。
estimates <- subset(val, variable == "estimate")
首先,我们重新排序因子水平,使得State
s以递减Value
的顺序绘制。
estimates$State <- with(estimates, reorder(State, -Value))
类似地,我们重新排序数据集并计算累积值。
estimates <- estimates[order(estimates$Value, decreasing = TRUE),]
estimates$cumulative <- cumsum(estimates$Value)
现在我们已经准备好绘制曲线。获得线和在同一坐标吧,关键是要转变状态变量(因素)是数字。
p <- ggplot(estimates, aes(State, Value)) +
geom_bar() +
geom_line(aes(as.numeric(State), cumulative))
p
正如在问题中提到,试图绘制两个可变基团的2个帕累托图紧邻彼此也不是很容易。如果你想多帕累托图你可能会更好使用磨制。
freqplot = function(x, by = NULL, right = FALSE)
{
if(is.null(by)) stop('Valor de "by" precisa ser especificado.')
breaks = seq(min(x), max(x), by = by )
ecd = ecdf(x)
den = ecd(breaks)
table = table(cut(x, breaks = breaks, right = right))
table = table/sum(table)
intervs = factor(names(table), levels = names(table))
freq = as.numeric(table/sum(table))
acum = as.numeric(cumsum(table))
normalize.vec = function(x){
(x - min(x))/(max(x) - min(x))
}
dados = data.frame(classe = intervs, freq = freq, acum = acum, acum_norm = normalize.vec(acum))
p = ggplot(dados) +
geom_bar(aes(classe, freq, fill = classe), stat = 'identity') +
geom_point(aes(classe, acum_norm, group = '1'), shape = I(1), size = I(3), colour = 'gray20') +
geom_line(aes(classe, acum_norm, group = '1'), colour = I('gray20'))
p
}