giacomoV - 1 year ago 100

R Question

By default

`ggplot2`

`a`

`b`

`c`

What I would like to produce a graph where the order changes depending on the changing proportions (

`%`

So,

`x-axis`

`%`

`y-axis`

You can visualise the table like this :

`code color `1` `2` `3``

1 a red 0.2 0.8 0.2

2 b blue 0.8 0.0 0.2

3 c yellow 0.0 0.2 0.6

The plot is ordered now according to the proportion. This enables to visualise more easily the hierarchy between the states over time. First

`blue`

`red`

`yellow`

However, I am not quite sure how to do it.

I tried using

`order`

`order(n)`

`td %>% group_by(time) %>% mutate(ord = order(n, decreasing = T))`

and to explicitly provide the position to

`ggplot`

but it doesn't seem to work.

`t %>%`

count(time, code) %>% group_by(time) %>% mutate(n = n / sum(n)) %>%

ggplot(aes(time, n, fill = code)) + geom_bar(stat = 'identity') +

scale_fill_manual(values = c('red', 'blue', 'yellow')) +

scale_x_discrete(limits = factor(td$ord)) # positions #

`t = structure(list(id = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,`

3L, 4L, 4L, 4L, 5L, 5L, 5L), .Label = c("1", "2", "3", "4", "5"

), class = "factor"), code = structure(c(2L, 1L, 1L, 2L, 3L,

3L, 2L, 1L, 3L, 2L, 1L, 3L, 1L, 1L, 2L), .Label = c("a", "b",

"c"), class = "factor"), time = structure(c(1L, 2L, 3L, 1L, 2L,

3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("1", "2",

"3"), class = "factor")), .Names = c("id", "code", "time"), row.names = c(NA,

-15L), class = "data.frame")

Answer Source

You could simply order your `data.frame`

according to column `n`

, before calling `ggplot()`

:

```
t %>%
count(time, code) %>% group_by(time) %>% mutate(n = n / sum(n)) %>%
arrange(desc(n)) %>% # only need to add this line
ggplot(aes(time, n, fill = code)) + geom_bar(stat = 'identity') +
scale_fill_manual(values = c('red', 'blue', 'yellow'))
```