PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 8, 2022

[FIXED] How can I sort the X axis in a Barplot in R?

 October 08, 2022     r, statistics     No comments   

Issue

I have binned data that looks like this:

  (8.048,18.05] (-21.95,-11.95] (-31.95,-21.95]   (18.05,28.05] (-41.95,-31.95]
             81              76              18              18             12
    (-132,-122]     (-122,-112]     (-112,-102]     (-162,-152]  (-102,-91.95]
              6               6               6               5              5
(-91.95,-81.95]     (-192,-182]   (28.05,38.05]   (38.05,48.05]  (58.05,68.05]
              5               4               4               4              4
  (78.05,88.05]     (98.05,108]     (-562,-552]     (-512,-502]    (-482,-472]
              4               4               3               3              3
    (-452,-442]     (-412,-402]     (-282,-272]     (-152,-142]  (48.05,58.05]
              3               3               3               3              3
  (68.05,78.05]       (118,128]       (128,138]     (-582,-572]    (-552,-542]
              3               3               3               2              2
    (-532,-522]     (-422,-412]     (-392,-382]     (-362,-352]    (-262,-252]
              2               2               2               2              2
    (-252,-242]     (-142,-132] (-81.95,-71.95]       (148,158]  (-1402,-1392]
              2               2               2               2              1
  (-1372,-1362]   (-1342,-1332]     (-942,-932]     (-862,-852]    (-822,-812]
              1               1               1               1              1
    (-712,-702]     (-682,-672]     (-672,-662]     (-632,-622]    (-542,-532]
              1               1               1               1              1
    (-502,-492]     (-492,-482]     (-472,-462]     (-462,-452]    (-442,-432]
              1               1               1               1              1
    (-432,-422]     (-352,-342]     (-332,-322]     (-312,-302]    (-302,-292]
              1               1               1               1              1
    (-202,-192]     (-182,-172]     (-172,-162] (-51.95,-41.95]  (88.05,98.05]
              1               1               1               1              1
      (108,118]       (158,168]       (168,178]       (178,188]      (298,308]
              1               1               1               1              1
      (318,328]       (328,338]       (338,348]       (368,378]      (458,468]
              1               1               1               1              1

How can I plot this data so that the bin is sorted from most negative on the left to most positive on the right? Currently my graph looks like this. Notice that it is not sorted at all. In particular the second bar (value = 76) is placed to the right of the first:

 (8.048,18.05] (-21.95,-11.95]
            81              76

This is the command I use to plot:

barplot(x,ylab="Number of Unique Tags", xlab="Expected - Observed")

Solution

I really want to help answer your question, but I gotta tell you, I can't make heads or tails of your data. I see a lot of opening parenthesis but no closing ones. The data looks sorted descending by whatever the values are on the bottom of each row. I have no idea what to make out of a value like "(8.048,18.05]"

Am I missing something obvious? Can you make a more simple example where your data structure is not a factor?

I would generally expect a data frame or a matrix with two columns, one for the X and one for the Y.

See if this example of sorting helps (I'm sort of shooting in the dark here)

tN <- table(Ni <- rpois(100, lambda=5))
r <- barplot(tN)

#stop here and examine the plot
#the next bit converts the matrix to a data frame,
#  sorts it, and plots it again

df<-data.frame(tN)
df2<-df[order(df$Freq),]
barplot(df2$Freq)


Answered By - JD Long
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing