0

using the data below, I would like to create a stacked bar plot as described here. However, I got errors like

Error in `geom_col()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `tapply()`:
! argument "X" is missing, with no default
Run `rlang::last_trace()` to see where the error occurred.

Here is my data:

    graph_data <- structure(list(
  age = structure(c(52, 52, 52, 52, 52, 52), format.stata = "%8.0g"),
  race = structure(c(1L, 1L, 2L, 2L, 3L, 3L), levels = c("0.White", "1.Black", "2.Hispanic"), class = "factor"),
  TLE = structure(c(27.6, 27.6, 29.7, 29.7, 31, 31), label = "r(mean)", format.stata = "%10.0g"),
  var = structure(c(2L, 1L, 2L, 1L, 2L, 1L), levels = c("Unhealthy Life Expectancy", "Healthy Life Expectancy"), class = "factor"),
  value = c(17, 7.3, 17.7, 10.6, 22.3, 13.3),
  label_pos = c(8.5, 20.65, 8.85, 23, 11.15, 28.95),
  label_text = c("17.0", " 7.3", "17.7", "10.6", "22.3", "13.3"),
  tle_pos = c(26.3, 26.3, 30.3, 30.3, 37.6, 37.6),
  tle_label = c("27.6", "27.6", "29.7", "29.7", "31.0", "31.0")
), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L))
Nader Mehri
  • 514
  • 1
  • 5
  • 21

1 Answers1

1
library(tidyverse)

graph_data %>% 
ggplot(aes(fill=var, y=value, x=race)) + 
    geom_bar(position="stack", stat="identity")

stacked bar chart It depends specifically what you want to do, but you can modify the code above and get what you want

Mark
  • 7,785
  • 2
  • 14
  • 34