r - Error (ymax not found) while adding a geom_line (different data) to a multi-layered ggplot -
i'm trying generate plot summarizes dataset first plotting median & quantiles in area / black line, after want outline specific 'firm' red line.
i'd while facetting on variable, plotting multiple variables @ once.
an example code of i'd plot follows:
require(dplyr) require(ggplot2) dt <- structure(list(firm = structure(c(1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 3l, 4l, 4l, 4l, 4l, 4l, 4l), .label = c("a", "b", "c", "d"), class = "factor"), year = c(2008l, 2009l, 2008l, 2009l, 2008l, 2009l, 2008l, 2009l, 2008l, 2009l, 2008l, 2009l, 2008l, 2009l, 2008l, 2009l, 2008l, 2009l, 2008l, 2009l, 2008l, 2009l, 2008l, 2009l), variable = structure(c(1l, 1l, 2l, 2l, 3l, 3l, 1l, 1l, 2l, 2l, 3l, 3l, 1l, 1l, 2l, 2l, 3l, 3l, 1l, 1l, 2l, 2l, 3l, 3l), .label = c("var1", "var2", "var3" ), class = "factor"), value = c(0.991894223, 2.216322113, 3.189415462, 0.663732077, 0.444826423, 2.674568191, 1.272077011, 7.691464914, 4.263339855, 0.214415839, 3.995328653, 6.028747322, 8.191459456, 0.16205906, 4.056495056, 5.17994524, 0.42435417, 0.678655669, 6.246411921, 0.505532339, 4.65045746, 8.85141854, 5.850616048, 2.028583225)), .names = c("firm", "year", "variable", "value" ), class = "data.frame", row.names = c(na, -24l)) head(dt) firm year variable value 1 2008 var1 0.9918942 2 2009 var1 2.2163221 3 2008 var2 3.1894155 4 2009 var2 0.6637321 5 2008 var3 0.4448264 6 2009 var3 2.6745682 i manually calculate ymin, ymax, , y ribbon / line plots. they're plotting fine.
dt_aggregates <- dt %>% group_by(variable, year) %>% arrange(variable, year) %>% summarize(y=median(value)) dt_aggregates$ymin <- 0.9*dt_aggregates$y dt_aggregates$ymax <- 1.1*dt_aggregates$y this firm want highlight:
dt_focus <- filter(dt, firm=="a") the following plots fine, , want.
g <- ggplot(data=dt_aggregates, aes(x=year, y=y, ymax=ymax, ymin=ymin, group=variable)) + facet_grid(variable~., scales="free") + geom_line() + geom_ribbon(alpha=0.3) + theme_bw() however, want add line (for 1 firm) onto (in red). once try add new line separate dataframe, following error. on getting work appreciated
# error in eval(expr, envir, enclos) : object 'ymax' not found g + geom_line(data=dt_focus, aes(x=year, y=value, group=variable), col="red", size=2)
Comments
Post a Comment