在堆积条形图中的不同元素之间绘制线条 [英] Draw lines between different elements in a stacked bar plot
问题描述
我试图在ggplot2中的两个单独的堆积条(相同的图)之间绘制线,以显示第二个条的两个部分是第一个条的子集。
我尝试了 geom_line
和 geom_segment
。然而,我在同一个图中为每个 geom
指定一个单独的开始和结束符作为一个数据框,它有五行。 / b>
没有行的情节示例代码:
library(data .table)
示例< - data.table(X_Axis = c('Count','Count','Dollars','Dollars','Dollars'),
Stack_Group = c('Purely A','A'和B'','纯粹A美元','B混合美元','混合美元'),
价值= c(10,3,120000,100000,50000))
示例[,Percent:= Value / sum(Value),by = X_Axis]
ggplot(示例,aes(x = X_Axis,y = Percent,fill = factor(Stack_Group ))+
geom_bar(stat ='identity',width = 0.5)+
scale_y_continuous(labels = scales :: percent)
结束图的目标:
您可以从plot对象中获取这些数据,而不是对段的开始和结束位置进行硬编码。这里有一个替代方案,您可以在其中提供应在其间绘制线条的x类别和栏元素的名称。
将图表分配给一个变量:
p < - ggplot()+
geom_bar(data = Example,
aes(x = X_Axis,y = Percent,fill = Stack_Group),stat ='identity',width = 0.5)
从plot对象中获取数据( ggplot_build
)。转换为 data.table
( setDT
):
d < - ggplot_build(p)$ data [[1]]
setDT(d)
在来自plot对象的数据中,'x'和'group'变量并不是通过它们的名字明确给出的,而是作为数字给出的。由于分类变量按照字典顺序排列在 ggplot
中,因此我们可以通过在每个x中的 rank
':
d [,r:= rank(group),by = x]
示例[,x:= .GRP,by = X_Axis]
示例[,r:= rank(Stack_Group),by = x]
加入从原始数据中添加'X_Axis'和'Stack_Group'的名称以绘制数据:
d < - d [例子[,。(X_Axis,Stack_Group,x,r)],on =。(x,r)]
设置应在其间绘制线条的x类别和栏元素的名称:
<$ c $计算
x_end_nm < - 美元
e_start < - A和B
e_upper < - A混合美元
e_lower< - B混合美元
选择绘图对象的相关部分创建行的开始/结束数据:
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]))
将线段添加到原始图中:
p +
geom_segment(data = d2,aes(x = x_start,xend = x_end,y = y_start,yend = y_end))
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:
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 (ggplot_build
). Convert to data.table
(setDT
):
d <- ggplot_build(p)$data[[1]]
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))
这篇关于在堆积条形图中的不同元素之间绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!