【问题标题】:Plotting certain points in different colors in R在R中以不同颜色绘制某些点
【发布时间】:2018-04-08 05:22:52
【问题描述】:

我有一个等间距点的网格 (13*8)。我想在该网格中以不同的颜色绘制一些特定的点。这些特定点的坐标存储在不同的矩阵中。有人可以帮我解决这个问题吗?

这是我用来生成网格的代码。

ggplot(data=a,aes(x=X,y=Y))+geom_point()

'a' 基本上包含在网格中绘制的点的坐标。这些点应该模拟板上螺栓的位置。

这是包含要突出显示的点的坐标的矩阵

    sigbolts
      x.c y.c
 [1,]   4   4
 [2,]   4   5
 [3,]   3   6
 [4,]   4   6
 [5,]   5   6
 [6,]   3   7
 [7,]   4   7
 [8,]   5   7
 [9,]   3   8
[10,]   4   8
[11,]   5   8
[12,]   8   8
[13,]   4   9
[14,]   4  10
[15,]   6  13

【问题讨论】:

  • how to create a reproducible example。显示a 包含的内容,并显示您如何存储要突出显示的点。变化很好,它只是另一个geom_point() 层,具有不同的data= 参数。

标签: r visualization


【解决方案1】:
library(tidyverse)

a = as_data_frame(expand.grid(1:10,1:10))
colnames(a) = c('x', 'y')
sigbolts = data_frame(x.c = c(1,3,5), y.c = c(2,4,2))
sigbolts$indicator = 1

df = left_join(a, sigbolts, by = c('x' = 'x.c', 'y' = 'y.c')) %>%
    mutate(indicator = as.factor(ifelse(is.na(indicator), 0, 1)))

ggplot(df, aes(x = x, y = y, color = indicator)) +
    geom_point()

【讨论】:

  • 实际上刚刚看到@MrFlick 的建议,这使得加入变得不必要:ggplot() + geom_point(data = a, aes(x = x, y = y)) + geom_point(data = sigbolts, aes(x = x.c, y = y.c), color = "red")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多