I am trying to do a bar plot, however, is printing multiple lines on each bar. I understand that I might have to aggregate but if I do I need to retain the original columns because I need to fill by some of them. Question: How can I get rid of the horizontal lines on each bar? Example below:
x <- structure(list(SampleDate = structure(c(19738, 19744, 19744,
19746, 19746, 19747, 19749, 19753, 19754, 19758, 19758, 19758,
19758, 19758, 19759, 19759, 19759, 19760, 19761, 19761, 19762,
19763, 19765, 19765, 19766, 19766, 19766, 19767, 19767, 19767,
19770, 19770, 19771, 19771), class = "Date"), Survey = c("early",
"late", "late", "late", "late", "late", "early", "late", "late",
"late", "late", "late", "late", "late", "late", "early", "early",
"early", "early", "early", "late", "late", "late", "late", "late",
"late", "late", "late", "early", "early", "early", "early", "early",
"early"), LifeStage = c("Adult", "Adult", "Adult", "Adult", "Adult",
"Adult", "Adult", "Adult", "Adult", "Adult", "Juvenile", "Adult",
"Adult", "Adult", "Adult", "Adult", "Adult", "unkLifeStage",
"Juvenile", "unkLifeStage", "Adult", "Adult", "Adult", "unkLifeStage",
"Adult", "unkLifeStage", "Adult", "unkLifeStage", "unkLifeStage",
"Adult", "unkLifeStage", "Juvenile", "unkLifeStage", "Juvenile"
), numb_fish = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), year = c(2024,
2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024,
2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024,
2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024
), month = c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)), row.names = c(NA,
-34L), class = c("tbl_df", "tbl", "data.frame"))
x
x$SampleDate <- as.Date(x$SampleDate,"%m/%d/%Y")
x <- x |> mutate(month = floor_date(SampleDate, "month"))
ggplot(x, aes(x = month, y = numb_fish, fill = LifeStage)) +
geom_bar(stat = "identity", color = 'black') +
labs(title = "Monthly Data",
y = "Total") +
scale_x_date(date_breaks = "1 month", date_labels = "%b-%y")
Below is the graphic that I am getting. Need to get rid of those annoying lines..
Your numb_fish
is all 1
, so we can count(.)
it ... but in case that number could be something else, we will summarize to get something more useful:
library(dplyr)
summarize(x, .by = c(month, LifeStage), n = sum(numb_fish)) |>
ggplot(aes(month, n, fill = LifeStage)) +
geom_bar(stat = "identity", color = "black") +
labs(title = "Monthly Data", y = "Total") +
scale_x_date(date_breaks = "1 month", date_labels = "%b-%y")