0

I have this matrix

(enter image description here)

that I converted to data frame. But now I want to do stacked bar plot that - in the first bar will be: 4 R, 3 S and 2 T (in percentage), in the second bar will be: 31 R, 10 H....

I have trouble with how to make bar for every column..

thanks!

AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
Or shlomo
  • 1
  • 1
  • I think you dont need to convert the matrix to data.frame i.e. `barplot(yourmatrix)` should work – akrun Apr 25 '21 at 23:26
  • It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Apr 26 '21 at 01:37
  • Does this answer your question? [Making a stacked bar plot for multiple variables - ggplot2 in R](https://stackoverflow.com/questions/6693257/making-a-stacked-bar-plot-for-multiple-variables-ggplot2-in-r) – AnilGoyal Apr 26 '21 at 03:29

1 Answers1

2

Assuming your data is something like

df <- data.frame(
  V1 = rnorm(5),
  V2 = rnorm(5),
  V3 = rnorm(5),
  V4 = rnorm(5),
  row.names = sample(LETTERS, 5)
)

df
          V1         V2         V3          V4
A -0.9455398 -0.5763650  0.7669187  0.38646398
C  1.0338468  0.3420015  0.4372878  0.01919166
J  1.5157619  0.2737368  0.0161940  1.81942467
O  0.3018475  1.4323012  0.9877099 -1.54101336
P -1.7783232  0.8032201 -0.8664866  0.34663143

You can do something like this

library(tidyverse)
df %>% rownames_to_column("ID") %>% pivot_longer(!ID) %>%
  ggplot() +
  geom_col(aes(x = ID, y = value, fill = name), position = 'dodge')

to get a chart like this

enter image description here

Change position and you may a get a chart as you desire.

AnilGoyal
  • 25,297
  • 4
  • 27
  • 45