可以通过以下步骤实现:
install.packages("ggplot2")
install.packages("dplyr")
library(ggplot2)
library(dplyr)
data <- data.frame(
Category = c("A", "A", "B", "B", "C", "C"),
Subcategory = c("X", "Y", "X", "Y", "X", "Y"),
Value = c(10, 15, 8, 12, 6, 9),
ScatterX = c(1, 1, 2, 2, 3, 3)
)
data <- data %>%
group_by(Category) %>%
mutate(CumulativeValue = cumsum(Value))
barplot <- ggplot(data, aes(x = Category, y = CumulativeValue, fill = Subcategory)) +
geom_bar(stat = "identity")
barplot <- barplot +
geom_point(data = data, aes(x = ScatterX, y = Value), size = 3, color = "red")
print(barplot)
这样,你就可以在R中的堆叠条形图上成功添加散点图了。根据实际情况,你可以根据需要调整图表的样式和其他属性。
领取专属 10元无门槛券
手把手带您无忧上云