Problems using ggplot 2: I installed and retrieved the tidyverse package, but for some reason I am getting an error where r-studio is saying that it doesnt recognize the package (even though I installed it with tidyverse). Any advice?

library(tidyverse)

insurance <-read_csv("C:/Users/sfkel/Downloads/insurance.csv")
insurance

#Display summary statistics for the age and charges column

mean_charges_by_age <- insurance %>% group_by(age) %>% summarize(mean_charges = mean(charges))
mean_charges_by_age

#As age increases, charges tend to also increase, but charges are more spread out and varied than the age column.

#People per region

insurance %>%
group_by(region) %>%
summarize(count = n ())

#Answer: NE = 324 NW = 325 SE = 364 SW = 325

How are average medical charges related to age

ggplot2(data = insurance, aes(x = age, y = mean_charges_by_age) +
geom_line())

H,. welcome to the forum.

The command is ggplot() not ggplot2().


ggplot(data = insurance, aes(x = age, y = mean_charges_by_age) +
geom_line())

Should work

I also tried that and got the following error:

#Error in ggplot():
#! mapping should be created with aes().
#:heavy_multiplication_x: You've supplied a object
#Run rlang::last_trace() to see where the error #occurred.

ggplot(data = insurance, aes(x = age, y = mean_charges_by_age) +
geom_line())

OOPs, I was not reading that carefully enough. Try

ggplot(data = insurance, aes(x = age, y = mean_charges_by_age)) +
         geom_line()

I think you had a ) in the wrong place.

Thank you so much for your help lol. I am unfortunatley still getting an error with the aesthetics:

Error in geom_line():
! Problem while computing aesthetics.
:information_source: Error occurred in the 1st layer.
Caused by error in check_aesthetics():
! Aesthetics must be either length 1 or the same as the data (1338)
:heavy_multiplication_x: Fix the following mappings: y
Run rlang::last_trace() to see where the error occurred.

rlang::last_trace()
<error/rlang_error>
Error in geom_line():
! Problem while computing aesthetics.
:information_source: Error occurred in the 1st layer.
Caused by error in check_aesthetics():
! Aesthetics must be either length 1 or the same as the data (1338)
:heavy_multiplication_x: Fix the following mappings: y


Backtrace:

  1. ├─base (local) <fn>(x)
  2. └─ggplot2:::print.ggplot(x)
  3. ├─ggplot2::ggplot_build(x)
  4. └─ggplot2:::ggplot_build.ggplot(x)
  5. └─ggplot2:::by_layer(...)
    
  6.   ├─rlang::try_fetch(...)
    
  7.   │ ├─base::tryCatch(...)
    
  8.   │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
    
  9.   │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
    
  10.   │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
    
  11.   │ └─base::withCallingHandlers(...)
    
  12.   └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
    
  13.     └─l$compute_aesthetics(d, plot)
    
  14.       └─ggplot2 (local) compute_aesthetics(..., self = self)
    
  15.         └─ggplot2:::check_aesthetics(evaled, n)
    

it could just be something weird, but if you can think of anything please let me know!

We have your programme but I think we need to see some sample data.

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here. between
```
```

The mean_charges_by_age data frame should have two columns, one with the age groups and one with mean_charges for each age group. You should use data = mean_charges_by_age instead of data = insurance. You should also use mean_charges as the Y variable.

An example:

library(tidyverse)

mean_mpg_by_cyl <- mtcars |> group_by(cyl) |> summarize(mean_mpg = mean(mpg))
mean_mpg_by_cyl
#> # A tibble: 3 × 2
#>     cyl mean_mpg
#>   <dbl>    <dbl>
#> 1     4     26.7
#> 2     6     19.7
#> 3     8     15.1
ggplot(data = mean_mpg_by_cyl, aes(cyl, mean_mpg)) + geom_line()

image

Created on 2023-10-07 with reprex v2.0.2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.