Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8346203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:56:02+00:00 2026-06-09T06:56:02+00:00

Using ggplot2 generate a plot which shows the following data. df=data.frame(score=c(4,2,3,5,7,6,5,6,4,2,3,5,4,8), age=c(18,18,23,50,19,39,19,23,22,22,40,35,22,16)) str(df) df

  • 0

Using ggplot2 generate a plot which shows the following data.

df=data.frame(score=c(4,2,3,5,7,6,5,6,4,2,3,5,4,8),
              age=c(18,18,23,50,19,39,19,23,22,22,40,35,22,16))
str(df)
df

Instead of doing a frequency plot of the variables (see below code), I want to generate a plot of the average values for each x value. So I want to plot the average score at each age level. At age 18 on the x axis, we might have a 3 on the y axis for score. At age 23, we might have an average score of 4.5, and so forth (Edit: average values corrected). This would ideally be represented with a barplot.

ggplot(df, aes(x=factor(age), y=factor(score))) + geom_bar()

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

Just not sure how to do this in R with ggplot2 and can’t seem to find anything on such plots. Statistically, I don’t know if the plot I desire to plot is even the right thing to do, but that’s a different store.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:56:03+00:00Added an answer on June 9, 2026 at 6:56 am

    You can use summary functions in ggplot. Here are two ways of achieving the same result:

    # Option 1
    ggplot(df, aes(x = factor(age), y = score)) + 
      geom_bar(stat = "summary", fun = "mean")
    
    # Option 2
    ggplot(df, aes(x = factor(age), y = score)) + 
      stat_summary(fun = "mean", geom = "bar")
    

    enter image description here

    Older versions of ggplot use fun.y instead of fun:

    ggplot(df, aes(x = factor(age), y = score)) + 
      stat_summary(fun.y = "mean", geom = "bar")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to plot heatmap in ggplot2 using csv data following casbon's solution in
I'm trying to plot timeseries data using ggplot2. Specifically, I'm trying to generate a
I have the following part of a data frame which is much bigger than
I am using ggplot2 to plot simple line charts of time series data. One
I have the following graph that I generated using ggplot2 I had finalPlot as
I am using ggplot2 to plot a figure that contains nine facets. Each facet
I'm using the tips data set in ggplot2 . If I do sp =
I am using ggplot2 (respectively qplot) to generate a report with Sweave. Now I
From the documentation for ggplot2's geom_tile() function, we have the following simple plot: #
I calculated following data frame of maximimum response time of a server with pre-defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.