r - 添加第二个轴(sec.axis)时ggplot图发生变化
问题描述
我正在尝试创建一个带有两个 y 轴的图,每个轴都有不同的值。当我添加第二个轴的数据时,第一个轴得到额外的(不需要的)区域。知道如何解决这个问题吗?我创建了一些简单的示例,只是为了说明我做错了什么。
谢谢!
library(tidyverse)
starwars <- starwars[,c(2,3,7)] %>% drop_na() %>%
dplyr::filter(mass > 40 & mass < 200) %>%
mutate(some_y = height/max(height))
viz <- starwars %>% ggplot() +
geom_line(aes(x = birth_year, y = mass), colour = "blue") +
labs(ylim = c(70,150)) #until here, the plot is fine
viz +
geom_line(data = starwars, #starting from here, it jumps up
aes(x = birth_year, y = some_y*100), colour = "red") +
scale_y_continuous(sec.axis = sec_axis(~ ., name = "2nd y",
labels = seq(0, 100, length.out = 11), breaks = seq(0, 100, length.out = 11)))
首先,它工作正常:(即)
然后,它跳起来:(即+ ...)
解决方案
推荐阅读
- angular - 角度表日期范围过滤器不起作用
- ios - AFNetworking 迁移到版本 4.0.1 - AFURLConnectionOperation
- vue.js - 如何在 Vue3 设置标签中定义组件名称?
- mysql - 使用自定义 json 编组器时为 struct 找到无效字段
- javascript - 当排序排序信息仅可用于某些项目时对 Javascript 数组进行排序
- javascript - React - 如何在 2021 年测试图像 onload 或 onerror
- android - 访问 QT / QML 中的系统图标
- angular - 在 ngx-contextmen 中以编程方式关闭子菜单
- java - 我在 MyPanel 类中创建和显示面板时遇到问题
- flutter - 如何从发布模式切换到调试模式