0

I have this code for drawing my graphs but I need to set the breakpoints in x axis with a fixed distances.

How do I need to change this code?

data1 <- as.matrix(read.table('$INPUT_FILE1', header = T));
data1.experiment <- as.numeric(data1[,\"Experiment\"]);
data1.obs <- as.numeric(data1[,\"Mean\"]);
data1.method <- as.factor(data1[,\"Method\"]);
df <- data.frame(data1.experiment, data1.method, data1.obs);
orderlist = c("5", "10", "20", "40", "60", "80");
ggplot(df, aes(x = data1.experiment, y = data1.obs, fill = data1.method), ylim=c(0,   380))+geom_bar(stat='identity', position='dodge')+labs(x='$xlabel',y='$ylabel',   fill='')+scale_fill_manual(values = c('indianred3','skyblue'), labels = c('DTB-MAC',   'IEEE802.11P'))+scale_y_continuous(limits = c(0, 380))+theme_bw()+theme(      panel.grid.major = element_line(colour = 'grey'), panel.border = element_rect(colour =   'black'), axis.line = element_blank(), panel.background = element_blank(),   legend.direction='horizontal', legend.position = c(0, 1), legend.justification = c(0, 1),   legend.background = element_rect(colour = NA));

For example if I want to draw this data set

Experiment Method Mean
5 IEEE802.11P 73.692058824
10 IEEE802.11P 36.846029412
20 IEEE802.11P 109.911111111
40 IEEE802.11P 238.427111111
60 IEEE802.11P 326.812469136
80 IEEE802.11P 372.041388889
5 DTB-MAC 7.470588235
10 DTB-MAC 27.014705882
20 DTB-MAC 84.032148148
40 DTB-MAC 177.680148148
60 DTB-MAC 244.599555556
80 DTB-MAC 286.52462963

The result I need is x axis with titles of 5,10,20,40,60,80 but with the same gap between them.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
user3415921
  • 31
  • 2
  • 9
  • You're missing a quotation mark somewhere. – Rich Scriven Jul 21 '14 at 16:20
  • 1
    We don't have access to your data so we really have no way of knowing what exactly is being plotted. Plus your code seems to have odd escapes in it which would make it difficult to copy and paste into R. Please edit your question so it contains a [minimal, reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Jul 21 '14 at 16:20
  • http://www.cookbook-r.com/Graphs/Axes_(ggplot2)/ – Rachel Gallen Jul 21 '14 at 16:30

1 Answers1

0

You just need to set your x-value as a factor as well.

Here's your data

data1<-data.frame(
    Experiment = c(5, 10, 20, 40, 60, 80, 5, 
10, 20, 40, 60, 80), 
    Method = c("IEEE802.11P", "IEEE802.11P", "IEEE802.11P", 
    "IEEE802.11P", "IEEE802.11P", "IEEE802.11P", "DTB-MAC", 
    "DTB-MAC", "DTB-MAC", "DTB-MAC", "DTB-MAC", "DTB-MAC"), 
    Mean = c(73.692058824, 36.846029412, 109.911111111, 
    238.427111111, 326.812469136, 372.041388889, 7.470588235, 
    27.014705882, 84.032148148, 177.680148148, 244.599555556, 
    286.52462963)
)

and here's how you can plot it

library(ggplot2)
ggplot(data1, aes(x = factor(Experiment), y = Mean, fill =factor(Method))) +
    ylim(c(0,380)) +
    geom_bar(stat='identity', position='dodge')+
    labs(x='$xlabel',y='$ylabel',   fill='')+
    scale_fill_manual(values = c('indianred3','skyblue'), labels = c('DTB-MAC',   'IEEE802.11P'))+
    scale_y_continuous(limits = c(0, 380))+
    theme_bw()+
    theme(
        panel.grid.major = element_line(colour = 'grey'),
        panel.border = element_rect(colour =   'black'), 
        axis.line = element_blank(), 
        panel.background = element_blank(),
        legend.direction='horizontal',
        legend.position = c(0, 1), legend.justification = c(0, 1),
        legend.background = element_rect(colour = NA));

to get

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • I don't know what is exactly the problem but when I set x as factor R put 5 break point in x axis after 40 and not in order. – user3415921 Jul 21 '14 at 17:10
  • Then it sounds like your values for Experiment may be characters for some reason? Did you import them correctly? You can explicitly set `factor(Experiement, levels=orderlist)` with your `orderlist` variable defined as above. – MrFlick Jul 21 '14 at 17:44