Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在ggplot中重叠两个y轴不同的图_R_Ggplot2_Yaxis - Fatal编程技术网

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

R 在ggplot中重叠两个y轴不同的图,r,ggplot2,yaxis,R,Ggplot2,Yaxis,我想用不同的y轴比例重叠两个图。我使用stat_count和geom_line。然而,geom_线不会出现在绘图上 我使用以下代码 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"),

我想用不同的y轴比例重叠两个图。我使用stat_count和geom_line。然而,geom_线不会出现在绘图上

我使用以下代码

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)
这是我的数据帧X1的可复制示例


你能帮我把几何图形线放在绘图上吗?

你还必须调整y值,使其符合主y轴的限制,即在几何图形线内应用用于辅助y轴的变换。试试这个:


X1您还必须调整y值,使其适合主y轴的限制范围内,即在geom_线内应用用于次y轴的变换。试试这个:


X1我还总结了数据帧,以改进对第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"))

我还总结了数据框架,以改进对第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"))

非常感谢。它起作用了!我还添加了geom_pointdata=x2,mapping=aesx=Week,y=Views/15000*20,以避免在没有视图的情况下误解第7周。谢谢!它起作用了!我还添加了geom_pointdata=x2,mapping=aesx=Week,y=Views/15000*20,以避免在没有视图的情况下误解第7周。