Facet Wrap in r barplot and line
Facet Wrap in r barplot and line
I'm dealing with R
and ggplot
and I have this dataframe:
R
ggplot
> Results
V NetStateCosts DeltaCosts CostPerTon
1 BAU 6.071476 -2.060885e-04 0.0000
2 ENT 56.549041 -5.165512e+01 512.2071
3 FIN -126.879230 6.180334e+01 -277.3276
4 TWC -24.398330 1.591942e+01 -240.2432
5 FITAX -118.786530 2.514306e+01 -210.3841
6 FITWC -214.627979 1.210538e+02 -383.4240
7 TWCTAX 26.490005 -3.510108e+01 240.9295`
I'm trying to plot a barplot
for the variables NetStateCosts
and DeltaCosts
(column V represents different scenarios) but then I want to add a second graph, using facet wrap, that plot over the other one a line built using CostPerTons
y value (but x is the same (V column)).
barplot
NetStateCosts
DeltaCosts
CostPerTons
Final plot should be something like this
However I'm struggling using facet_wrap
to do that. Any ideas?
facet_wrap
Here is what I was able to do but I don't know how to add the other plot:
Results.tidy <- Results %>%
gather(D, d_val, NetStateCosts:DeltaCosts) %>%
gather(CS, c_val, CostPerTon)
barRes <- ggplot(Results.tidy, aes(x=V))
barRes +
geom_bar(aes(y=d_val,fill=D),stat="identity",position="dodge")
facet_wrap()
ggplotGrob()
grid.arrange()
gridExtra
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
facet_wrap()
is for facetting the same plot, not combining multiple types of plots. Check outggplotGrob()
andgrid.arrange()
in thegridExtra
package for combining your graphs. gridExtra CRAN page– Jan Boyer
12 hours ago