The expression 代わりに is used to express the idea of substitution, replacement, or exchange. It has three main usages depending on the context.
When attached to a verb or noun, 代わりに indicates doing one thing instead of another, or someone/something acting in place of someone/something else.
With nouns: Noun + の + 代わりに
With verbs (dictionary form): Verb + 代わりに
This usage can also describe someone acting as a substitute for another person:
代わりに can express a trade-off – one thing is given or done in return for another. The first clause states what was received or gained, and the second states what was given or done in exchange.
代わりに can connect two contrasting qualities, expressing that while something has one characteristic, it also has a compensating or offsetting one. This works with adjectives as well.
Adj-i + 代わりに / Adj-na + な + 代わりに