-1

This is not a duplicate question, but it is based on: Stacked bar chart

I am trying to use the accepted answer by agstudy. I have the following dataframe:

  types   c1      c2  c3  c4   c5  c6
       A   20     2   6   1    16  1
       B   15     1   7   1    7   1
       C   7      5   3   0    8   3
       D   5      7   4   7    6   4
       F   6      6   6   2    5   6
       E   17     8   2   3    4   9


tbl<-melt(tbl,id.vars="types")
ggplot(tbl,aes(x=types,y=value,fill=variable))+geom_bar(stat='identity')

This is a simple way to create a stacked bar chart. I originally had an issue about stacking cause of the melt().

My issue comes with stacking the data to get all the values on top of each in one column per row. It would not stack because I had the wrong names and I did not understand the output of melt. But now that I do understand the melt function, it splits up the data and groups it over the id.vars. by doing so you can create a graph, such that the fill aspect of the graph will be the variable where it fills in the bar with the values in the value column from melt. This is interpreted by R as a stacked bar chart.

Jack Armstrong
  • 1,182
  • 4
  • 26
  • 59
  • . Have you changed the column names in the `melt` output? In the `ggplot` a different object is used `trans_tbl`. By default, the `melt` returns the column names as value and variable (if you haven't changed those names). If you look at `?reshape2::melt` `melt(data, ..., na.rm = FALSE, value.name = "value")` – akrun Jul 10 '18 at 03:07
  • You can change the column names with `reshape2::melt(tbl, id.var = "types", variable.name = "buys", value.name = "total")` – akrun Jul 10 '18 at 03:11
  • What do you mean change the columns in the melt output? – Jack Armstrong Jul 10 '18 at 03:11
  • I meant change the column names that you want in the long format. By default, it is `variable` and `value` – akrun Jul 10 '18 at 03:12
  • I would prefer to use the defult method instead of changing the column names – Jack Armstrong Jul 10 '18 at 03:12
  • unable to reproduce, tbl doesn't have total ,buys, loans, lbs, free, pro, und columns which are used in your code – TC Zhang Jul 10 '18 at 03:15
  • @TCZhang sorry, fixed. – Jack Armstrong Jul 10 '18 at 03:18
  • 2
    Have you looked at your data after melting? You don't have columns named `total` or `c1`. In a `ggplot` `aes` function, you're mapping variables that are in your data to aesthetics such as x position or fill color, so `y = value` means that y values are set in the plot based on the `value` column of your data – camille Jul 10 '18 at 03:18

1 Answers1

2

The melt by default changes the 'wide' format to 'long' format and create two columns 'variable' and 'value' for the column names and the values corresponding to that columns in the 'wide' format accordingly. The usage, according to ?reshape2::melt is

melt(data, ..., na.rm = FALSE, value.name = "value")

We could change the value.name and variable.name to different names (if needed).

newtbl <- reshape2::melt(tbl, id.var = "types")

It is better to have a look at the data before doing the plotting with either head

head(newtbl, 2)
#   types variable value
#1     A       c1    20
#2     B       c1    15

or names (or colnames - here it is a data.frame, so names would work as well)

names(newtbl) 
#[1] "types"    "variable" "value"   

or to check the structure with str

str(newtbl)
#'data.frame':  36 obs. of  3 variables:
# $ types   : chr  "A" "B" "C" "D" ...
# $ variable: Factor w/ 6 levels "c1","c2","c3",..: 1 1 1 1 1 1 2 2 2 2 ...
# $ value   : int  20 15 7 5 6 17 2 1 5 7 ...

In the other post, they used exactly these column names as argument in the ggplot

ggplot(newtbl,aes(x=types,y=value,fill=variable)) +
                     geom_bar(stat='identity')
akrun
  • 874,273
  • 37
  • 540
  • 662