Code issue in R - Error in 'geom_point()'

My code chunk runs on markdown, but when I try to knit my markdown it gives me this error:

Error in geom_point():
! Problem while setting up geom.
:information_source: Error occurred in the 1st layer.
Caused by error in compute_geom_1():
! geom_point() requires the following missing aesthetics: y
Backtrace:

  1. rmarkdown::render(...)
  2. knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
  3. knitr:::process_file(text, output)
  4. knitr:::process_group.block(group)
  5. knitr:::call_block(x)
    ...
  6. ggplot2:::ggplot_build.ggplot(x)
  7. ggplot2:::by_layer(...)
  8. ggplot2 (local) f(l = layers[[i]], d = data[[i]])
  9. l$compute_geom_1(d)
  10. ggplot2 (local) compute_geom_1(..., self = self)
    Execution halted

Here is my code:

if (!require("ggplot2")) install.packages("ggplot2")
library(ggplot2)

aa_score <- (ghana_map$"Aedes aegypti score")

ggplot(ghana_map, aes(x = HDI, y = aa_score, color = Water_access)) +
  geom_point(size = 1, stroke = 0.5) +  
  geom_hline(yintercept = 0, color = "black", linetype = "solid") +  
  geom_vline(xintercept = 0.5, color = "black", linetype = "solid") +  
  labs(x = "HDI", y = "Aedes Aegypti Score", color = "Water Access") +
  theme_minimal() +
  theme(
    axis.title.x = element_text(face = "bold"),  
    axis.title.y = element_text(face = "bold"),
    legend.position = "top",  
    legend.direction = "horizontal",  
    legend.box.background = element_rect(color = "black", size = 0.5), 
    legend.title.align = 0.5, 
    legend.text.align = 0.5  
  ) +
  guides(color = guide_legend(override.aes = list(fill = "white")))


I I run this with a fake dataset on my system, (R version 4.3.3 (2024-02-29)
Platform: aarch64-apple-darwin20 (64-bit) Running under: macOS Sonoma 14.4.1ggplot2_3.5.0 ) I get a few warnings, but it still works:

Warning messages:
1: The `size` argument of `element_rect()`
is deprecated as of ggplot2 3.4.0.
ℹ Please use the `linewidth` argument
  instead.
This warning is displayed once every 8
hours.
Call
`lifecycle::last_lifecycle_warnings()`
to see where this warning was generated. 
2: The `legend.title.align` argument of
`theme()` is deprecated as of ggplot2
3.5.0.
ℹ Please use theme(legend.title =
  element_text(hjust)) instead.
This warning is displayed once every 8
hours.
Call
`lifecycle::last_lifecycle_warnings()`
to see where this warning was generated. 
3: The `legend.text.align` argument of
`theme()` is deprecated as of ggplot2
3.5.0.
ℹ Please use theme(legend.text =
  element_text(hjust)) instead.
This warning is displayed once every 8
hours.
Call
`lifecycle::last_lifecycle_warnings()`
to see where this warning was generated. 
1 Like

Are you in a position to share data? It'll be difficult to troubleshoot without more context.

figured it out now, how do i close the topic?

You should be able check the solution box on the post you just made.

1 Like

This topic was automatically closed 7 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.