0

I have the following code

library(ggplot2)
library(dplyr)

# create data
time <- as.numeric(rep(seq(1,7),each=7))  # x Axis
value <- runif(49, 10, 100)               # y Axis
group <- rep(LETTERS[1:7],times=7)        # group, one shape per group
data <- data.frame(time, value, group)

# stacked area chart
ggplot(data, aes(x=time, y=value, fill=group)) + 
  geom_area()+
  geom_text(data = data %>% filter(time == last(time)), aes(label = group, 
                                                               x = time + 0.5, 
                                                               y = value, 
                                                               color = group)) + 
  guides(color = FALSE) + theme_bw() + 
  scale_x_continuous(breaks = scales::pretty_breaks(10))

Where i getenter image description here

But i am aiming for link

Is there any solution for stacked area plot?

Maksym Moroz
  • 306
  • 2
  • 14

3 Answers3

4

The question code is plotting the text labels in the value's of the last time, when in fact the areas are cumulative. And in reverse order.

Also, the following graph plots data created with the same code but with

set.seed(1234)

Then the data creation code is the same as in the question.

# stacked area chart
ggplot(data, aes(x=time, y=value, fill=group)) + 
  geom_area()+
  geom_text(data = data %>% 
              filter(time == last(time)) %>%
              mutate(value = cumsum(rev(value))), 
            aes(label = rev(group), 
                x = time + 0.5, 
                y = value, 
                color = rev(group))) + 
  guides(color = FALSE) + theme_bw() + 
  scale_x_continuous(breaks = scales::pretty_breaks(10))

enter image description here

Edit.

Following the discussion in the comments to this answer, I have decided to post code based on the comment by user Jake Kaupp.

ggplot(data, aes(x = time, y = value, fill = group)) + 
  geom_area()+
  geom_text(data = data %>% filter(time == last(time)),
            aes(x = time + 0.5, y = value, 
                label = rev(group), color = rev(group)),
            position = position_stack(vjust = 0.5)) + 
  guides(color = FALSE) + 
  theme_bw() + 
  scale_x_continuous(breaks = scales::pretty_breaks(10))
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • You can just use `position = position_stack()`, which makes the `mutate` and `rev` calls unnecessary. – Jake Kaupp Oct 31 '19 at 15:51
  • @JakeKaupp Thanks. That should be an answer. – Rui Barradas Oct 31 '19 at 16:24
  • @JakeKaupp did you mean something like this `(data = data %>% filter(time == last(time)) %>% # mutate(value = cumsum(rev(value))), aes(label = (group), position = position_stack(vjust = 0.5), x = time + 0.5, y = value, color = rev(group)))` – Maksym Moroz Nov 04 '19 at 15:23
  • 1
    @MaksymMoroz I have edited the question with that suggestion. – Rui Barradas Nov 04 '19 at 19:42
  • @RuiBarradas But i should say that `position = position_stack(vjust = 0.5))` doesn't work propely with for example `set.seed(342)` , this whai i've got [link](https://i.stack.imgur.com/LTmlh.png) – Maksym Moroz Nov 05 '19 at 08:23
  • @MaksymMoroz What about mine, does it work? As for `position = position_stack(vjust = 0.5))`, you will probably have to ajust the value of `vjust`, that's what I did. – Rui Barradas Nov 05 '19 at 11:40
  • @RuiBarradas yours - yes but man it can't work for one values and not for others. As i far as i understood it place letters on average level of values they represent but it is needed them to be depended on last time values. There is something wrong with `data = data %>% filter(time == last(time)),` – Maksym Moroz Nov 05 '19 at 12:22
0

You can use the text function to put text wherever you want. For example:

text(7.2, 350, "B", col="brown")
mikey
  • 1,066
  • 9
  • 17
0

Here we go

time <- as.numeric(rep(seq(1,7),each=8))  # x Axis
value <- runif(56, 10, 100)               # y Axis
group <- rep(LETTERS[1:8],times=7)        # group, one shape per group
data <- data.frame(time, value, group)
round_df <- function(x, digits) {
  # round all numeric variables
  # x: data frame 
  # digits: number of digits to round
  numeric_columns <- sapply(x, mode) == 'numeric'
  x[numeric_columns] <-  round(x[numeric_columns], digits)
  x
}

data$value<- round_df(data$value, 2)
# stacked area chart
ggplot(data, aes(x=time, y=value, fill=group)) + 
  geom_area()+
  geom_text(aes(x = time + 0.5, y = value, label=ifelse(time == max(time), group, NA)),position = position_stack(vjust = 0.5),check_overlap = TRUE)+
  guides(color = FALSE) + theme_bw()+ 
  scale_x_continuous(breaks = scales::pretty_breaks(10)) +
  geom_text(aes(label=ifelse(time != min(time) & time != max(time),value, NA)),position = position_stack(vjust = 0.5),check_overlap = TRUE)+
  geom_text(aes(x = time + 0.18,label=ifelse(time == min(time),value, NA)),position = position_stack(vjust = 0.5),check_overlap = TRUE)+
  geom_text(aes(x = time - 0.18,label=ifelse(time == max(time),value, NA)),position = position_stack(vjust = 0.5),check_overlap = TRUE)

And get enter image description here

Factor levels but why not letters? That is the next step :)

UPDATED

just converted factor to char data$group <- as.character(data$group) enter image description here

Maksym Moroz
  • 306
  • 2
  • 14