【问题标题】:Scatter plot points subset colour shape ifelse散点图点子集颜色形状 ifelse
【发布时间】:2014-05-30 08:11:47
【问题描述】:

我想做一个散点图,其中一些值,在一个子集中,以不同的颜色和形状绘制。 我已经解决了以下代码,但我没有设法使它正确。 非常感谢任何帮助!

# My data
iris
iris$Code <- 1:150

# A selection of my data I'd like to plot differently
subset <- subset(iris, iris$Sepal.Width<3.5)
sel <- as.character(subset$Code) # I think the problems start here :)

# Plotting doesn't work
plot(iris$Sepal.Length ~ iris$Sepal.Width,
     col=ifelse(iris$Code==sel, "red", "black"),
     pch=ifelse(iris$Code==sel, 17, 1))

【问题讨论】:

    标签: r plot subset point


    【解决方案1】:

    试试这个:

    #define subset
    sel <- iris[iris$Sepal.Width<3.5,"Code"]
    #plot
    plot(iris$Sepal.Length ~ iris$Sepal.Width,
         col=ifelse(iris$Code %in% sel, "red", "black"),
         pch=ifelse(iris$Code %in% sel, 17, 1))
    

    注意:在您的代码中有一些明显的错误:

    dd &lt;- iris - 为什么分配给dd 而从不使用它?

    iris$Sepal.Widith - 拼写

    ... "black") - 最后缺少逗号。

    【讨论】:

    • 非常感谢 zx8754,效果很好!是的,我犯了一些错误,对此感到抱歉!我想我写的问题太快了!我刚刚编辑了我的帖子。
    猜你喜欢
    • 1970-01-01
    • 2015-11-13
    • 2017-08-16
    • 2019-11-27
    • 2017-10-12
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多