Could anyone help me to add a second y-axis in ggplot or alternatively combine the two separate ggplots I have made? (R-code attached)
The data: Dataframe = Deals1 include three columns (Year = Date, Number of transactions each year = N, and total transaction value each year = total_tvalue).
The dataset includes 22 rows (Year 2000-2021), number og transactions varies from 50-500 and transaction value varies from 100.000 to 800.000
Thanks!
# Two seperate plots
plot1 = ggplot(Deals1, aes(x=Date, y=N)) + geom_bar(stat = "identity")
plot2 = ggplot(Deals1, aes(x=Date, y=total_tvalue, group = 1)) + geom_line(stat = "identity")
# Doesnt work
ggplot(Deals1, aes(x=Date)) +
geom_bar( aes(y=N), stat = "identity") +
geom_line( aes(y=total_tvalue))+
scale_y_continuous(
name = "Number of transactions",
sec_axis(name = "Transaction value"))+
ggtitle("M&A Activity")
> dput(Deals1)
structure(list(Date = c("2000", "2001", "2002", "2003", "2004",
"2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012",
"2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020",
"2021"), N = c(428L, 337L, 222L, 243L, 220L, 228L, 230L, 215L,
146L, 143L, 131L, 94L, 121L, 128L, 154L, 161L, 156L, 139L, 159L,
121L, 74L, 95L), total_tvalue = c(796728L, 283487L, 124839L,
199670L, 276307L, 412632L, 379802L, 224635L, 188737L, 292432L,
141469L, 244239L, 126452L, 173573L, 404071L, 564486L, 400689L,
376499L, 477247L, 591219L, 262643L, 166189L)), row.names = c(NA,
-22L), class = "data.frame")