使用具有相同规模的因子和变量 - 可能吗?

发布于 2024-10-04 19:35:51 字数 1182 浏览 0 评论 0原文

我想用 ggplot 绘制来自不同数据帧的数据。但是,我在离散和连续尺度方面遇到问题。

假设我们要使用此数据:

x <- rnorm(9,5)
y <- rnorm(9,5)
f1 <- rep(c("a","b","c"),3) 
df1 <- data.frame(x,y,f1)
x <- rnorm(9,5)
y <- rnorm(9,5)
f2 <- rep(c("d","e","f"),3)
df2 <- data.frame(x,y,f2)

我想在一个图上显示两个数据框。 df1$f1 和 df1$f2 都应负责点的颜色:

p <- ggplot(df1,aes(x,y))
p <- p + geom_point(aes(colour=f1))
p <- p + geom_point(data=df2,aes(x,y,colour=f2))
p

这有效。

但是,当我有这些数据时:

x <- rnorm(9,5)
y <- rnorm(9,5)
f1 <- rep(c("a","b","c"),3) 
df1 <- data.frame(x,y,f1)
x <- rnorm(9,5)
y <- rnorm(9,5)
quan <- rnorm(9,1)
df2 <- data.frame(x,y,quan)

此图不起作用:

p <- ggplot(df1,aes(x,y))
p <- p + geom_point(aes(colour=f1))
p <- p + geom_point(data=df2,aes(x,y,colour=quan))
p

Error: Continuous variable () supplied to discrete scale_hue.

f1 作为一个因子,quan 是一个数值,并且它们不能是合乎逻辑的使用相同的比例。但是如何为每个数据框定义单独的比例呢?或者这可能是不可能的?

我尝试使用 scale_colour_discretescale_colour_continuous,但似乎两个比例中只能使用一个图。

I want to plot data from different data frames with ggplot. However, I'm having problems with discrete and continuous scales.

Let's say we want to use this data:

x <- rnorm(9,5)
y <- rnorm(9,5)
f1 <- rep(c("a","b","c"),3) 
df1 <- data.frame(x,y,f1)
x <- rnorm(9,5)
y <- rnorm(9,5)
f2 <- rep(c("d","e","f"),3)
df2 <- data.frame(x,y,f2)

I'd like to show both data frames on one plot. df1$f1 and df1$f2 shall both be responsible for colours of the points:

p <- ggplot(df1,aes(x,y))
p <- p + geom_point(aes(colour=f1))
p <- p + geom_point(data=df2,aes(x,y,colour=f2))
p

This works.

However, when I have this data:

x <- rnorm(9,5)
y <- rnorm(9,5)
f1 <- rep(c("a","b","c"),3) 
df1 <- data.frame(x,y,f1)
x <- rnorm(9,5)
y <- rnorm(9,5)
quan <- rnorm(9,1)
df2 <- data.frame(x,y,quan)

this plot doesn't work:

p <- ggplot(df1,aes(x,y))
p <- p + geom_point(aes(colour=f1))
p <- p + geom_point(data=df2,aes(x,y,colour=quan))
p

Error: Continuous variable () supplied to discrete scale_hue.

f1 as a factor, quan is a numeric value, and it seems logical that they can't be used with the same scale. But how can I define a separate scale for each data frame? Or is this maybe not possible?

I tried using scale_colour_discrete and scale_colour_continuous, but it seems as if only one of the two scales can be used for one plot.

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

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

发布评论

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

评论(1

水波映月 2024-10-11 19:35:51

每个图只能有一种类型的比例。在第一个示例中,两种情况下的比例都是一个因素,因此您不会收到错误。

对于第二个示例,一个用于连续变量 (f),另一个用于因子 (quan)。当您尝试传递“quan”时,ggplot 需要一个连续变量,而不是一个因子,并为您提供错误消息。

您应该尝试先使用 rbind() 然后绘图来简化。但请确保您的数据格式相同。如果 rbind 失败,那是因为您没有向它传递类似结构的数据帧:

df3 <- rbind(df1,df2) 
p <- ggplot(df3, aes(x,y,colour=f) + geom_point()

You can only have one type of scale per plot. In the first example, the scale in both cases is a factor so you do not receive an error.

For the second example, one is for a continuous variable (f) and the other is for a factor (quan). When you try to pass 'quan' ggplot expects a continuous variable, rather than a factor and provides you with the error message.

You should try to simplify by using rbind() first, and then plotting. But make sure your data is formatted the same way. If rbind fails it's because you're not passing similarly structured data frames to it:

df3 <- rbind(df1,df2) 
p <- ggplot(df3, aes(x,y,colour=f) + geom_point()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文