Aanna Aanna - 14 days ago 10
R Question

histogram /ggplot with x and y and True of False

I'm trying to make a histogram with the x axis as age and y axis as 0 or 1 (true or false)

person = c(1:50)
age = sample(1:5,50,replace=T)
drunk = sample(0:1,50,replace=T)
df = data.frame(person,age,drunk)

#plots that I tried:
ggplot(df, aes(x = age, y = drunk)) +
geom_bar(stat="identity")

hist(df$age)

barplot(df$drunk, names.arg=df$age)


These plots are not working for me. What I'm trying to get is: the X axis should look like 50 numbers ranging from 1-5 and the Y axis should be either 0 or 1 (True or False)

df$drunk <- as.character(df$drunk)
df$drunk[df$drunk == "0"] <- "False"
df$drunk[df$drunk == "1"] <- "True"


I tried using the code above to make a plot with it, but it's not working.

http://i.imgur.com/c2DoEha.png for example. Each Person has a random age in the plot and its shown if he is/was drunk (0 - 1 or true / false)

Answer

You need to:

  1. change the x in your aes to person
  2. add factor(drunk) to your y in aes
  3. add scale_y_discrete to label the y axis with TRUE and FALSE instead of 1 and 0.
  4. add scale_x_discrete to change the person tick label to the corresponding age using a helper function in label, and setting the limits as the range of the person variable in df.

    ggplot(df, aes(x = person, y = factor(drunk))) +
       geom_bar(stat="identity") +
       labs(x = "Age", y = "Drunk") +
       scale_y_discrete(labels = c("FALSE","TRUE")) +
       scale_x_discrete(limits = seq(min(df$person), max(df$person)), labels = function(x) df[df$person == x,]$age)
    

enter image description here