【问题标题】:R color scatter plot points based on values基于值的R颜色散点图
【发布时间】:2013-07-09 14:42:38
【问题描述】:

我能够绘制散点图并根据一个标准为点着色,即我可以将所有 >=3 的点着色为红色,其余点着色为黑色。 我希望能够以这种方式为点着色:

  1. =3 红色

  2. 其余为黑色

我下面的代码完成了第 1 步和第 3 步,但我不确定如何合并第 2 步的第二个参数

data<- read.table('sample_data.txtt', header=TRUE, row.name=1)
pos<- data$col_name1
cn<- data$col_name2
plot(pos,cn, ylim=c(0,5), col="blue")
plot(pos,cn, col=ifelse(cn>=3,"red","black"), ylim=c(0,10))

任何帮助都会很棒!!! 提前致谢

【问题讨论】:

    标签: r


    【解决方案1】:

    这里最好的做法是在数据对象中添加一列来表示点颜色。然后通过过滤更新它的部分。

    data<- read.table('sample_data.txtt', header=TRUE, row.name=1)
    # Create new column filled with default colour
    data$Colour="black"
    # Set new column values to appropriate colours
    data$Colour[data$col_name2>=3]="red"
    data$Colour[data$col_name2<=1]="blue"
    # Plot all points at once, using newly generated colours
    plot(data$col_name1,data$col_name2, ylim=c(0,5), col=data$Colour, ylim=c(0,10))
    

    应该清楚如何使其适应具有更多颜色和条件的绘图。

    【讨论】:

    • cut 更适合创建新列,即data$Colour &lt;- cut(data$col_name2, breaks = c(-Inf, 1, 3, Inf), labels = c("blue", "black", "red"))。将其保持在一行,并且更容易推广
    【解决方案2】:

    也可以指定ifelse() 两次:

    plot(pos,cn, col= ifelse(cn >= 3, "red", ifelse(cn <= 1,"blue", "black")), ylim = c(0, 10))
    

    【讨论】:

    • 两个分析器都很棒!我什至不能责怪它在星期一错过了嵌套的 ifelse
    • 我喜欢这个,很优雅。为我的替代方案辩护我只能说,对于需要更多颜色的问题,它可以更清晰地扩展。
    【解决方案3】:

    最好使用 cut() 创建一个新的因子变量。我还使用 ggplot2 添加了一些选项。

    df <- data.frame(
      X1=seq(0, 5, by=0.001),
      X2=rnorm(df$X1, mean = 3.5, sd = 1.5)
    )
    
    # Create new variable for plotting
    df$Colour <- cut(df$X2, breaks = c(-Inf, 1, 3, +Inf), 
                     labels = c("low", "medium", "high"), 
                     right = FALSE)
    
    ### Base Graphics
    
    plot(df$X1, df$X2, 
         col = df$Colour, ylim = c(0, 10), xlab = "POS", 
         ylab = "CS", main = "Plot Title", pch = 21)
    
    plot(df$X1,df$X2, 
         col = df$Colour, ylim = c(0, 10), xlab = "POS", 
         ylab = "CS", main = "Plot Title", pch = 19, cex = 0.5)
    
    # Using `with()` 
    
    with(df, 
         plot(X1, X2, xlab="POS", ylab="CS", col = Colour, pch=21, cex=1.4)
         )
    
    # Using ggplot2
    library(ggplot2)
    
    # qplot()
    qplot(df$X1, df$X2, colour = df$Colour)
    
    # ggplot()
    p <- ggplot(df, aes(X1, X2, colour = Colour)) 
    p <- p + geom_point() + xlab("POS") + ylab("CS")
    p
    
    p + facet_grid(Colour~., scales = "free")
    

    【讨论】:

    • 使用“with”,基本方法不起作用:df = 3] = "high" df$Colour[df$X2
    • 它有效,您只是在plot() 调用中犯了语法错误。我已经编辑了答案并添加了您的版本。
    【解决方案4】:

    这是一种使用阈值和相关颜色查找表将颜色映射到感兴趣变量的方法。

     # make a grid 'Grd' of points and number points for side of square 'GrdD'
    Grd <- expand.grid(seq(0.5,400.5,10),seq(0.5,400.5,10))
    GrdD <- length(unique(Grd$Var1))
    
    # Add z-values to the grid points
    Grd$z <- rnorm(length(Grd$Var1), mean = 10, sd =2)
    
    # Make a vector of thresholds 'Brks' to colour code z 
    Brks <- c(seq(0,18,3),Inf)
    
    # Make a vector of labels 'Lbls' for the colour threhsolds
    Lbls <- Lbls <- c('0-3','3-6','6-9','9-12','12-15','15-18','>18')
    
    # Make a vector of colours 'Clrs' for to match each range
    Clrs <- c("grey50","dodgerblue","forestgreen","orange","red","purple","magenta")
    
    # Make up lookup dataframe 'LkUp' of the lables and colours 
    LkUp <- data.frame(cbind(Lbls,Clrs),stringsAsFactors = FALSE)
    
    # Add a new variable 'Lbls' the grid dataframe mapping the labels based on z-value
    Grd$Lbls <- as.character(cut(Grd$z, breaks = Brks, labels = Lbls))
    
    # Add a new variable 'Clrs' to the grid dataframe based on the Lbls field in the grid and lookup table
    Grd <- merge(Grd,LkUp, by.x = 'Lbls')
    
    # Plot the grid using the 'Clrs' field for the colour of each point
    plot(Grd$Var1,
         Grd$Var2,
         xlim = c(0,400),
         ylim = c(0,400),
         cex = 1.0,
         col = Grd$Clrs,
         pch = 20,
         xlab = 'mX',
         ylab = 'mY',
         main = 'My Grid',
         axes = FALSE,
         labels = FALSE,
         las = 1
    )
    
    axis(1,seq(0,400,100))
    axis(2,seq(0,400,100),las = 1)
    box(col = 'black')
    
    legend("topleft", legend = Lbls, fill = Clrs, title = 'Z')
    

    【讨论】:

      猜你喜欢
      • 2013-06-26
      • 1970-01-01
      • 2017-01-14
      • 2018-04-08
      • 2018-10-02
      • 1970-01-01
      • 1970-01-01
      • 2016-05-04
      • 1970-01-01
      相关资源
      最近更新 更多