R上的ggplot2出现ggforce facet_zoom错误

弗兰·宁林

我在R 4.0.2中有一个data.frame,在一列中有一个连续变量,在另一列中有一个分类变量的两个可能值(变量“类型”:已知或新颖),我用它们为它们分别上色(使用调色板)来自ggsci 2.9包)。我用ggplot2 3.3.2表示直方图(stat_bin),我想使用ggforce 0.3.2的facet_zoom函数仅缩放属于“类型”之一的数据(使用zoom.data选项,完成后)在http://cran.univ-paris1.fr/web/packages/ggforce/vignettes/Visual_Guide.html#contextual-zoom上的火山示例中),但是出现此错误:

Error: Aesthetics must be either length 1 or the same as the data (2000): x

可重现的示例:

library(ggplot2)
library(ggsci)
library(ggforce)

testdata <- as.data.frame(sort(rnorm(1000)))
testdata$type <- "known"
testdata[501:1000,2] <- "novel"

# Working code
ggplot(testdata) +
  stat_bin(aes(x=testdata[,1], fill = type), binwidth = 1, color="white") +
  scale_fill_npg() + theme_light() +
  facet_zoom(xlim = c(0, 4), ylim = c(0, 300), horizontal = TRUE, zoom.size = 0.3)

# Desired code
ggplot(testdata) +
  stat_bin(aes(x=testdata[,1], fill = type), data = cbind(testdata, zoom = FALSE), binwidth = 1, color="white") +
  stat_bin(aes(x=testdata[testdata$type == "novel",1]), data = cbind(testdata, zoom = TRUE), binwidth = 0.5) +
  scale_fill_npg() + theme_light() +
  facet_zoom(xlim = c(0, 4), ylim = c(0, 300), horizontal = TRUE, zoom.size = 0.3, zoom.data = zoom)

谢谢!

斯蒂芬

问题是您将整个数据集作为第二个数据传递stat_bin只需传递子集的df,而不是尝试在中进行子集化aes()

顺便说一句:我也将数据中的第一个变量重命名为x。

library(ggplot2)
library(ggsci)
library(ggforce)

set.seed(42)

testdata <- data.frame(x = sort(rnorm(1000)))
testdata$type <- "known"
testdata[501:1000,2] <- "novel"

# Desired code
ggplot(testdata) +
  stat_bin(aes(x = x, fill = type), data = cbind(testdata, zoom = FALSE), binwidth = 1, color="white") +
  stat_bin(aes(x = x), data = cbind(testdata[testdata$type == "novel", ], zoom = TRUE), binwidth = 0.5) +
  scale_fill_npg() + theme_light() +
  facet_zoom(xlim = c(0, 4), ylim = c(0, 300), horizontal = TRUE, zoom.size = 0.3, zoom.data = zoom)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用ggforce facet_zoom操纵y轴比例

ggplot 结合 geom_text_repel 和 facet_zoom

在R ggplot2和ggforce中使用facet_wrap_paginate缺少x轴标签

如何从facet_zoom()更改缩放区域的位置?

ggforce facet_zoom-仅在缩放示例上显示标签

R Shiny应用出现ggplot2错误

facet_zoom正在放大不同的x轴值

R ggplot2 防止 vline 出现在空的 ggridge facet 中

ggplot2中的facet_grid错误

SSL:macOS 10.15上的python3出现CERTIFICATE_VERIFY_FAILED错误

Swift 3上的Alamofire 4.0出现“没有成员”错误

Windows上通过Vagrant的Symfony 2出现错误,因为Symfony尝试使用Windows和Linux路径的混搭来查找树枝文件

发布后的Asp.Net Core 2出现错误“找不到布局”

修复在R中使用ggplot2绘制gpx文件时出现的错误

在R中运行代码时库(ggplot2)中出现错误

在 R 中出现几个错误 - ggplot2 包不会加载

runmqsc AMQ8157:Windows 7 x64 Edition上的MQ版本8.0.0.5出现安全错误

使用ggplot2 facet wrap R的控制图

在ggplot2 / metR中绘图时出现nullGrob错误

小牛和iOS 7出现Theos错误

Java 8出现奇怪的Android Studio错误

RxJs-Ajax错误0而不是502出现

EntityFramework Core 2.2到3.1.1出现错误

Xcode 8出现错误:“密码验证失败。”

安装时Adobe XD CC 2019出现错误

webdriverio 5.7.5出现错误$(...)。setValue不是函数

意外删除的Windows 7出现错误NTLDR丢失

SignalR Owin Host 2.0.3出现401错误

Android Studio 3 RC2出现错误“包装龙目岛不存在”