在堆叠条形图上叠加折线图可以通过使用ggplot2和dplyr这两个R语言的包来实现。下面是具体的步骤:
install.packages("ggplot2")
install.packages("dplyr")
library(ggplot2)
library(dplyr)
df_sum <- df %>%
group_by(category) %>%
summarise(total = sum(value))
barplot <- ggplot(df_sum, aes(x = category, y = total, fill = category)) +
geom_bar(stat = "identity")
lineplot <- ggplot(df, aes(x = category, y = profit, color = "Profit")) +
geom_line()
combined_plot <- barplot + lineplot
combined_plot <- combined_plot +
labs(title = "Stacked Bar Chart with Line Overlay",
x = "Category",
y = "Total",
color = "Line") +
theme_minimal()
print(combined_plot)
这样,你就可以在堆叠条形图上叠加折线图了。根据你的具体需求,你可以进一步调整图形的样式和布局。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云