logo
down
shadow

How to better create stacked bar graphs with multiple variables from ggplot2?


How to better create stacked bar graphs with multiple variables from ggplot2?

By : Patt Mont
Date : November 22 2020, 07:01 PM
I wish this help you You don't need prop.tables or count etc to do the 100% stacked bars. You just need +geom_bar(position="stack")
code :


Share : facebook icon twitter icon
Making a stacked bar plot for multiple variables - ggplot2 in R

Making a stacked bar plot for multiple variables - ggplot2 in R


By : user3299273
Date : March 29 2020, 07:55 AM
I wish did fix the issue. First, some data manipulation. Add the category as a variable and melt the data to long format.
code :
dfr$category <- row.names(dfr)
mdfr <- melt(dfr, id.vars = "category")
library(scales)
(p <- ggplot(mdfr, aes(category, value, fill = variable)) +
    geom_bar(position = "fill", stat = "identity") +
    scale_y_continuous(labels = percent)
)
how to create stacked bar charts for multiple variables with percentages

how to create stacked bar charts for multiple variables with percentages


By : kim_exe
Date : March 29 2020, 07:55 AM
wish of those help For (1)
To get percentages, you'll have to create a data.frame from melted. At least that's the way I could think of.
code :
# 200 is the total sum always. Using that to get the percentage
require(plyr)
df <- ddply(melted, .(variable, value), function(x) length(x$value)/200 * 100)
ggplot(df) + 
geom_bar(aes(variable, fill=value, weight=V1, position="fill")) +
scale_fill_manual(name="Responses",
                  values=c("#EFF3FF", "#BDD7E7", "#6BAED6",
                           "#2171B5"),
                  breaks=c("strongly disagree", 
                           "disagree", 
                           "agree", 
                           "strongly agree"),
                  labels=c("strongly disagree", 
                           "disagree", 
                           "agree", 
                           "strongly agree")) +
labs(x="Items", y="Percentage (%)", title="my title") +
coord_flip()
df$value <- factor(df$value, levels=c("strongly agree", "agree", "disagree", 
                 "strongly disagree"), ordered = TRUE)
variable.order <- names(sort(daply(df, .(variable), 
                     function(x) x$V1[x$value == "strongly agree"] ), 
                     decreasing = TRUE))
value.order <- c("strongly agree", "agree", "disagree", "strongly disagree")
df$variable <- factor(df$variable, levels = variable.order, ordered = TRUE)
df$value <- factor(df$value, levels = value.order, ordered = TRUE)
Stacked bar plot based in 4 variables with ggplot2

Stacked bar plot based in 4 variables with ggplot2


By : John Harding
Date : March 29 2020, 07:55 AM
it should still fix some issue I have a data frame like this: , You have to melt the data frame first
code :
library(data.table)
dt_melt <- melt(data, id.vars = 'nthreads')
ggplot(dt_melt, aes(x = nthreads, y = value, fill = variable)) + 
    geom_bar(stat = 'identity')
Stacked bar chart with multiple categorical variables in ggplot2 with facet_grid

Stacked bar chart with multiple categorical variables in ggplot2 with facet_grid


By : Emiliano Gamboa
Date : March 29 2020, 07:55 AM
will help you Try this, maybe not so straightforward, but it works. It includes reshaping as @aosmith suggested by gather. Then calculation of number of observations after grouping and then percentage for each group Death + habitat. Then summarized to get unique values.
code :
sampledf_edited <- sampledf %>% 
  tidyr::gather("habitat", "count", 2:4) %>% 
  group_by(Death, habitat, count) %>% 
  mutate(observation = n()) %>% 
  ungroup() %>% 
  group_by(Death, habitat) %>% 
  mutate(percent = observation/n()) %>% 
  ungroup() %>% 
  group_by(Death, habitat, count, percent) %>%
  summarize()
sampledf_edited$count <- as.factor(sampledf_edited$count)
ggplot(sampledf_edited, aes(habitat, percent, fill = count)) +  
geom_bar(stat = "identity") + 
facet_grid(~ Death)
Stacked barplot with ggplot2 depending on two variables

Stacked barplot with ggplot2 depending on two variables


By : Rajesh Bajracharya
Date : March 29 2020, 07:55 AM
hope this fix your issue You do need to melt the data (per @Gregor's comment). The reason is that to map "clicked" and "dismissed" to color, you need a single column that marks which counts come from "clicked" and which from "dismissed". Melting to long format is how you create that column.
Related Posts Related Posts :
  • How do I interpret rpart splits on factor variables when building classification trees in R?
  • Building all combinations of a vector - looking for a nicer way
  • Fetching comments from tweets of a single user in R
  • Replace space between two words with an underscore in a vector
  • Alternate tick labels in grouped ggplot to avoid overlap in R
  • NSE in tidyverse with select()
  • How to split a table by one column in R?
  • writing to a dataframe from a for-loop in R
  • How to create a data.frame with a unknown number of columns?
  • R dplyr's group_by consider empty groups as well
  • injection from R^d to [0,1]^d
  • Parsing a vector in R
  • What is 'n' column in the below output?
  • Lattice: multiple plots in one window?
  • R - If statement function to set value of new column
  • Avoiding seasonality assumption for stl() or decompose() in R
  • Cannot add new library path to .libPaths()
  • What's the higher-performance alternative to for-loops for subsetting data by group-id?
  • package ‘fpc’ is not available (for R version 3.5.3)
  • R - Convert CHR to Date
  • Get characters after and before a pattern match in R
  • Subtract even columns from odd ones in a data frame
  • Changing numbers in a string
  • Using && for character vectors in r
  • Why am I getting this New York output alone?
  • Saving H2o data frame
  • In R, when using named rows, can a sparse matrix column be added (concatenated) to another sparse matrix?
  • How to convert hourly data to 6/12/24 hourly data
  • Crosstab with multiple items
  • count words with for loop in R
  • Replace cos^..(...) by (cos(...))^
  • Is there a Pair-Wise PostHoc Comparisons for the Chi-Square Test in R?
  • How do I specify random factors in R?
  • ANOVA with 3 fixed factors in R
  • convert numbers written in words to numbers using R programming
  • In R, how can I look up a value in one column based on an interval in adjoining columns that includes values I want to m
  • Convert Date in YYYYMM to YYYY-MM-DD
  • grep does not show all the indices, only [1]1
  • Normalizing a dataframe having several conditions, R
  • What is the equivalent of "everything()" operator in "data.table"?
  • The colors in ggplot do not correspond to the colors in the legend
  • How to add to each column of dataframe value from named vector?
  • Losing data while saving csv character file
  • How to target arbitrary HTML attribute containing space in R?
  • Iterate over a column and find the sum
  • reference another column with tidyeval in filter
  • How to create a new variable by comparing two other variables
  • Retrieving elements that are occurring after adding a fixed number from a start element in a vector
  • Get total mean for variables nested in a list of lists of lists
  • Render Xaringan Rmd when using _site.yml file
  • Regex: matching multiple patterns and getting middle of string
  • removing for loop with sample function
  • R: scraping additional data after POST only works for first page
  • R - Add ticks on top of plot with levels in variable?
  • How to avoid factors in R when reading csv data
  • Meaning of :: syntax
  • How to get month and week of the month from year and week no in R?
  • Can you save R code files with .RData objects for version control?
  • How to connect points of two different plots to each other using ggplot in R?
  • Is there an R function for finding the value below specific value
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk