具有X和Y轴色标的散点图

时间:2013-08-07 18:04:31

标签: r graph plot colors dataframe

我尝试了一个包含许多点(> 150)的散点图。目标是区分图表某些区域的点。我基本上寻找的是一种为x和y轴设置2个色标的方法(每个1个)。基本上,我正在寻找这样的东西:

enter image description here

每个独特的点应该是各个尺度的颜色的混合。到目前为止我尝试过的是使用ggplot的散点图。我已经尝试设置颜色属性,但是它分配了自己的坐标。它也不适用于我的限制,我必须创建散点图的单独图(简而言之,放大左上角,右上角,左下角,右下角的图)。如果我将xlim和ylim设置为我自己喜欢的附加图,我得到的只是一个裁剪,导致其他点的一些剪切以及它们在图的边缘上的文本。我不能简单地创建一个单独的绘图,因为我需要在整个绘图上使用相同颜色的点和更具体的绘图(奇异颜色)。

png("image.png", width = 2000, height = 1500, res = 85);
ggplotXY <- ggplot(scatterPlotData, aes(x=x, y=y, colour=labels, label=labels)) +
geom_point() +
geom_text(hjust=0, vjust=0)
ggplotXY
dev.off()

目前整体情节:

enter image description here

放大左下角的当前情节:

png("image.png", width = 2000, height = 1500, res = 85);
ggplotXY <- ggplot(scatterPlotData, aes(x=x, y=y, colour=labels, label=labels)) +
geom_point() +
geom_text(hjust=0, vjust=0) +
coord_cartesian(xlim=c(0,100), ylim=c(0, 2.5))
ggplotXY
dev.off()

enter image description here

正如您所看到的,某些点被裁剪并且不会被忽略。为了省略不适用的点,我将不得不创建一个新的数据框,其中的实际点在限制范围内,但这样做会在创建新图时改变点的颜色。我正在考虑将每个点的颜色包含在我正在阅读的数据框中,但添加和减去十六进制颜色代码并不是很好。我尝试了这些方面的东西:

png("image.png", width = 2000, height = 1500, res = 85);
ggplotXYColor <- ggplot(scatterPlotData, aes(x=x, y=y, label=labels)) +
geom_point(colour=scatterPlotData$scatterPointColour)
ggplotXYColor
dev.off()

enter image description here

如果您想知道,scatterPlotData $ scatterPointColour如下:

  [1] "#2276c6" "#224dd0" "#201893" "#22459f" "#21580f" "#219998" "#201893"
  [8] "#216871" "#22459f" "#201893" "#2276c6" "#22459f" "#22353d" "#201893"
 [15] "#225602" "#21cabe" "#2178d3" "#21eb83" "#21eb83" "#201893" "#201893"
 [22] "#22978b" "#2276c6" "#301054" "#201893" "#301054" "#225e33" "#228f59"
 [29] "#226664" "#220c47" "#21eb83" "#228f59" "#227ef7" "#227ef7" "#226e95"
 [36] "#21c28d" "#22459f" "#228f59" "#223d6e" "#221caa" "#22459f" "#226e95"
 [43] "#225602" "#221caa" "#21d2f0" "#222d0c" "#22459f" "#201893" "#2020c4"
 [50] "#210623" "#21a1c9" "#201893" "#228f59" "#201893" "#201893" "#221caa"
 [57] "#220c47" "#201893" "#22a7ed" "#101893" "#22c080" "#201893" "#2276c6"
 [64] "#201893" "#201893" "#21d2f0" "#222d0c" "#21c28d" "#225602" "#226664"
 [71] "#226e95" "#201893" "#201893" "#21b22b" "#2020c4" "#21cabe" "#21f3b4"
 [78] "#22d0e2" "#201893" "#21c28d" "#21fbe5" "#220c47" "#225602" "#230209"
 [85] "#226664" "#210e55" "#211eb7" "#2170a2" "#201893" "#221caa" "#220c47"
 [92] "#21f3b4" "#21fbe5" "#201893" "#201893" "#201893" "#224dd0" "#247add"
 [99] "#201893" "#23fffc" "#25db1d" "#24188f" "#245a18" "#2449b6" "#24a3d3"
[106] "#201893" "#2451e7" "#24624a" "#24830e" "#2020c4" "#201893" "#201893"
[113] "#25b228" "#25eb80" "#23ced5" "#244185" "#24ed8d" "#243123" "#2449b6"
[120] "#201893" "#273b5e" "#201893" "#264dcd" "#2420c1" "#2578d0" "#264dcd"
[127] "#251eb3" "#22c8b1" "#22c080" "#22f1a7" "#249370" "#251eb3" "#2428f2"
[134] "#2428f2" "#249ba1" "#201893" "#2020c4" "#201893" "#244185" "#2472ac"
[141] "#2449b6" "#247add" "#201893" "#244185" "#243123" "#249370" "#24b435"
[148] "#2020c4" "#248b3f" "#2020c4"

我将十六进制颜色转换为十进制,然后将特定的十进制颜色一起添加,然后将其转换回十六进制。从理论上讲,它应该是一个很好的白色到黄色的x轴,白色到蓝色的y轴。随着x和y点的增加,颜色应该变得更绿。如你所见,它并不那么简单。我没有遇到任何使用2轴颜色的库。

总而言之,我需要能够使用2轴颜色为点提供独特的颜色,并且能够创建其他绘图,这些绘图将在更加放大的画布上具有精确的某些颜色。

如果有人可以提供帮助,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

在这里,您首先使用基本图形处理第一个问题(混合两个颜色渐变)。

## use white->yellow for the x-axis and white->blue for the y-axis
chooseColors <- function(x, y) {
  x <- 1-x/max(x)
  y <- 1-y/max(y)
  return(rgb(green=y, red=y, blue=x))
}


## example values for the whole range
values <- expand.grid(1:100, 1:100)

## plot it
plot(values, col=chooseColors(values[,1], values[,2]), pch=16)

enter image description here

更现实的玩具示例:

set.seed(1)
n <- 50
values <- cbind(sample(1:15, size=n, replace=TRUE), sample(1:15, size=n, replace=TRUE))

## plot it
plot(values, col=chooseColors(values[,1], values[,2]), pch=16)

enter image description here