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

时间:2020-09-14 21:11:03

标签: r ggplot2

我希望将第五个地球化学数据集添加到我的图形中,其规模与前四个不同。我已经成功地将第二个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”进行绘制?

提前谢谢!

1 个答案:

答案 0 :(得分:1)

尝试一下。当使用多个数据源时,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, 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,color=geochem.type)) +
  geom_path(data=data.fe3,aes(x=geochem.value/0.05,
                              y=depth,color=geochem.type)) +
  theme_bw() 

输出:

enter image description here

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

#Code 3 clean up other labels in axis
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(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,color=geochem.type)) +
  geom_path(data=data.fe3,aes(x=geochem.value/0.05,
                              y=depth,color=geochem.type)) +
  theme_bw() 

输出:

enter image description here