R语言 图例中的某些条柱间隔未使用ggplot着色

kxkpmulp  于 2023-04-03  发布在  其他
关注(0)|答案(2)|浏览(163)

我有以下map
然而,代表区间2,3的bin在图例中是白色的,而我在dataframe中的最大值是2.55。所以,我不明白为什么?我试图修改休息,颜色的数量,似乎没有什么工作...
我已经试过了:

# Define the values that have equal distance between each other
break_values <- seq(-3, 3, by = 0.2)

# Define the desired breaks
my_breaks <- c(-3, -2, -1, -0.5, -0.1, 0.1, 0.5, 1, 2, 3)

# Map the break values to the desired breaks using the cut() function
break_labels <- cut(break_values, breaks = my_breaks, include.lowest = TRUE)

# Set the colors for each break
my_colors <- c(
  "#67001f", "#b2182b", "#d6604d", "#f4a582", 
  "#bdbdbd",
  "#4393c3", "#2166ac", "#084594", "#053061"
)

# Plot the map with the custom breaks and colors
map1=ggplot(data = sif_min_sm2) +
  geom_tile(aes(x = lon, y = lat, fill = cut(SIF_SM, breaks = my_breaks, include.lowest = TRUE))) +
  coord_equal() +
  labs(x = "Longitude", y = "Latitude") +
  scale_fill_manual(
    values = my_colors,
    labels = levels(break_labels),
    na.value = "white",
    name = "SIF anomalies",
  ) +
  ggtitle("Corresponding SIF for driest SM month") +
  theme_minimal() +
  theme(
    panel.background = element_blank(),
    plot.title = element_text(size = 14, face = "bold"),
    legend.position = "right",
    legend.key.size = unit(1, "lines"), 
    legend.text = element_text(size = 8), 
    legend.key.height = unit(1, "cm"),
    legend.margin = margin(t = 0, r = 5, b = 0, l = 0)
  )

我的df片段:

structure(list(lon = c(-53.75, -53.25, -52.75, -52.25, -51.75, 
-51.25, -50.75, -50.25, -49.75, -49.25, -48.75), lat = c(-28.25, 
-28.25, -28.25, -28.25, -28.25, -28.25, -28.25, -28.25, -28.25, 
-28.25, -28.25), SIF_SM = c(2.55345014731089, -0.308750009536743, 
0.473372499148051, 0.276275843381882, 0.404755104333162, 0.200375850001971, 
-0.258405844370524, -0.501487548152606, -0.367008318503698, -0.158782511949539, 
-0.483018159866333)), row.names = 159373:159383, class = "data.frame")
v2g6jxz6

v2g6jxz61#

根据你的示例数据,我无法重现你的问题。但我猜测这是因为NA值。因此,我稍微调整了你的示例数据,添加了一个超出cut使用的中断范围的值,即我用4替换了第一个值。
这样我就可以重现您的问题:

library(ggplot2)

# Plot the map with the custom breaks and colors
p <- ggplot(data = sif_min_sm2) +
  geom_tile(aes(x = lon, y = lat, fill = cut(SIF_SM, breaks = my_breaks, include.lowest = TRUE))) +
  coord_equal() +
  labs(x = "Longitude", y = "Latitude") +
  ggtitle("Corresponding SIF for driest SM month") +
  theme_minimal() +
  theme(
    panel.background = element_blank(),
    plot.title = element_text(size = 14, face = "bold"),
    legend.position = "right",
    legend.key.size = unit(1, "lines"), 
    legend.text = element_text(size = 8), 
    legend.key.height = unit(1, "cm"),
    legend.margin = margin(t = 0, r = 5, b = 0, l = 0)
  )

p +
  scale_fill_manual(
    values = my_colors,
    labels = levels(break_labels),
    na.value = "white",
    name = "SIF anomalies"
  )

此外,根据从scale_fill_manual删除labels时@tjebo的回答,这确实与NA相关,在这种情况下,NA显示为图例标签。

p +
  scale_fill_manual(
    values = my_colors,
    na.value = "white",
    name = "SIF anomalies"
  )

解决这个问题的一个选择是明确设置限制,这也确保了正确的颜色被分配给箱子。尽管如此,我还是建议检查你的数据范围,如果我的猜测是正确的,调整装箱:

p +
  scale_fill_manual(
    values = my_colors,
    labels = levels(break_labels),
    na.value = "white",
    name = "SIF anomalies",
    limits = levels(break_labels)
  )

数据

sif_min_sm2 <- structure(list(lon = c(
  -53.75, -53.25, -52.75, -52.25, -51.75,
  -51.25, -50.75, -50.25, -49.75, -49.25, -48.75
), lat = c(
  -28.25,
  -28.25, -28.25, -28.25, -28.25, -28.25, -28.25, -28.25, -28.25,
  -28.25, -28.25
), SIF_SM = c(
  4,
  #2.55345014731089, 
  -0.308750009536743,
  0.473372499148051, 0.276275843381882, 0.404755104333162, 0.200375850001971,
  -0.258405844370524, -0.501487548152606, -0.367008318503698, 
  -0.158782511949539,
  -0.483018159866333
)), row.names = 159373:159383, class = "data.frame")
g2ieeal7

g2ieeal72#

我不能完全重现这个问题。我相信这可能与你的数据有关。当使用自己的自制数据时,我不会遇到这个问题。
另一点需要注意的是,当使用剪切作为填充美学时,您不需要在scale_ function中指定标签-您的实际问题可能会隐藏在这里。
然而,我建议使用scale_binned-不需要削减,所有现成的,直观的。

library(tidyverse)

df <- data.frame(x=rep(1:5, 6), y = rep(1:6, each = 5), value = rnorm(30, 0))

# Define the desired breaks
my_breaks <- c( -3, -2, -1, -0.5, -0.1, 0.1, 0.5, 1, 2, 3)

# Set the colors for each break
my_colors <- c(
  "#67001f", "#b2182b", "#d6604d", "#f4a582", 
  "#bdbdbd",
  "#4393c3", "#2166ac", "#084594", "#053061"
)

ggplot(df) +
  geom_tile(aes(x = x, y = y, fill = cut(value, breaks = my_breaks, include.lowest = TRUE))) +
  coord_equal() +
  scale_fill_manual(
    values = my_colors,
    ## remove labels if you're using cuts for your fill aesthetic anyways. 
    na.value = "white"
  )

## NA added 
df$value[1] <- NA

## Alternatively, use scale_binned
ggplot(df) +
  geom_tile(aes(x = x, y = y, fill = value)) +
  coord_equal() +
  scale_fill_stepsn(breaks = my_breaks, limits = range(my_breaks), 
                    colors = my_colors, na.value = "white")

创建于2023-03-29带有reprex v2.0.2

相关问题