Reputation: 13
I'm having trouble creating a stacked bar graph with multiple gradients of colors in each bar.
Some sample data:
library(tidyverse)
library(magrittr)
df <-
data.frame(person = rep(1:3, each = 30),
drug_type = rep(rep(1:3, each = 10),3),
drug = rep(1:30,times=3),
sales_pct = rep(.033, times = 90)) %>%
as_tibble()
There are three persons
and 30 drugs
, each of which is one of 3 drug_types
.
What I would like to do is to show the each drug's fraction of each person's sales and to use color to visually segment the drugs into groups. I am trying to to this with one bar for each person
, and, within each person's bar, each drug_type
has its own color palette. So, drug_type 1
would, for example, be a spectrum of blues, 2
a spectrum of greens, etc.
Any advice? Please help!
Upvotes: 0
Views: 155
Reputation: 466
Is this kind of what you had in mind?
library(tidyverse)
library(magrittr)
df <- as.tibble(data.frame(person = rep(1:10, each = 3),
drug_type = rep(1:10, times = 3),
sales_pct = rep(.1, times = 30))) %>%
mutate(person = as.factor(person),
drug_type = as.factor(drug_type))
ggplot(df, aes(x = person)) +
geom_bar(aes(fill = drug_type)) +
scale_y_continuous(limits = c(0,3)) +
scale_fill_brewer(palette = "Spectral")
Upvotes: 0