Stacked Bar Chart for Count Data

Hi Everyone,

I wondered if someone could help with a re-labelling 'fill' variables ' and the x-axis on a stacked bar chart please?

My ambition is to:

  • Express the fill variable(s) as of six domains in the health survey, titled ' MR, Mob, SC, Act, Pain, Anx')
  • And have the x-variable representing the 'health states' (0,1, 2, 3,4, 5), rather than X0, X1....X5
library(ggplot2)
library(dplyr)
library(reshape2)
library(viridis)
library(hrbthemes)
library(readxl)

data <- read.table(text = "0	1	2	3	4	5
MR	155	211	64	14	1	1
Mob	0	393	51	2	0	0
SC	0	427	12	7	0	0
Act	0	386	45	15	0	0
Pain	0	379	62	5	0	0
Anx	0	355	73	18	0	0", header = TRUE)


data$row <- seq_len(nrow(data))
data2 <- melt(data, id.vars ="row")

ggplot(data2, aes(x = variable, y = value, fill = row)) + 
  geom_bar(stat = "identity") +
  xlab("\nHealthState") +
  ylab("Count\n") +
  theme_bw()

Stacked

Would appreciate all feedback.

I am not absolutely sure I got it but give this a try. I renamed the data frame to dat1. The name data is a reserved word in R.

names(dat1) <-   rownames(dat1)
dat1$nams <-  rownames(dat1)

mdat1 <-   melt(dat1, id.vars = "nams")

ggplot(mdat1, aes(x = variable, y = value, fill = nams)) + 
  geom_bar(stat = "identity")

Thanks John,

Much appreciated :slight_smile:

I replicated your commands to obtain the following graph:Graph

Is it somehow possible to maintain the fill detail as the same, yet change the variable on the x-axis to the levels health status (0,1,2,3,4,5)?

Sounds unusual, but I am looking to identify the number of counts of (MR, Mob.... Anx) associated with a certain health status/score (0,1....5).

Appreciate your time and feedback.

I misunderstood what you were saying about the x-axis labelling.

I think this gets it although I am not all that sure about how elegant it is.

library(ggplot2)
library(dplyr)
library(reshape2)

dat1 <- read.table(text = "0	1	2	3	4	5
MR	155	211	64	14	1	1
Mob	0	393	51	2	0	0
SC	0	427	12	7	0	0
Act	0	386	45	15	0	0
Pain	0	379	62	5	0	0
Anx	0	355	73	18	0	0", header = TRUE)


names(dat1) <-   rownames(dat1)
dat1$nams <-  rownames(dat1)

mdat1 <-   melt(dat1, id.vars = c("nams"))

mdat1$health <-   rep(c(1:nrow(dat1)), each = nrow(dat1))

ggplot(mdat1, aes(x = variable, y = value, fill = nams)) + 
  geom_bar(stat = "identity")

Thanks again John. Truly appreciate your time.

The fill you specified is 100 percent correct; but I think the x-axis is incorrect?

I am trying to specify the x-axis in terms of the health states (health related quality of life on a scale of 0-5).

Therefore, a graph with the x-axis as follows, but with the fill the same as your own, where the fill represents the 6 different variables (MR....Anx)?


dat <- read.table(text = "HRQL-0	HRQL-1	HRQL-2	HRQL-3	HRQL-4 HRQL-5
MR	155	211	64	14	1	1
Mob	0	393	51	2	0	0
SC	0	427	12	7	0	0
Act	0	386	45	15	0	0
Pain	0	379	62	5	0	0
Anx	0	355	73	18	0	0", header = TRUE)

dat$row <- seq_len(nrow(dat))
dat2 <- melt(dat, id.vars = "row")

ggplot(dat2, aes(x = variable, y = value, fill = row)) + 
  geom_bar(stat = "identity") +
  xlab("\nHRQL Index") +
  ylab("Counts\n") +
  theme_bw()

HRQL

Oh, now I think I see what you are doing. I was dismissing the original names as some weird data layout.

I have to go out now but I should be able to have a look later or tomorrow. I think I see a solution.

Thank you, much appreciated

Is this what you mean?

library(tidyverse)

dat <- read.table(text = "0 1   2   3   4   5
MR  155 211 64  14  1   1
Mob 0   393 51  2   0   0
SC  0   427 12  7   0   0
Act 0   386 45  15  0   0
Pain    0   379 62  5   0   0
Anx 0   355 73  18  0   0", header = TRUE)

dat %>% 
    rownames_to_column("domains") %>% 
    gather(health_state, count, -domains) %>% 
    mutate(health_state = factor(str_extract(health_state, "\\d$"))) %>% 
    ggplot(aes(x = health_state, y = count, fill = domains)) +
    geom_col() +
    xlab("\nHRQL Index") +
    ylab("Counts\n") +
    theme_bw()

Created on 2021-04-29 by the reprex package (v2.0.0)

Yes,

Thank you very much Andres.

Thank you too, John. I appreciate everybody's contribution.

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.