R 使用ggplot创建帕累托图

R 使用ggplot创建帕累托图,r,ggplot2,R,Ggplot2,我使用这段代码创建了一个帕累托图,但是它在这个数据集上工作不好,x轴在图中变得不排序 cds您可以发布样本数据吗?请使用dput(newdata)的输出编辑问题。或者,如果dput的输出太大(head(newdata,20))。此外,在ggplot图形中,如果您已经有了data参数,请不要在aes()中使用newdata[,1]之类的内容,请使用列的名称。如果您的问题只是排序,请尝试aes(x=reorder(Cities,-Count),y=Count)可以在此处找到一些重新排序x轴的选项:

我使用这段代码创建了一个帕累托图,但是它在这个数据集上工作不好,x轴在图中变得不排序


cds您可以发布样本数据吗?请使用
dput(newdata)
的输出编辑问题。或者,如果dput的输出太大(head(newdata,20))
。此外,在ggplot图形中,如果您已经有了
data
参数,请不要在
aes()
中使用
newdata[,1]
之类的内容,请使用列的名称。如果您的问题只是排序,请尝试
aes(x=reorder(Cities,-Count),y=Count)
可以在此处找到一些重新排序x轴的选项:
cds <- catsvdogs
newdata <- cds[order(-cds$`Number of Households (in 1000)`),]
newdata <- data.frame(newdata$Location,newdata$`Number of Households (in 1000)`)
newdata <- newdata[c(1:10),c(1:2)]
newdata$cumulative <- cumsum(newdata$newdata..Number.of.Households..in.1000..)


ggplot(newdata, aes(x=newdata[,1])) +
  geom_bar(aes(y=newdata[,2]), fill='blue', stat="identity") +
  geom_point(aes(y=cumulative), color = rgb(0, 1, 0), pch=16, size=1) +
  geom_path(aes(y=cumulative, group=1), colour="slateblue1", lty=3, size=0.9) +
  theme(axis.text.x = element_text(angle=90, vjust=0.6)) +
  labs(title = "Pareto Plot", x = 'Cities', y =
         'Count')