1

so I got 2 plots, that work perfectly fine on their own. I'm trying to show them side by side, bars next to each other as pretty as possible, but it's just not working out. Trying to compare past and current food expenditure of some 600 people.

  ggplot(data=survey,aes(x=Past.FoodExp))+
  geom_bar()+
  geom_text(stat ='count',aes(label =..count.., vjust = -0.2))
  ggplot(data=survey,aes(x=Current.FoodExp))+
  geom_bar()+
  geom_text(stat ='count',aes(label =..count.., vjust = -0.2))

This is what I tried, but it didn't give me a result I wanted.

ggplot(data=survey, aes(y=Past.foodExp, x=Current.FoodExp)) + 
    geom_bar(position="dodge", stat="identity")

As you can see, it shows my ranges on both x and y axes, and I need it on only x, while y being the total count of people I surveyed. Any of you got an idea how to go around this and present it properly? Thanks.


Reproducible example

dput(head(survey,10))

structure(list(Past.FoodExp = structure(c(2L, 1L, 3L, 4L, 3L, 
4L, 3L, 4L, 3L, 3L), .Label = c("0-100\x80", "101-200\x80", "201-300\x80", 
"300\x80+"), class = "factor"), Current.FoodExp = structure(c(2L, 
2L, 4L, 4L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("0-100\x80", 
"101-200\x80", "201-300\x80", "300\x80+"), class = "factor")), row.names = c(NA, 
10L), class = "data.frame")
dc37
  • 15,840
  • 4
  • 15
  • 32
aurora5
  • 11
  • 6
  • Can you provide a reproducible example of your dataset ? see this link: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – dc37 Apr 18 '20 at 18:39
  • @dc37 Yeah, gimme a sec – aurora5 Apr 18 '20 at 18:42
  • I uploaded the csv here https://easyupload.io/va2r62 survey<-read.csv( file="repex.csv", header=TRUE, sep=",", quote = "\"") – aurora5 Apr 18 '20 at 18:45
  • just copy paste the output of `dput(survey)`. If it is too long, you can do `dput(head(survey,10))` – dc37 Apr 18 '20 at 18:46
  • here you go dput(head(survey,10)) structure(list(Past.FoodExp = structure(c(2L, 1L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 3L), .Label = c("0-100\200", "101-200\200", "201-300\200", "300\200+"), class = "factor"), Current.FoodExp = structure(c(2L, 2L, 4L, 4L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("0-100\200", "101-200\200", "201-300\200", "300\200+"), class = "factor")), row.names = c(NA, 10L), class = "data.frame") – aurora5 Apr 18 '20 at 18:49

1 Answers1

0

You need to reshape your dataframe into a longer format. You can use pivot_longer function for example:

library(tidyr)
library(dplyr)

survey %>% pivot_longer(everything(), names_to = "var", values_to = "val")

# A tibble: 20 x 2
   var             val          
   <chr>           <fct>        
 1 Past.FoodExp    "101-200\x80"
 2 Current.FoodExp "101-200\x80"
 3 Past.FoodExp    "0-100\x80"  
 4 Current.FoodExp "101-200\x80"
 5 Past.FoodExp    "201-300\x80"
 6 Current.FoodExp "300\x80+"   
 7 Past.FoodExp    "300\x80+"   
 8 Current.FoodExp "300\x80+"   
 9 Past.FoodExp    "201-300\x80"
10 Current.FoodExp "201-300\x80"
11 Past.FoodExp    "300\x80+"   
12 Current.FoodExp "201-300\x80"
13 Past.FoodExp    "201-300\x80"
14 Current.FoodExp "300\x80+"   
15 Past.FoodExp    "300\x80+"   
16 Current.FoodExp "300\x80+"   
17 Past.FoodExp    "201-300\x80"
18 Current.FoodExp "300\x80+"   
19 Past.FoodExp    "201-300\x80"
20 Current.FoodExp "300\x80+" 

So, with this structure, you can now pass it easily in ggplot2 as follow:

library(tidyr)
library(dplyr)
library(ggplot2)

survey %>% pivot_longer(everything(), names_to = "var", values_to = "val") %>%
  ggplot(aes(x = val, fill = var))+
  geom_bar(position = position_dodge())+
  geom_text(stat = "count", aes(label = ..count..), vjust = -0.2, position = position_dodge(0.9))

enter image description here

Does it answer your question ?

dc37
  • 15,840
  • 4
  • 15
  • 32