0
citetest<-data.frame(db=c("db1","db2","db3","db4","db5"), 
                     risall=c(248,134,360,122,46),
                     riscross=c(149,88,255,100,40),
                     risuniq=c(99,46,105,22,6))

head(citetest)
#>    db risall riscross risuniq
#> 1 db1    248      149      99
#> 2 db2    134       88      46
#> 3 db3    360      255     105
#> 4 db4    122      100      22
#> 5 db5     46       40       6

Created on 2022-07-29 by the reprex package (v2.0.1)

enter image description here

using the above dataframe I'm looking to develop a stacked bar chart, one for each "dbx" column. The fill colors need to align with the corresponding row.

ggplot(df, aes(x=db, y=y, fill=y))+
    geom_bar(position="stack", stat="identity")
harre
  • 7,081
  • 2
  • 16
  • 28
Halfway
  • 13
  • 2
  • Hi Halfway. Please note that `citetest` is in another format than the posted dataframe. – harre Jul 29 '22 at 12:26

1 Answers1

1

You need to get your phases-columns in a long format in order to plot them with ggplot:

library(tidyr)

citetest |> 
  pivot_longer(-db) |>
  ggplot(aes(x=db, y=value, fill=name))+
  geom_bar(position="stack", stat="identity")

enter image description here

harre
  • 7,081
  • 2
  • 16
  • 28
  • I'll review this and try to understand. This is exactly what I was looking to do. I appreciate the help! – Halfway Jul 29 '22 at 12:28