简体   繁体   English

在ggplot中重叠两个具有不同y轴的图

[英]Overlapping two graphs with different y-axis in ggplot

I want to overlap two plots with different y-axis scales.我想用不同的 y 轴比例重叠两个图。 I use stat_count() and geom_line .我使用stat_count()geom_line However, geom_line doesn't appear on the plot.但是, geom_line没有出现在 plot 上。

I use the following code.我使用以下代码。

library(ggplot2)

ggplot(X1, aes(x = Week)) + 
  stat_count() +
  scale_x_continuous(breaks = seq(from = 0, to = 21, by = 1))+
  scale_y_continuous(
    name = expression("Count"), 
    limits = c(0, 20),
    sec.axis = sec_axis(~ . * 15000 / 20, name = "Views"))+
  geom_line(aes(y = Views), inherit.aes = T)

Here is the reproducible example of my data frame X1 .这是我的数据框X1的可重现示例。

structure(list(Views = c(1749, 241, 309, 326, 237, 276, 2281, 
1573, 10790, 1089, 1732, 3263, 2601, 2638, 2929, 3767, 2947, 
65, 161), Week = c(1, 2, 2, 2, 3, 3, 4, 5, 5, 5, 6, 8, 8, 8, 
8, 9, 10, 10, 10)), row.names = c(NA, -19L), class = c("tbl_df", 
"tbl", "data.frame"))

我在输出中收到的情节

Could you help me to put geom_line on the plot, please?你能帮我把geom_line放在 plot 上吗?

You also have to adjust the y values so that they fit inside the limits of the primary y-axis, ie apply the transfomation used for the secondary y-axis also inside geom_line .您还必须调整 y 值,使其适合主要 y 轴的限制,即在geom_line内应用用于次要 y 轴的变换。 Try this:尝试这个:

X1 <- structure(list(Views = c(1749, 241, 309, 326, 237, 276, 2281, 
                         1573, 10790, 1089, 1732, 3263, 2601, 2638, 2929, 3767, 2947, 
                         65, 161), Week = c(1, 2, 2, 2, 3, 3, 4, 5, 5, 5, 6, 8, 8, 8, 
                                            8, 9, 10, 10, 10)), row.names = c(NA, -19L), class = c("tbl_df", 
                                                                                                   "tbl", "data.frame"))

library(ggplot2)

ggplot(X1, aes(x = Week)) + 
  stat_count() +
  scale_x_continuous(breaks = seq(from = 0, to = 21, by = 1))+
  scale_y_continuous(
    name = expression("Count"), 
    limits = c(0, 20),
    sec.axis = sec_axis(~ . * 15000 / 20, name = "Views"))+
  geom_line(aes(y = Views / 15000 * 20), inherit.aes = T)

Created on 2020-05-21 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2020 年 5 月 21 日创建

I also summarised the dataframe to improve the interpretation of the week 5 spike and plotted separate layers我还总结了 dataframe 以改进第 5 周峰值的解释并绘制了单独的层

x1 <- structure(list(Views = c(1749, 241, 309, 326, 237, 276, 2281, 
                               1573, 10790, 1089, 1732, 3263, 2601, 2638, 2929, 3767, 2947, 
                               65, 161), Week = c(1, 2, 2, 2, 3, 3, 4, 5, 5, 5, 6, 8, 8, 8, 
                                                  8, 9, 10, 10, 10)), row.names = c(NA, -19L), class = c("tbl_df", 
                                                                                                         "tbl", "data.frame"))

x2 <- x1 %>% 
  group_by(Week) %>% 
  summarise(Views = sum(Views))
library(ggplot2)

ggplot() + 
  geom_line(data = x2, mapping = aes(x = Week, y = Views/15000 * 20))+
  geom_bar(data = x1, mapping = aes(x = Week), stat = 'count')+
  scale_x_continuous(breaks = seq(from = 0, to = 21, by = 1))+
  scale_y_continuous( name = expression("Count"), 
                     ylim.prim <- c(0, 20),
                     ylim.sec <- c(0, 15000),
                     sec.axis = sec_axis(~ . * 15000 / 20, name = "Views"))

在此处输入图像描述

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

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