Добавление линий эволюции в сложенный барплот - PullRequest
0 голосов
/ 02 апреля 2019

Я хотел бы добавить линии на свой составной барплот, чтобы вывести эволюционную группу за группой. В настоящее время я нашел альтернативное решение с пакетом ggpubr.

Любая помощь будет признательна! Спасибо!

# My dataset     
dff
#    day composition median
# 1   JO           a  12.21
# 2   JO           b   0.79
# 3   JO           c  42.06
# 4   JO           d  30.65
# 5   JO           e   0.00
# 6  J28           a   1.28
# 7  J28           b   0.38
# 8  J28           c  85.13
# 9  J28           d   7.90
# 10 J28           e   0.00

# Stacked barplot
library(ggplot2)
ggplot(data = dff) +   
  geom_col(aes(x = day, y = median, fill = composition)) + 
  ylim(0,100)

enter image description here

# Median linked by lines
library(ggpubr)
ggpaired(data = dff,x = "day", y = "median", color = "composition",
 line.color = "gray", line.size = 0.4, xlab = FALSE, ylab = "Median")

enter image description here

Желаемое решение (или что-то подобное)

enter image description here

1 Ответ

3 голосов
/ 02 апреля 2019

Вы можете попробовать это:

w = 0.9 # this is the width of geom_col; in case you want something other than the default width

ggplot(data = dff,
       aes(x = day, y = median, fill = composition)) +   
  geom_col(width = w) +
  geom_line(aes(x = ifelse(as.integer(day) == 1, 
                           as.integer(day) + w/2,
                           as.integer(day) - w/2)),
            position = "stack")

plot

Данные:

dff <- read.table(text = "    day composition median
1   JO           a  12.21
2   JO           b   0.79
3   JO           c  42.06
4   JO           d  30.65
5   JO           e   0.00
6  J28           a   1.28
7  J28           b   0.38
8  J28           c  85.13
9  J28           d   7.90
10 J28           e   0.00")
...