简体   繁体   English

R ggplot 如何选择具有 2 个值的栅格的 colors

[英]R ggplot How do I choose the colors of a raster with 2 values

Here's a simple example of my problem.这是我的问题的一个简单示例。 Steps are 1. create a SpatRaster (terra's version of a raster) with 10 rows, 10 columns and all cell values either 0 or 1. Step 2. Convert to a data frame and rename the columns.步骤是 1. 创建一个具有 10 行、10 列且所有像元值为 0 或 1 的 SpatRaster(terra 的栅格版本)。步骤 2. 转换为数据框并重命名列。 Step 3. Create a graph with ggplot.步骤 3. 使用 ggplot 创建一个图表。 It uses blue and black for the colors and has a continuous legend.它为colors使用蓝色和黑色,并有一个连续的图例。

library(terra)
x <- rast(nrow=10, ncol=10, xmin=0, xmax=1)
values(x) <- sample(c(0,1), replace=TRUE, size=100)
x_df <- as.data.frame(x, xy = TRUE)
names(x_df) <- c("x", "y", "value")
col1 <- c("red", "green")
g <- ggplot(data = x_df, aes(x, y,  fill = value)) +
  geom_tile()

Now I'd like to change the colors to those in col1.现在我想将 colors 更改为 col1 中的那些。 I have tried putting colors = col1 in the ggplot aes section and outside it, in an aes in geom_tile, and a few other options.我尝试将colors = col1放在 ggplot aes 部分及其外部、geom_tile 中的 aes 以及其他一些选项中。 Pretty much all return the error message Error: Aesthetics must be either length 1 or the same as the data (100): xxx .几乎所有都返回错误消息Error: Aesthetics must be either length 1 or the same as the data (100): xxx

Converting value to a factor solves one problem (the legend is now discrete 0,1) but I still can't figure out where to add code to change the default two colors.value转换为因子解决了一个问题(图例现在是离散的 0,1),但我仍然无法弄清楚在哪里添加代码来更改默认的两个 colors。

Here's a simple example of my problem.这是我的问题的一个简单示例。 Steps are 1. create a SpatRaster (terra's version of a raster) with 10 rows, 10 columns and all cell values either 0 or 1. Step 2. Convert to a data frame and rename the columns.步骤是 1. 创建一个具有 10 行、10 列且所有像元值为 0 或 1 的 SpatRaster(terra 的栅格版本)。步骤 2. 转换为数据框并重命名列。 Step 3. Create a graph with ggplot.步骤 3. 使用 ggplot 创建一个图表。 It uses blue and black for the colors and has a continuous legend.它为colors使用蓝色和黑色,并有一个连续的图例。

library(terra)
x <- rast(nrow=10, ncol=10, xmin=0, xmax=1)
values(x) <- sample(c(0,1), replace=TRUE, size=100)
x_df <- as.data.frame(x, xy = TRUE)
names(x_df) <- c("x", "y", "value")
col1 <- c("red", "green")
g <- ggplot(data = x_df, aes(x, y,  fill = value)) +
  geom_tile()

Now I'd like to change the colors to those in col1.现在我想将 colors 更改为 col1 中的那些。 I have tried putting colors = col1 in the ggplot aes section and outside it, in an aes in geom_tile, and a few other options.我尝试将colors = col1放在 ggplot aes 部分及其外部、geom_tile 中的 aes 以及其他一些选项中。 Pretty much all return the error message Error: Aesthetics must be either length 1 or the same as the data (100): xxx .几乎所有都返回错误消息Error: Aesthetics must be either length 1 or the same as the data (100): xxx

Converting value to a factor solves one problem (the legend is now discrete 0,1) but I still can't figure out where to add code to change the default two colors.value转换为因子解决了一个问题(图例现在是离散的 0,1),但我仍然无法弄清楚在哪里添加代码来更改默认的两个 colors。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM