0

I am trying to plot the graph using ggplot2

So I have the following data set:

Experiment,Method,Values,Mean

10,IEEE802.11P,1,19.80
10,IEEE802.11P,5,21.91
10,IEEE802.11P,10,23.66
20,IEEE802.11P,1,16.03
20,IEEE802.11P,5,19.64
20,IEEE802.11P,10,27.81
40,IEEE802.11P,1,16.43
40,IEEE802.11P,5,27.65
40,IEEE802.11P,10,42.61
60,IEEE802.11P,1,17.06
60,IEEE802.11P,5,36.15
60,IEEE802.11P,10,49.87
80,IEEE802.11P,1,18.78
80,IEEE802.11P,5,41.70
80,IEEE802.11P,10,53.54
10,DTB-MAC,1,16.39
10,DTB-MAC,5,17.39
10,DTB-MAC,10,20.5
20,DTB-MAC,1,12.34
20,DTB-MAC,5,19.55
20,DTB-MAC,10,23.4
40,DTB-MAC,1,17.26
40,DTB-MAC,5,25.24
40,DTB-MAC,10,36.04
60,DTB-MAC,1,15.97
60,DTB-MAC,5,33.33
60,DTB-MAC,10,42.15
80,DTB-MAC,1,17.73
80,DTB-MAC,5,35.05
80,DTB-MAC,10,46.38

and here is the code for plotting:

library(grid);
library(ggplot2);
library(reshape2);
pdf(file = '$filename.pdf', width=5, height=5);

dat <- read.csv('bdr.csv');

ggplot(dat, aes(x = Experiment, y = Mean, colour = Method,
            linetype = as.factor(Values), shape = as.factor(Values))) +
  geom_line() +
  geom_point()+
labs(x='$xlabel',y='$ylabel', fill='')+
scale_shape_discrete(name='Beacon Send Rate', breaks=c('1', '5', '10'), labels = c('1HZ', '5HZ', '10HZ')) +
scale_linetype_discrete(name='Beacon Send Rate', breaks=c('1', '5', '10'), labels=c('1HZ', '5HZ', '10HZ'))+

scale_y_continuous(limits = c(0, 100), breaks = (seq(0,100,by = 20)))+
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='top', legend.background = element_rect(colour = NA));

the output is here:

https://www.dropbox.com/s/wpmyrkatnizer4b/look2.pdf?dl=0

but I have two problem:

1- the numbers in x.axis (variable Experiment) doesn't appear correctly. It must be 10, 20, 40, 60, 80 but the out put is 20,40,60,80.

2- Is there any method the mix the legends. For example in my case I have two groups of legends (1hz,5hz,10hz) and (DTB-MAC,IEEE802.11P) and make just one group with 6 items?

user3415921
  • 31
  • 2
  • 9

1 Answers1

0

You could combine the legends into one by mapping to the combination of Method and Values. You could either make a new variable in your dataset using interaction or, as I've done here, make the interaction using : with the two factor variables. If you map to the combined variable, you have to do a certain amount of work assigning the colors, linetype, and shapes "by hand" with the appropriate scale_manual. To do this correctly you have to know the order the combined variables shows up in the legend.

For the x axis, you can always set the x axis breaks in scale_x_continuous.

ggplot(dat, aes(x = Experiment, y = Mean, colour = Method:factor(Values),
             linetype = Method:factor(Values), shape = Method:factor(Values))) +
    geom_line() +
    geom_point()+
    labs(x='$xlabel',y='$ylabel', fill='') +
    scale_shape_manual(name = 'Method and\nBeacon Send Rate', 
                    labels = c('DTB-MAC 1HZ', 'DTB_MAC 5HZ', 'DTB_MAC 10HZ', 'IEEE802.11P 1HZ', 'IEEE802.11P 5HZ', 'IEEE802.11P 10HZ'), 
                    values = rep(c(15,16,17), 2)) +
    scale_linetype_manual(name='Method and\nBeacon Send Rate', 
                      labels = c('DTB-MAC 1HZ', 'DTB_MAC 5HZ', 'DTB_MAC 10HZ', 'IEEE802.11P 1HZ', 'IEEE802.11P 5HZ', 'IEEE802.11P 10HZ'), 
                      values = rep(c("solid", "dashed", "dotted"), 2)) +
    scale_color_manual(name='Method and\nBeacon Send Rate',
                    labels = c('DTB-MAC 1HZ', 'DTB_MAC 5HZ', 'DTB_MAC 10HZ', 'IEEE802.11P 1HZ', 'IEEE802.11P 5HZ', 'IEEE802.11P 10HZ'), 
                    values = rep(c("red", "blue"), each = 3)) +
    scale_y_continuous(limits = c(0, 100), breaks = (seq(0,100,by = 20))) +
    scale_x_continuous(breaks = c(10, 20, 40, 60, 80)) +
    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='top', 
         legend.background = element_rect(colour = NA)) +
    guides(shape = guide_legend(ncol = 2, keyheight = .5))
aosmith
  • 34,856
  • 9
  • 84
  • 118
  • thank you very much its exactly what I was looking for. Could you please help me two divide the legends for example in this example that we have lots of legends to divide them to two or three lines? – user3415921 Sep 25 '14 at 16:12
  • @user3415921 [This answer](http://stackoverflow.com/a/18400725/2461552) shows how to do what you want with the legend. I'll add it to the plot above. – aosmith Sep 25 '14 at 16:25
  • How Can I put two legend key in one row one I put them vertically? – user3415921 Sep 25 '14 at 17:13
  • @user3415921 You can use the same code as in my last edit if you have a vertical legend. Setting the `keyheight` will "squish" the legend down a bit, see my newest edit in `guides`. – aosmith Sep 25 '14 at 19:45