微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用其他数据框添加geom_point图层?

如何解决如何使用其他数据框添加geom_point图层?

我希望将第五个地球化学数据集添加到我的图形中,其规模与前四个不同。我已经成功地将第二个x轴缩放到了其他数据集,但是在绘制数据集时遇到了麻烦。

一个数据集是“ data.fe.clean”,第二个数据集是“ data.fe3”。这是我的代码

p.fe.clean <- ggplot(data.fe.clean,aes(x=geochem.value,y=depth)) +
  geom_point(aes(color=geochem.type)) +
  geom_path(aes(color=geochem.type)) +
  facet_wrap(~ core) +
  scale_x_continuous(data.fe3,sec.axis = dup_axis(~.*0.05,name = "Fe (III) [nA]")) +
  geom_point(data.fe3,inherit.aes = FALSE,aes(x=geochem.value/0.05,y=depth)) +
  geom_path(aes(color=geochem.type)) +
  theme_bw() 
  
print(p.fe.clean)

这给了我错误

Error: `mapping` must be created by `aes()`
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
<error/rlang_error>
`mapping` must be created by `aes()`
Backtrace:
 1. ggplot2::geom_point(...)
 2. ggplot2::layer(...)
 3. ggplot2:::validate_mapping(mapping)
Run `rlang::last_trace()` to see the full context.
> 

这还将在我的图形下方添加最特殊的值列表,如下所示:

enter image description here

如何删除值列表,同时还要从数据框“ data.fe3”进行绘制?

提前谢谢!

解决方法

尝试一下。当使用多个数据源时,ggplot2包会遇到不同地理区域的问题。因此,如果您使用其他几何,请尝试添加data=...。这里的代码,缺少数据,无法在问题上重现:

p.fe.clean <- ggplot(data.fe.clean,aes(x=geochem.value,y=depth)) +
  geom_point(aes(color=geochem.type)) +
  geom_path(aes(color=geochem.type)) +
  facet_wrap(~ core) +
  scale_x_continuous(data.fe3,sec.axis = dup_axis(~.*0.05,name = "Fe (III) [nA]")) +
  geom_point(data=data.fe3,inherit.aes = FALSE,aes(x=geochem.value/0.05,y=depth)) +
  geom_path(aes(color=geochem.type)) +
  theme_bw() 

一些连接点的代码:

#Code 2 connect dots
ggplot(data.fe.clean,y=depth,color=geochem.type)) +
  geom_path(data=data.fe3,color=geochem.type)) +
  theme_bw() 

输出:

enter image description here

轴上还有其他值的问题是因为您在scale_x_continuous()中重复了数据。这里的代码:

#Code 3 clean up other labels in axis
ggplot(data.fe.clean,y=depth)) +
  geom_point(aes(color=geochem.type)) +
  geom_path(aes(color=geochem.type)) +
  facet_wrap(~ core) +
  scale_x_continuous(sec.axis = dup_axis(~.*0.05,color=geochem.type)) +
  theme_bw() 

输出:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。