2

When I try to plot a graph, all my x axis values are overlaying on each other.

cdf <- ggplot(meltedData2, aes(x=id,group=1))+geom_line(aes(y=value,colour=variable),stat="identity")
cdf <- cdf + labs(x=xAxis, y=yAxis, title=title, ps=30)+scale_x_discrete(limits=id)

enter image description here

When I inserted the pretty breaks, I See half of the x axis values are missing

cdf <- cdf + labs(x=xAxis, y=yAxis, title=title, ps=30)+scale_x_discrete(limits=id, breaks=pretty_breaks())

enter image description here

I am looking for grouping of these x axis values automatically without overlaying on each other. Any help would be appreciated.

Here is the sample data

   rates<-data.frame(rates)
   id <- rownames(rates)
   rates <- cbind(id=id, rates)
   meltedData2 <- melt(rates,id.vars="id", na.rm = TRUE)
   print(meltedData2)
     id variable      value
1     1     run1 0.13661202
2     2     run1 0.54788419
3     3     run1 0.60124611
4     4     run1 0.58677686
5     5     run1 0.58219178
6     6     run1 0.66757493
7     7     run1 0.54103343
...........................
...........................
541 266     run2 0.61267606
542 267     run2 0.45714286
543 268     run2 0.53066038
544 269     run2 0.56542056
545 270     run2 0.55744681
546 271     run2 0.58429119
547 272     run2 0.55922330
548 273     run2 0.63581489
549 274     run2 0.57789855
550 275     run2 0.57085828

dput(head(rates))

structure(list(id = structure(c(1L, 112L, 199L, 210L, 221L, 232L
), .Label = c("1", "10", "100", "101", "102", "103", "104", "105", 
"106", "107", "108", "109", "11", "110", "111", "112", "113", 
"114", "115", "116", "117", "118", "119", "12", "120", "121", 
"122", "123", "124", "125", "126", "127", "128", "129", "13", 
"130", "131", "132", "133", "134", "135", "136", "137", "138", 
"139", "14", "140", "141", "142", "143", "144", "145", "146", 
"147", "148", "149", "15", "150", "151", "152", "153", "154", 
"155", "156", "157", "158", "159", "16", "160", "161", "162", 
"163", "164", "165", "166", "167", "168", "169", "17", "170", 
"171", "172", "173", "174", "175", "176", "177", "178", "179", 
"18", "180", "181", "182", "183", "184", "185", "186", "187", 
"188", "189", "19", "190", "191", "192", "193", "194", "195", 
"196", "197", "198", "199", "2", "20", "200", "201", "202", "203", 
"204", "205", "206", "207", "208", "209", "21", "210", "211", 
"212", "213", "214", "215", "216", "217", "218", "219", "22", 
"220", "221", "222", "223", "224", "225", "226", "227", "228", 
"229", "23", "230", "231", "232", "233", "234", "235", "236", 
"237", "238", "239", "24", "240", "241", "242", "243", "244", 
"245", "246", "247", "248", "249", "25", "250", "251", "252", 
"253", "254", "255", "256", "257", "258", "259", "26", "260", 
"261", "262", "263", "264", "265", "266", "267", "268", "269", 
"27", "270", "271", "272", "273", "274", "275", "28", "29", "3", 
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "4", 
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "5", 
"50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "6", 
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "7", 
"70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "8", 
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "9", 
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99"), class = "factor"), 
    run1 = c(0.136612021857924, 0.547884187082405, 0.601246105919003, 
    0.586776859504132, 0.582191780821918, 0.667574931880109), 
    run2 = c(0.136612021857924, 0.547884187082405, 0.601246105919003, 
    0.586776859504132, 0.582191780821918, 0.667574931880109)), .Names = c("id", 
"run1", "run2"), row.names = c(NA, 6L), class = "data.frame")

dput(head(meltedData2))

structure(list(id = structure(c(1L, 112L, 199L, 210L, 221L, 232L
), .Label = c("1", "10", "100", "101", "102", "103", "104", "105", 
"106", "107", "108", "109", "11", "110", "111", "112", "113", 
"114", "115", "116", "117", "118", "119", "12", "120", "121", 
"122", "123", "124", "125", "126", "127", "128", "129", "13", 
"130", "131", "132", "133", "134", "135", "136", "137", "138", 
"139", "14", "140", "141", "142", "143", "144", "145", "146", 
"147", "148", "149", "15", "150", "151", "152", "153", "154", 
"155", "156", "157", "158", "159", "16", "160", "161", "162", 
"163", "164", "165", "166", "167", "168", "169", "17", "170", 
"171", "172", "173", "174", "175", "176", "177", "178", "179", 
"18", "180", "181", "182", "183", "184", "185", "186", "187", 
"188", "189", "19", "190", "191", "192", "193", "194", "195", 
"196", "197", "198", "199", "2", "20", "200", "201", "202", "203", 
"204", "205", "206", "207", "208", "209", "21", "210", "211", 
"212", "213", "214", "215", "216", "217", "218", "219", "22", 
"220", "221", "222", "223", "224", "225", "226", "227", "228", 
"229", "23", "230", "231", "232", "233", "234", "235", "236", 
"237", "238", "239", "24", "240", "241", "242", "243", "244", 
"245", "246", "247", "248", "249", "25", "250", "251", "252", 
"253", "254", "255", "256", "257", "258", "259", "26", "260", 
"261", "262", "263", "264", "265", "266", "267", "268", "269", 
"27", "270", "271", "272", "273", "274", "275", "28", "29", "3", 
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "4", 
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "5", 
"50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "6", 
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "7", 
"70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "8", 
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "9", 
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99"), class = "factor"), 
    variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("run1", 
    "run2"), class = "factor"), value = c(0.136612021857924, 
    0.547884187082405, 0.601246105919003, 0.586776859504132, 
    0.582191780821918, 0.667574931880109)), .Names = c("id", 
"variable", "value"), row.names = c(NA, 6L), class = "data.frame")
Twinkling Star
  • 135
  • 2
  • 14
  • Can you add an example of your dataset? See [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for ideas on how to include a reproducible example. – aosmith May 18 '16 at 14:55
  • Using not the command `scale_x_discrete(limits=id)` should work. – Roman May 18 '16 at 15:22
  • I just removed the limits=id, now I see the values are not sorted numerically on the x axis. My x-axis looks like 100 20 40 60 80, it should be 20 40 60 80 100 – Twinkling Star May 18 '16 at 15:29
  • 1
    @TwinklingStar As already mentioned add some sample data using `dput()` for instance. Should the x-axis a discrete or a continuous one? Try to drop the complete `scale_x_discrete(limits=id)` command. – Roman May 18 '16 at 15:47
  • This sort of thing usually happens when you make the mistake of using values for your x axis that you _think_ are numeric, but actually are character or factor. Sometimes this happens when you import your data incorrectly, but it's impossible to tell for sure without more info as noted in other comments. – joran May 18 '16 at 16:19
  • Added dput as requested. To be more clear, I have a rates data frame with one column of values. I am trying to assign/bind Id to each value. While plotting the graph with Id on x axis and value on y axis, I am facing two problems 1. X axis values are not sorted numerically 2. X axis values are overlaying on each other – Twinkling Star May 18 '16 at 16:41

0 Answers0