Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

ggplot: grouped bar plot with column group ordering

$
0
0

I have the following data:

benchmark <- data.frame(
  num_element = c(10000, 40000, 70000, 100000, 130000),
  a = c(4.4363, 70.913, 215.83, 440.84, 740.84),
  b = c(5.6356, 26.254, 48.570, 71.882, 95.455),
  c = c(5.6356, 26.254, 48.570, 71.882, 95.455))

I want a grouped bar plot where all a, b, c value of the same num_element are together. This is how I plot it:

library(ggplot2) 
library(reshape2)

data.m <- melt(benchmark, id.vars='num_element')

ggplot(data.m, aes(num_element, value))+ 
  geom_bar(aes(fill = variable), 
           width = 0.4, 
           position = position_dodge(width=0.5), 
           stat="identity")+  
  theme(legend.position="top", 
        legend.title = element_blank(),
        axis.title.x=element_blank(), 
        axis.title.y=element_blank())

But the graph looks like this:

enter image description here

Attempts:

I convert the num_element to all strings:

num_element = c('10000', '40000', '70000', '100000', '130000')

and got this plot:

enter image description here

How do I arrange the x-axis in increasing order? Is there a way to do it without converting num_element to String?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>