geom_point 周围的黑色轮廓和颜色匹配图例 - ggplot

问题描述 投票:0回答:1

我知道这可能简单明了,但我尝试过的所有不同方法都无法保持填充颜色。我想让第一个 geom_point(data = may_sonde_ph_situ) 在点周围有一个黑色轮廓,以便它们在图中突出。使用该 geom_point 仅绘制了 12 个数据点。

我还希望这些 geom_point 形状的图例具有与图形上显示的颜色适当的颜色。现在,它们的形状和颜色编码为它们的视线名称,并且它根据其他 geom_point 调用的视线匹配颜色,但在图例上它们是黑色的。

代码

annotation_ph_sonde <- data.frame(
   x = c("2023-05-20", "2023-05-20"),
   y = c(8.6,6.4),
   label = c("TMDL Numeric Target Upper Limit = 8.5", "TMDL Numeric Target Lower Limit = 6.5")
)


ggplot() +
  geom_point(data = may_sonde_ph_situ, aes(x = sample_date_time, y = result, color = station_name, shape = station_name,
                                           group = station_name), size = 3)+
  geom_point(data = may_sonde_ph, aes(x = sample_date_time, y = result, color = station_name,
                                      group = station_name), size = 0.8, alpha = 0.6) +
  theme_classic() +
  geom_hline(yintercept = c(8.5, 6.5), linetype = "solid", color = "black", size = 0.7) +
    scale_color_viridis(
    discrete = TRUE, name = NULL,
    guide = guide_legend(order = 1)
  ) +
  ggnewscale::new_scale_color() +
    scale_color_viridis(
    discrete = TRUE, name = NULL,
    guide = guide_legend(order = 2)
  ) +
  ggtitle(paste0("May 2023 - pH")) +
  theme(
    plot.title = element_text(hjust = 0.5),
    axis.title.x = element_blank(),
    legend.position = "bottom",
    legend.box = "vertical",
    legend.direction = "horizontal",
    panel.grid.major.y = element_line(size = .01, color = "grey60"),
    legend.margin = margin(),
    panel.grid.major.x = element_line(size=.01, color="grey60" ),
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.title=element_blank()) +
  labs(y="pH") +
  scale_y_continuous(limits = c(6, 9), breaks = seq(6, 9, by = 0.5),
                     expand = c(0,0)) +
  geom_text(data = annotation_ph_sonde, aes(x=x, y=y, label=label))+
  guides(color = guide_legend(nrow = 1, byrow = TRUE)) +
    scale_x_discrete(breaks = unique(may_sonde_ph$date),
                     labels = unique(may_sonde_ph$date),
                     expand = c(0.04,0.04))

Need black outline and legend to match on color

样本数据

may_sonde_ph <- structure(list(station_name = c("TMDL-R1", "TMDL-R2", "TMDL-R3", 
"TMDL-R4", "TMDL-R1", "TMDL-R2", "TMDL-R3", "TMDL-R4", "TMDL-R1", 
"TMDL-R2", "TMDL-R3", "TMDL-R4", "TMDL-R1", "TMDL-R2", "TMDL-R3", 
"TMDL-R4", "TMDL-R1", "TMDL-R2", "TMDL-R3", "TMDL-R4", "TMDL-R1", 
"TMDL-R2", "TMDL-R3", "TMDL-R4", "TMDL-R1", "TMDL-R2", "TMDL-R3", 
"TMDL-R4", "TMDL-R1", "TMDL-R2"), sample_date_time = c("2023-05-12 11:00:00", 
"2023-05-12 11:00:00", "2023-05-12 11:00:00", "2023-05-12 11:00:00", 
"2023-05-12 11:15:00", "2023-05-12 11:15:00", "2023-05-12 11:15:00", 
"2023-05-12 11:15:00", "2023-05-12 11:30:00", "2023-05-12 11:30:00", 
"2023-05-12 11:30:00", "2023-05-12 11:30:00", "2023-05-12 11:45:00", 
"2023-05-12 11:45:00", "2023-05-12 11:45:00", "2023-05-12 11:45:00", 
"2023-05-12 12:00:00", "2023-05-12 12:00:00", "2023-05-12 12:00:00", 
"2023-05-12 12:00:00", "2023-05-12 12:15:00", "2023-05-12 12:15:00", 
"2023-05-12 12:15:00", "2023-05-12 12:15:00", "2023-05-12 12:30:00", 
"2023-05-12 12:30:00", "2023-05-12 12:30:00", "2023-05-12 12:30:00", 
"2023-05-12 12:45:00", "2023-05-12 12:45:00"), parameter = c("pH", 
"pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH", 
"pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH", 
"pH", "pH", "pH", "pH", "pH", "pH", "pH"), result = c(8.32, 8.24, 
8.39, 8.17, 8.32, 8.23, 8.37, 8.12, 8.32, 8.22, 8.35, 8.05, 8.32, 
8.21, 8.34, 8, 8.32, 8.19, 8.32, 7.97, 8.32, 8.17, 8.3, 7.94, 
8.32, 8.14, 8.29, 7.9, 8.32, 8.12), units = c("None", "None", 
"None", "None", "None", "None", "None", "None", "None", "None", 
"None", "None", "None", "None", "None", "None", "None", "None", 
"None", "None", "None", "None", "None", "None", "None", "None", 
"None", "None", "None", "None"), comments = c(NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), date = c("2023-05-12", 
"2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", 
"2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", 
"2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", 
"2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", 
"2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", 
"2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12"), ym = c("2023-05", 
"2023-05", "2023-05", "2023-05", "2023-05", "2023-05", "2023-05", 
"2023-05", "2023-05", "2023-05", "2023-05", "2023-05", "2023-05", 
"2023-05", "2023-05", "2023-05", "2023-05", "2023-05", "2023-05", 
"2023-05", "2023-05", "2023-05", "2023-05", "2023-05", "2023-05", 
"2023-05", "2023-05", "2023-05", "2023-05", "2023-05")), row.names = c(NA, 
30L), class = "data.frame")


