我正在尝试在ggplot2中的两个单独的堆叠条形图(相同的图形)之间绘制线条,以显示第二个条形图的两个部分是第一个条形图的子集.
我曾经尝试都geom_line和geom_segment.但是,我遇到了同样的问题geom,即在同一个图中为每个(需要两行)指定一个单独的开始和停止,而不是有五行的数据帧.
没有线的图的示例代码:
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)
Run Code Online (Sandbox Code Playgroud)