5

in this barplot the bars are not scaled in the same way. So the y axis is not ordered properly (157 > 1342.6). How do I fix this?

library(reshape)
library(ggplot2)

data <- matrix(1:9, 3, 3)
colnames(data) <- c("approach", "best", "worst")
data[1,] <- c("a", 1.8, 157.0)
data[2,] <- c("b", 592.3, 1342.6)
data[3,] <- c("c", 613.1, 3744.1)
data <- as.data.frame(data)
data <- melt(data, id="approach")

p <- ggplot(data, aes(x=approach, y=value, fill=variable)) +
  geom_bar(position="dodge", stat="identity")
p

plot

Thanks in advance.

dbrettschneider
  • 3,173
  • 1
  • 29
  • 28

2 Answers2

4

You need to have numerical values in your value-column, currently you have factors:

library(reshape)
library(ggplot2)

data <- matrix(1:9, 3, 3)
colnames(data) <- c("approach", "best", "worst")
data[1,] <- c("a", 1.8, 157.0)
data[2,] <- c("b", 592.3, 1342.6)
data[3,] <- c("c", 613.1, 3744.1)
data <- as.data.frame(data)
data <- melt(data, id="approach")

data$value = as.double(levels(data$value))[data$value] # <-- converting 

p <- ggplot(data, aes(x=approach, y=value, fill=variable)) +
  geom_bar(position="dodge", stat="identity")
p

enter image description here

martin
  • 3,149
  • 1
  • 24
  • 35
  • This changes the data only to 1-6. But I want to have the actual values that range between 1.8 and ~4000. – dbrettschneider Aug 01 '14 at 13:28
  • @dbrettschneider I changed it, but I don't know how better convert the values. – martin Aug 01 '14 at 13:31
  • @dbrettschneider I added the picture, is this what you want? – martin Aug 01 '14 at 13:33
  • Yes, thanks alot. But I don't really like that solution. Ggplot should do this automatically... – dbrettschneider Aug 01 '14 at 13:38
  • 2
    @dbrettschneider A matrix in R stores data as one [type](http://stackoverflow.com/a/5159049/2461552). To avoid the need to convert characters to numeric after the fact, you might put your data directly in a `data.frame` rather than starting with a `matrix`. Example: `dat2 = data.frame(approach = c("a", "b", "c"), best = c(1.8, 592.3, 613.1), worst = c(157.0, 1342.6, 3744.1))` – aosmith Aug 01 '14 at 15:31
  • I currently have this problem and this code isn't working anymore. `data$value = as.double(levels(data$value))[data$value] ` is turning the whole column to NA – pr0cz Aug 19 '22 at 16:48
  • Just use `data$value = as.numeric(data$value)` instead. – pr0cz Aug 19 '22 at 16:55
1

Hint for users not reading the comments: The above solution is not working anymore.

library(reshape)
library(ggplot2)

data <- matrix(1:9, 3, 3)
colnames(data) <- c("approach", "best", "worst")
data[1,] <- c("a", 1.8, 157.0)
data[2,] <- c("b", 592.3, 1342.6)
data[3,] <- c("c", 613.1, 3744.1)
data <- as.data.frame(data)
data <- melt(data, id="approach")

data$value = as.numeric(data$value)# <-- Use this instead and adjust it to the preferred column values (doubles/factors etc)

p <- ggplot(data, aes(x=approach, y=value, fill=variable)) +
  geom_bar(position="dodge", stat="identity")
p
pr0cz
  • 509
  • 7
  • 22