在ggplot2中的分组条形图上添加趋势线

时间:2020-04-11 12:07:30

标签: r ggplot2

我想在每种颜色的列上添加趋势线,如下所示: What I want to have

这可能吗?我已经尝试过使用geom_line(),但由于您需要拥有与小节点相同的点数,因此它不会让我使用。这是我当前的代码:

library(ggplot2)
library(reshape)
o2concplot <- melt(o2conctable[,c('years','mino2conc','meano2conc','maxo2conc')],id.vars = 1)

ggplot(o2concplot,aes(x = years, y = value)) + geom_bar(aes(fill = variable),stat = "identity", position = "dodge") + labs(title = "Ocean Dissolved O[2] changes 2008-2018*", x = "Year", y = "Dissolved O[2] (μmolL^-1)", caption = "*Based on data from IMOS") + scale_fill_manual(name = "Key", labels = c("Minimum", "Mean", "Maximum"), values = c("cornflower blue", "light green", "coral2")) + scale_x_continuous(breaks = c(2008, 2009, 2010, 2015, 2017, 2018))

1 个答案:

答案 0 :(得分:0)

您可以在group中尝试aes

您可以使用官方文档here

h <- ggplot(nlme::Oxboys, aes(age, height))
# A single line tries to connect all the observations
h + geom_line()

给予

enter image description here

h + geom_line(aes(group = Subject))

给予

enter image description here