繁体   English   中英

ggplot2和R 2.14.2“ continuous_scale中的错误”和“继承中的错误”

[英]“Error in continuous_scale” and “error in inherits” ggplot2 & R 2.14.2

我正在关注Drew Conway和John White所著的《 面向黑客机器学习 》一书中的教程,但我一直在绘制直方图时遇到问题。 示例代码在此处运行绘图部分:

> quick.hist <- ggplot(ufo.us, aes(x = DateOccurred)) +
+   geom_histogram() + 
+   scale_x_date(major = "50 years")

产生

Error in continuous_scale(aesthetics, "date", identity, breaks = breaks,  : unused argument(s) (major = "50 years")

> ggsave(plot = quick.hist,
+        filename = "C:\test.png",
+        height = 6,
+        width = 8)

产生

Error in inherits(plot, "ggplot") : object 'quick.hist' not found

我正在使用R版本2.14.2。 和ggplot2库。 在此先感谢您的帮助。


解决了

一个对我有用的快速解决方案是消除引用标签的每一行的'+ scale_x_date(major =“ 50 years”)'部分。 产生直方图的最终代码如下:

> quick.hist <- ggplot(ufo.us, aes(x = DateOccurred)) +
+   geom_histogram()

我想在某个时候向图表添加标签,但就目前而言,此解决方案适用于新版本的ggplot2。


更好的分辨率
在阅读本书的动手示例时,我遇到了类似的问题。 我在这里发布了本书最终情节的完整摘要(这与该问题中最初引用的情节不同,但也暴露了相同的问题)。
此修复解决了以下问题

  • scale_x_date上的旧语法(感谢Jonas Heidelberg
  • 需要显式引用scales库(感谢B0WSER
  • legend=不推荐使用的语法(由guide=代替)
  • opts()弃用的语法(由labs()和其他替换)

本书摘要的更改以粗体显示在下面:

library(ggplot2)
图书馆(秤)
state.plot <-
ggplot(all.sightings, aes(x=YearMonth, y=Sightings)) +
geom_line(aes(color="darkblue")) +
facet_wrap(~State, nrow=10, ncol=5) +
theme_bw() +
scale_color_manual(values=c("darkblue"="darkblue"), guide =“ none” ) +
scale_x_date(breaks = date_breaks(width =“ 5 years”),
标签= date_format(“%Y”)) +
xlab("Time") + ylab("Nb of Sightings") +
实验室(标题= "Nb of UFO sightings by Month-Year and US State (1990-2000)")

print(state.plot)

第二个问题是由第一个引起的。 该论坛讨论建议您看到与第一个问题不兼容的版本。 链接PDF在第31/32页讨论您的问题; 它似乎

 scale_x_date(breaks = date_breaks(width = "50 years"), labels = date_format("%Y"))

是您应该使用的新语法,而不是scale_x_date(major = "50 years") 阅读PDF以获取更多详细信息……并祝您学习指南顺利! 如果继续,您可能想要安装本教程编写的确切软件版本。

根据R帮助,在输入date_breaks时,必须指定宽度,然后指定时间范围和规格(“ sec”,“ min”,“ hour”,“ day”,“ week”,“ month”,“ year”)使它正常运行。 我已经在前面的代码片段中添加了必要的语法。 在通过2012年10月14日的“面向黑客的机器学习”教程时,已对此进行了验证和测试。

有同样的问题。

新增中

library(scales)

解决了这个问题。

只需使用作者的Github存储库代码中的代码,即可轻松工作。 仅供参考,我将作者的代码复制并粘贴到此处。

quick.hist <- ggplot(ufo.us, aes(x = DateOccurred)) +
  geom_histogram() + 
  scale_x_date(breaks = "50 years")

ggsave(plot = quick.hist,
       filename = file.path("images", "quick_hist.png"),
       height = 6,
       width = 8)

只需两个步骤:

  1. 添加库:

    图书馆(秤)

  2. 更改旧的参数名称:

    breaks="50 years"而不是major="50 years"

就这样。

在对该线程尝试其他建议之后,我发现最好删除scale_x_date行。 即使对R版本进行了调整,默认设置也会看起来更好。

另外,在研究示例时,您会意识到在此问题上花费5-10分钟以上是不值得的。 (不幸的是,我是很难学到的。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM