stacked Bar plot displaying values

my dataframe

tata4 <- data.frame(Subtype = c("Prostate", "Oesophagus", "Lung"),  
alive = c(88, 22, 100), dead = c(12, 55, 17), uncertain = c(10, 2, 2), total = c(186,46,202)) 

what I want is this - A stacked barplot for each of the 'prostate', 'oesophagus and Lung. The columns should be subdivided vertically into the number alive,dead and uncertain according to each cancer. (with each bar displaying the total).

I'm struggling to find the right codes. I can't seem to find a way of making the 'fill' function the values of each tumour site.

Can someone please help.

What have you tried so far? what is your specific problem?, we are more inclined towards helping you with specific coding problems rather than doing your work for you.

Could you please turn this into a self-contained REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

No worries, I had a bash. Here is my dataframe


tata3 <- data.frame( stringsAsFactors = TRUE, Subtype = c("lung", "prostate", "oesophagus"),  
alive = c(88, 22, 100), dead = c(12, 55, 17), uncertain = c(10, 2, 2), total = c(186,46,202))

Then remove the 'Total' column using subset function, as this is superfluous.


tata3 <- subset(tata3, select= -c(total))

The convert data into long from wide by this code


tata3_long <- tata3

tata3_long$Subtype <- as.factor(1:nrow(tata3_long))

tata3_long <- melt(tata3_long, id.vars = "Subtype")

Then the following code to get the plot


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

The only problem I am now left with is how to make the Subtype appear on the X Axis (i.e. Lung, Prostate, Oesophagus) rather than the numbers (1,2,3,). I think it is something to do with the


as.factor(1:nrow(tata3_long)

But I cannot modify this line of code successfully. Plot attached.
stacks
Cheers.

Think I might actually have worked in out.

data_long1 <- melt(tata3,id.vars = c("Subtype"))

better way of changing data to long from wide
and then the following code

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

I renamed the lung to HPC

Rplot6

The reshape2::melt() function and more generally the reshape2 package has long been superseded by the tidyr package, the modern equivalent would be the tidyr::pivot_longer() function, consider this alternative solution:

# Library calls
library(tidyverse)

# Sample data in a copy/paste friendly format
tata3 <- data.frame(
    stringsAsFactors = TRUE,
    Subtype = c("lung", "prostate", "oesophagus"),
    alive = c(88, 22, 100),
    dead = c(12, 55, 17),
    uncertain = c(10, 2, 2),
    total = c(186, 46, 202))

# Relevant code
tata3 %>% 
    select(-total) %>% 
    pivot_longer(cols = -Subtype,
                 names_to = "variable",
                 values_to = "value") %>%
    mutate(Subtype = str_replace(Subtype, "lung", "HPC")) %>% 
    ggplot(aes(x = Subtype, y = value, fill = variable)) +
    geom_col()

Created on 2022-03-02 by the reprex package (v2.0.1)

Also, please be aware that your "reprex" is missing library calls, it is an important part of a reproducible example so next time please try to make a proper self-contained reproducible example and ideally run the code before posting so you can make sure it is functional.

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.