-1

I want to create a stacked bar chart in R such that it shows the sum of levels of a feature over time. The feature is of type factor, "char", with levels A, B, H, N, P, U, W. Date feature is type date.

Example data from "chart_df":

char date
w 2022-04-09
w 2022-04-07
b 2022-04-06
n 2022-04-05
b 2022-04-03
b 2022-04-03

I'm a total beginner. I've tried y= count(), sum(), summarize() with no luck. I've even tried to group by month in hopes that cleaned it up, but it didn't help. I've used this as my guide: https://r-graph-gallery.com/136-stacked-area-chart.html

I can't figure out how to sum the number of chars for a given date(for ex, "b" would have 2 for 2022-04-03). Below is where I'm at so far but it looks awful: enter image description here

library(tidyverse)
library(plotly)
library(ggplot2)
library(viridis)
library(hrbrthemes)
p <- chart_df %>% 
  ggplot( aes(x=date, y = frequency(char), fill=char, text=char)) +
  geom_area() + 
    scale_fill_viridis(discrete = TRUE) +
  theme(legend.position="none") +
  theme_ipsum() +
  theme(legend.position="top")

# Turn it interactive
p <- ggplotly(p, tooltip="text")
p

I'd like to create a nice, clear and understandable stacked bar chart showing amounts of char for each day over time. Thank you.

Sercan
  • 4,739
  • 3
  • 17
  • 36
Angie
  • 1
  • `table(chart_df$char)` will count but you don't have to, `geom_bar` will do it automatically: `ggplot(chart_df, aes(char))+geom_bar()`. – Rui Barradas Aug 08 '22 at 05:20

2 Answers2

0

One option would be to use stat="count" in geom_area (and drop the y aes):

library(ggplot2)
library(plotly)
library(viridis)
library(hrbrthemes)

chart_df$date <- as.Date(chart_df$date)

p <- ggplot(chart_df, aes(x = date, fill = char, text = char)) +
  geom_area(stat = "count") +
  scale_fill_viridis(discrete = TRUE) +
  theme(legend.position = "none") +
  theme_ipsum() +
  theme(legend.position = "top")

ggplotly()

Or as a second option you could compute the counts manually using e.g. dplyr::count:

library(dplyr)

chart_df_agg <- chart_df %>%
  count(date, char, name = "count")

p <- ggplot(chart_df_agg, aes(x = date, y = count, fill = char, text = char)) +
  geom_area() +
  scale_fill_viridis(discrete = TRUE) +
  theme(legend.position = "none") +
  theme_ipsum() +
  theme(legend.position = "top")

ggplotly()

DATA

chart_df <- data.frame(
  stringsAsFactors = FALSE,
  char = c("w", "w", "b", "n", "b", "b"),
  date = c(
    "2022-04-09", "2022-04-07",
    "2022-04-06", "2022-04-05", "2022-04-03", "2022-04-03"
  )
)
stefan
  • 90,330
  • 6
  • 25
  • 51
0

Thanks, everyone. The " y = count" tip was super helpful.

1

I figured it out using the lubridate library (good for date stuffs):

s <- chart_df_agg %>% 
  ggplot(aes(x= chart_df_agg$`year(chart_df$date)`, y = char_count,
              fill=char, text=char)) +
  geom_area(size= 0.1, colour="black") + 
  scale_fill_viridis(discrete = TRUE) +
  theme(legend.position="none") +
  theme_ipsum() +
  theme(legend.position="top")

# Turn it interactive
s <- ggplotly(s, tooltip="text")
s

vimuth
  • 5,064
  • 33
  • 79
  • 116
Angie
  • 1