提供给ggplot2 geom_point图中的scale_x_连续的非连续变量

发布于 2024-10-20 17:57:03 字数 560 浏览 1 评论 0原文

我有一个有两列的表,它们都是连续数据。我检查了 csv 文件以确保这些列中只有数字值。然而,当我绘制它们时,其中一个似乎被视为非连续数据,并且我得到:错误:提供给scale_x_continuous的非连续变量。 这是我的表格的一个小版本,

   budget    gross
1  234       4234
2  42342     2323
3  22165     346
4  290       452
...

我试图创建一个散点图,其中 y 轴为总数,x 轴为预算。 我尝试了这个,但出现了上述错误。

p <- ggplot(test, aes(Budget, Gross))+geom_point(alpha=I(1/5), aes(colour=Budget))+ opts(titles="Movies per Year", panel.grid.major = theme_blank(), panel.grid.minor = theme_blank())+scale_x_continuous()

太感谢了

I have a table that has two columns both of them are continuous data. I checked the csv file to make sure there are only numeric values in those columns. However, when I plot them one of them seems to be taken as non-continuous data, and I get: Error: Non-continuous variable supplied to scale_x_continuous.
This is a small version of my table

   budget    gross
1  234       4234
2  42342     2323
3  22165     346
4  290       452
...

I am trying to create a scatter plot where the gross numbers are in the y axis and the budget in the x axis.
I tried this but I get the fore-mentioned error.

p <- ggplot(test, aes(Budget, Gross))+geom_point(alpha=I(1/5), aes(colour=Budget))+ opts(titles="Movies per Year", panel.grid.major = theme_blank(), panel.grid.minor = theme_blank())+scale_x_continuous()

Thank you so much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉默的熊 2024-10-27 17:57:04

尝试class(test$Budget)。 R 很可能认为您的专栏是一个因素。如果是这样,您可以通过在 read.csv() 内部使用 stringsAsFactors 选项来解决该问题:

test <- read.csv(file = "yourdata.csv", stringsAsFactors = FALSE)

或者为整个会话设置它:

options(stringsAsFactors = FALSE)

从个人经验的话,我推荐后者。实际上,我以这种方式启动所有脚本 - 大多数需要因子的函数将根据需要强制其他向量类型,如果它们不这样做,那么我将手动指定它。但是,数据中潜藏着一堆向量只会让您头疼。

Try class(test$Budget). Odds are that R believes that your column is a factor. If that's so, you can get around the problem by using the stringsAsFactors option, either inside of your read.csv():

test <- read.csv(file = "yourdata.csv", stringsAsFactors = FALSE)

or set it for the entire session:

options(stringsAsFactors = FALSE)

From personal experience, I'd recommend the latter. I start all of my scripts that way, actually - most functions that need factors will coerce other vector types as necessary, and if they don't, then I'll manually specify it. But having a bunch of vectors lurking in your data will cause you nothing but headaches.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文