2

With the following code I can create the chart underneath:

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)

enter image description here

How can I now create an additional bar on top, which gives me the summary-plot of the three groups? It should ideally be in the same plot and not in a separate one

pogibas
  • 27,303
  • 19
  • 84
  • 117

1 Answers1

1

Easiest way to get additional bar in the same plot is to append data to the original dataset:

# 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))

enter image description here

pogibas
  • 27,303
  • 19
  • 84
  • 117