简体   繁体   English

R - 在 ggplot 中包括两个 y 轴

[英]R - Including two y-axis in ggplot

Could anyone help me to add a second y-axis in ggplot or alternatively combine the two separate ggplots I have made?谁能帮我在 ggplot 中添加第二个 y 轴,或者将我制作的两个单独的 ggplot 组合起来? (R-code attached) (附上R代码)

The data: Dataframe = Deals1 include three columns (Year = Date, Number of transactions each year = N, and total transaction value each year = total_tvalue).数据:Dataframe = Deals1 包括三列(Year = Date,每年的交易数量 = N,每年的总交易价值 = 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数据集包括 22 行(2000-2021 年),交易数量从 50-500 不等,交易价值从 100.000 到 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")

A secondary axis in ggplot is just an inert annotation drawn on to the side of the plot. ggplot 中的辅助轴只是绘制在绘图一侧的惰性注释。 It does not affect what is on the actual plot panel in any way.它不会以任何方式影响实际绘图面板上的内容。

In your case, if you plot both the bars and the line on the same panel, you can't see the bars because the line is 1,000 times larger than them.在您的情况下,如果您在同一面板上同时绘制条形图和线条,则看不到条形图,因为线条比它们大 1,000 倍。

To use a secondary axis here, we have to divide tvalue by about 1,000 so that it is on approximately the same scale as N .要在此处使用辅助轴,我们必须将tvalue除以大约 1,000,使其与N的比例大致相同。 Of course, this means that anyone reading our chart would get the wrong numbers for tvalue if they look at our y axis.当然,这意味着任何阅读我们图表的人在查看我们的 y 轴时都会得到错误的tvalue数字。 That's where a secondary axis comes in. We specify that the secondary axis shows numbers that are 1,000 times larger than they "really" are.这就是辅助轴的用武之地。我们指定辅助轴显示的数字比“实际”大 1,000 倍。

In addition, your plotting code needs a couple of other tweaks.此外,您的绘图代码还需要一些其他的调整。 At the moment it doesn't draw a line at all because the years are in character format rather than numeric, so you need to either use as.numeric(Date) or add a group = 1 to the aesthetic mapping.目前它根本没有画一条线,因为年份是字符格式而不是数字,所以您需要使用as.numeric(Date)或将group = 1添加到美学映射中。 Secondly geom_bar(stat = "identity") is just a long way of writing geom_col其次geom_bar(stat = "identity")只是写geom_col的漫长道路

library(ggplot2)

ggplot(Deals1, aes(as.numeric(Date))) + 
  geom_col(aes(y = N), fill = "deepskyblue4", alpha = 0.8) + 
  geom_line(aes(y = total_tvalue / 1500), color = "orangered3", size = 1) +
  scale_y_continuous(
    name = "Number of transactions", breaks = 0:5 * 100,
    sec.axis = sec_axis(~.x * 1500, 
                        name = "Transaction value",
                        labels = function(x) {
                           paste0(scales::dollar(x/1000), "K")})) +
  ggtitle("M&A Activity") +
  theme_light(base_size = 16) 

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM