0

I am using this code to create a Pareto chart, but it does not work well on this dataset, the x-axis becomes unsorted in the graphenter image description here

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')
yifei peng
  • 17
  • 1
  • 2
    Can you post sample data? Please edit **the question** with the output of `dput(newdata)`. Or, if it is too big with the output of `dput(head(newdata, 20))`. Also, in ggplot graphics, if you already have the `data` argument, don't use stuff like `newdata[,1]` in `aes()`, use the **names** of the columns. – Rui Barradas Nov 06 '20 at 07:24
  • 2
    if your problem is just ordering try `aes(x = reorder(Cities, -Count), y=Count)` – DS_UNI Nov 06 '20 at 08:03
  • 1
    Some options to reorder the x-axis can be found here: https://stackoverflow.com/q/3253641/6240490 – AndreasM Nov 06 '20 at 11:11

0 Answers0