Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

r - Draw lines between different elements in a stacked bar plot

I'm trying to draw lines between two separate stacked bars (same plot) in ggplot2 to show that two segments of the second bar are a subset of the first bar.

I have tried both geom_line and geom_segment. However, I have run into the same issue around designating a single start and stop for each geom (need two lines) in the same plot as a dataframe that has five lines.

Sample code of the plot without the lines:

library(data.table)
Example <- data.table(X_Axis = c('Count', 'Count', 'Dollars', 'Dollars', 'Dollars'),
                  Stack_Group = c('Purely A', 'A & B', 'Purely A Dollars', 'B Mixed Dollars', 'A Mixed dollars'),
                  Value = c(10,3, 120000, 100000, 50000))
Example[, Percent := Value/sum(Value), by = X_Axis]


ggplot(Example, aes(x = X_Axis, y = Percent, fill = factor(Stack_Group))) +
  geom_bar(stat = 'identity', width = 0.5) + 
  scale_y_continuous(labels = scales::percent)

Goal for the end plot: enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of hard-coding the start and end positions of the segments, you may grab this data from the plot object. Here's an alternative where you provide the names of the x categories and bar elements between which the lines should be drawn.

Assign the plot to a variable:

p <- ggplot() +
  geom_bar(data = Example,
           aes(x = X_Axis, y = Percent, fill = Stack_Group), stat = 'identity', width = 0.5)

Grab data from the plot object (layer_data; or ggplot_build$data[[1]] pre-ggplot2 2.0.0). Convert to data.table (setDT):

d <- layer_data(p)
setDT(d)

In the data from the plot object, the 'x' and 'group' variables are not given explicitly by their name, but as numbers. Because categorical variables are ordered lexicographically in ggplot, we can match the numbers with their names by their rank within each 'x':

d[ , r := rank(group), by = x]

Example[ , x := .GRP, by = X_Axis]
Example[ , r := rank(Stack_Group), by = x]

Join to add names of 'X_Axis' and 'Stack_Group' from original data to plot data:

d <- d[Example[ , .(X_Axis, Stack_Group, x, r)], on = .(x, r)]

Set names of x categories and bar elements between which the lines should be drawn:

x_start_nm <- "Count"
x_end_nm <- "Dollars"

e_start <- "A & B"
e_upper <- "A Mixed dollars"
e_lower <- "B Mixed Dollars"

Select relevant parts of the plot object to create start/end data of lines:

d2 <- data.table(x_start = rep(d[X_Axis == x_start_nm & Stack_Group == e_start, xmax], 2),
                 y_start = d[X_Axis == x_start_nm & Stack_Group == e_start, c(ymax, ymin)],
                 x_end = rep(d[X_Axis == x_end_nm & Stack_Group == e_upper, xmin], 2),
                 y_end = c(d[X_Axis == x_end_nm & Stack_Group == e_upper, ymax],
                           d[X_Axis == x_end_nm & Stack_Group == e_lower, ymin]))

Add line segments to the original plot:

p + 
  geom_segment(data = d2, aes(x = x_start, xend = x_end, y = y_start, yend = y_end))
 

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...