1

I am plotting a multiple grouping bar chart and finding that, although the data is displaying correctly, the chart is completely skewed in it's display because the y-axis tick values are not in numerical order.

Current graph output:

enter image description here

Below is my code, can anyone show me how I can order the values numerically?

     library(ggplot2)
veg_db <- read.csv("filepath\\filename.csv", header = TRUE)
veg_df <- data.frame(veg_db)

veg <- veg_df[c(125),c(4,5,9,12,13,18)]
white_veg <- data.frame(t(veg))
colnames(white_veg) <- c("value")
rownames(white_veg) <- c("Broccoli", "Carrots", "Leafy Greens", "Peas", "Peppers", "Tomatoes")
white_veg <- cbind(vegetables = rownames(white_veg),ethnicity = "white", white_veg)


veg <- veg_df[c(129),c(4,5,9,12,13,18)]
black_veg <- data.frame(t(veg))
colnames(black_veg) <- c("value")
rownames(black_veg) <- c("Broccoli", "Carrots", "Leafy Greens", "Peas", "Peppers", "Tomatoes")
black_veg <- cbind(vegetables = rownames(black_veg),ethnicity = "black", black_veg)

total_veg <- merge(white_veg, black_veg, all = TRUE)
total_veg

plot <- ggplot(total_veg, aes(vegetables, value, fill = ethnicity))+
  geom_bar(position="dodge",stat="identity")


plot

Here is the output of my total_veg dataframe:

total_veg

     vegetables ethnicity value
1      Broccoli     white 10.18
2      Broccoli     black  6.46
3       Carrots     white 12.58
4       Carrots     black  8.54
5  Leafy Greens     white  2.88
6  Leafy Greens     black  1.68
7          Peas     white 19.96
8          Peas     black 13.13
9       Peppers     white  3.09
10      Peppers     black  3.12
11     Tomatoes     white 80.13
12     Tomatoes     black 62.08
MrFlick
  • 195,160
  • 17
  • 277
  • 295
Matt Vaccaro
  • 318
  • 3
  • 9
  • 2
    Please see [how to create a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). A `dput()` of your data is prefered so we can see the data.types. It seems likely that `value` is a factor/character rather than a numeric value in your data.frame. This is probably from the `cbind()` creating a matrix rather than a data.frame. Is there a reason you are using `cbind()` rather than `data.frame()`? Or at least use `cbind.data.frame()`. – MrFlick May 20 '15 at 18:43
  • Then maybe it's happening during the transpose `t()`. That's an operation meant for matrices, not data.frames really. Without the original input file it's not easy to reproduce and diagnose the exact cause. – MrFlick May 20 '15 at 19:38
  • I swapped the cbind() functions for data.frame() functions but it didn't adjust the plot at all. I ran str(total_veg) and found that indeed value is a factor. To convert it I used as.numeric(as.character()) – Matt Vaccaro May 20 '15 at 19:43
  • @MrFlick thanks for all the help, still quite new to r and stackoverflow, your feedback is much appreciated – Matt Vaccaro May 20 '15 at 19:45
  • What does `str(total_veg)` return? Is `value` a factor rather than numeric? – Dennis May 21 '15 at 08:25
  • Yes, value was a factor. I used as.numeric(as.character()) function to remedy that. From what I'd read it's not best practice, but it got the job done. Any input on what the "proper" adjustment strategy would be? @Dennis – Matt Vaccaro May 22 '15 at 15:14

2 Answers2

0

Value was a factor rather than numeric (discovered this via str() function).

I used as.numeric(as.character()) to convert value from factor to numeric, which then displayed the data in a meaningful order when plotted. Thanks to @MrFlick for pointing me in the right direction.

Matt Vaccaro
  • 318
  • 3
  • 9
0

I encountered the same issue.

The file that you uploaded might have had that column as a string some value(mine was currency and it had $ in the beginning). Hence it was disordered.

Either re-upload the data in the right format or change it to a numeric value and you would get it plotted right.