R studio histogram Error: stat_bin() must not be used with a y aesthetic

13,958

geom_histogram takes one vector as an argument and will put the data in different 'buckets' or 'bins'

You probably want something like:

figure4 <- ggplot(data= dataexample3, mapping = aes(x=year, y=income))
figure4 + geom_col(aes(color=country)
Share:
13,958
G.ven
Author by

G.ven

Updated on June 27, 2022

Comments

  • G.ven
    G.ven almost 2 years

    enter image description here

    I want to make a histogram with the data above, data shows the income per person in some country. Using this command I keep getting that

    Error: stat_bin() must not be used with a y aesthetic.

    incomenew <- data_opdracht_income_per_person_xlsx %>%
      gather("1990":"2018", key = "year", value = "income")
    incomenew
    
    incomenew$year <- as.numeric(incomenew$year)
    
    incomenew$income <- as.numeric(incomenew$income)
    
    
    dataexample3 <- incomenew %>%
      filter(country == "Netherlands" | country == "China" | country == "Nigeria")
    
    figure4 <- ggplot(data= dataexample3, mapping = aes(x=year, y=income))
    
    figure4 + geom_histogram() 
    

    How can I change y not to be aesthetic?

    • divibisan
      divibisan about 5 years
      Please don't post data as images or links. Make a minimal reproducible example of your problem and include it as formatted text in your question.
    • camille
      camille about 5 years
      It's really hard to help without knowing anything about your data. But is a histogram what you actually want? That's a chart that shows the distribution of a continuous variable, where the y-axis shows the count or density of values within given bins. So setting a y-value doesn't make sense. Maybe you want a bar chart
  • G.ven
    G.ven about 5 years
    how can i get the bars from each country next to each other, instead of on top of each other?
  • Benoît Fayolle
    Benoît Fayolle about 5 years
    geom_col(aes(color=country),position = "dodge")
  • G.ven
    G.ven about 5 years
    Thank you for your help!