may_sonde_ph_situ <- structure(list(program = c("Ventura River TMDL", "Ventura River TMDL", 
"Ventura River TMDL", "Ventura River TMDL", "Ventura River TMDL", 
"Ventura River TMDL", "Ventura River TMDL", "Ventura River TMDL", 
"Ventura River TMDL", "Ventura River TMDL", "Ventura River TMDL", 
"Ventura River TMDL"), station_name = c("TMDL-R1", "TMDL-R1", 
"TMDL-R1", "TMDL-R2", "TMDL-R2", "TMDL-R2", "TMDL-R3", "TMDL-R3", 
"TMDL-R3", "TMDL-R4", "TMDL-R4", "TMDL-R4"), sample_date_time = c("2023-05-12 06:17:00", 
"2023-05-19 04:42:00", "2023-05-26 04:55:00", "2023-05-12 05:26:00", 
"2023-05-19 04:11:00", "2023-05-26 04:20:00", "2023-05-12 04:30:00", 
"2023-05-19 03:33:00", "2023-05-26 03:25:00", "2023-05-12 03:36:00", 
"2023-05-19 02:40:00", "2023-05-26 02:45:00"), parameter = c("pH", 
"pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH", "pH"
), result = c(8.29, 8.38, 8.35, 8.35, 8.41, 8.3, 8.3, 8.34, 8.26, 
8, 7.84, 7.79), units = c("None", "None", "None", "None", "None", 
"None", "None", "None", "None", "None", "None", "None"), date = c("2023-05-12", 
"2023-05-19", "2023-05-26", "2023-05-12", "2023-05-19", "2023-05-26", 
"2023-05-12", "2023-05-19", "2023-05-26", "2023-05-12", "2023-05-19", 
"2023-05-26"), ym = c("2023-05", "2023-05", "2023-05", "2023-05", 
"2023-05", "2023-05", "2023-05", "2023-05", "2023-05", "2023-05", 
"2023-05", "2023-05")), row.names = c(NA, -12L), class = "data.frame")
r ggplot2
1个回答
0
投票

您遇到的问题是您需要使用

scale_shape_manual
手动指定具有边框的形状。从那里你需要稍微欺骗
geom_point
以获得想要的结果。

library(viridis)
#> Loading required package: viridisLite
library(ggplot2)



ggplot() +
  geom_point(data = may_sonde_ph_situ, aes(x = sample_date_time, y = result, 
                                           fill = station_name,
                                           shape = station_name,
                                           group = station_name), size = 3) +
  geom_point(data = may_sonde_ph_situ, aes(x = sample_date_time, y = result, 
                                           shape = station_name,
                                           group = station_name), size = 3,
                color  = 'black', alpha = 1) +
  geom_point(data = may_sonde_ph, aes(x = sample_date_time,
                                      y = result, color = station_name,
                                      group = station_name),
                 size = 0.8, alpha = 0.6, show.legend = FALSE) +
  
  scale_y_continuous(limits = c(6, 9), breaks = seq(6, 9, by = 0.5),
                     expand = c(0,0)) +
  scale_x_discrete(breaks = unique(may_sonde_ph$date),
                   labels = unique(may_sonde_ph$date),
                   expand = c(0.04,0.04)) +
  geom_hline(yintercept = c(8.5, 6.5), linetype = "solid", color = "black", linewidth = 0.7) +
## this these shapes have borders
scale_shape_manual(values = 21:25)  +
  scale_color_viridis(
    discrete = TRUE, name = NULL,
    guide = guide_legend(order = 2)
  ) +
  scale_fill_viridis_d() +
  labs(title = "May 2023 - pH", y = "pH") +  
  theme_classic() +
  theme(
    plot.title = element_text(hjust = 0.5),
    axis.title.x = element_blank(),
    legend.position = "bottom",
    legend.box = "vertical",
    legend.direction = "horizontal",
    panel.grid.major.y = element_line(size = .01, color = "grey60"),
    legend.margin = margin(),
    panel.grid.major.x = element_line(size=.01, color="grey60" ),
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.title=element_blank())
#> Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
#> ℹ Please use the `linewidth` argument instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

创建于 2024-05-09,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.