在堆叠条形图的各个类别旁边显示一个摘要栏
Display a summary bar next to the individual categories of a stacked barchart
使用以下代码我可以在下方创建图表:
library(ggplot2)
ds <- as.data.frame(mtcars)
ds$gear <- as.factor(ds$gear)
ds$carb <- as.factor(ds$carb)
d1 <- ds[ds$carb %in% c("1","2","3"),]
ggplot(d1, aes(fill=gear, x=carb)) +
coord_flip() +
geom_bar(position=position_fill(reverse=TRUE), width=0.7)
我现在如何在顶部创建一个额外的条形图,它提供了三个组的摘要图?
理想情况下,它应该在同一个地块中,而不是在单独的地块中
在同一图中获取额外条形的最简单方法是将数据附加到原始数据集:
# Create new category "Summary" and combine with the original dataset
pd <- rbind(
d1[, c("gear", "carb")],
data.frame(carb = "Summary", gear = d1$gear)
)
# Plot combined dataset
ggplot(pd, aes(carb, fill = gear)) +
coord_flip() +
geom_bar(position = position_fill(reverse = TRUE))
使用以下代码我可以在下方创建图表:
library(ggplot2)
ds <- as.data.frame(mtcars)
ds$gear <- as.factor(ds$gear)
ds$carb <- as.factor(ds$carb)
d1 <- ds[ds$carb %in% c("1","2","3"),]
ggplot(d1, aes(fill=gear, x=carb)) +
coord_flip() +
geom_bar(position=position_fill(reverse=TRUE), width=0.7)
我现在如何在顶部创建一个额外的条形图,它提供了三个组的摘要图? 理想情况下,它应该在同一个地块中,而不是在单独的地块中
在同一图中获取额外条形的最简单方法是将数据附加到原始数据集:
# Create new category "Summary" and combine with the original dataset
pd <- rbind(
d1[, c("gear", "carb")],
data.frame(carb = "Summary", gear = d1$gear)
)
# Plot combined dataset
ggplot(pd, aes(carb, fill = gear)) +
coord_flip() +
geom_bar(position = position_fill(reverse = TRUE))