【问题标题】:Error in subset object not found (with geom_point in ggplot2)未找到子集对象中的错误(ggplot2 中带有 geom_point)
【发布时间】:2017-10-26 15:54:49
【问题描述】:

我正在努力解决这个问题:

我有以下data.frame:

df<-data.frame(read.csv(file="C:/Users/136728/Desktop/R/TestIRIC.csv",
header=TRUE))

我尝试使用子集用 ggplot2 绘制下图:

ggplot(data = df) + geom_point(aes(x = x, y = y)) + geom_point(data = subset(subset(data = df, y > 0 & y < 100), x > 250 & x < 375), aes(x = x, y = y), colour = "pink")

这个想法是重现下图: enter image description here

但是我收到以下错误消息:“子集中错误(数据 = df,y > 0 & y

ggplot(data = df) + geom_point(aes(x = x, y = y)) + geom_point(data = subset(df, Group == "Group1"), aes(x = x, y = y), colour = "pink")

非常感谢您的帮助!

【问题讨论】:

  • 它是subset(x = df, y &gt; 0 &amp; y &lt; 100)subset 不带 data 参数。
  • 好的,然后我用下面的代码来做 y 突出显示:ggplot(data = df) + geom_point(aes(x = x, y = y)) + geom_point(data = subset( x = df, y > 0 & y 250 & x
  • 尝试使用 data=df[df$y>0&df$y250&df$x
  • 确实我觉得我应该指定x和y属于df,但是上面不起作用...
  • 下次分享一个可重现的例子,比如dput(head(df)),我们会看到你的数据并能够测试可能的解决方案。

标签: r object dataframe ggplot2 subset


【解决方案1】:

我想,下面的代码会做你想做的事。这里的点由布尔值着色,指示它们是否在范围内。然后scale_color_manual用于手动定义两个选项的颜色。一般来说,我认为最好使用颜色参数来正确着色点,而不是在第一组之上绘制第二组。

ggplot(data = df) + geom_point(aes(x = x, y = y, color = y > 0 & y < 100 & x > 250 & x < 375))+scale_color_manual(values = c("black", "pink"))

另一种可能更可靠的方法是首先向data.frame 添加一列,指示它是否适合数据,即:

df$in_range <- df$y>0&df$y<100&df$x>250&df$x<375
ggplot(df, aes(x = x, y = y, color = in_range)) + geom_point() + ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 2020-02-27
    • 2021-10-12
    • 1970-01-01
    相关资源
    最近更新 更多