【问题标题】:Save colours in a palette in R在 R 的调色板中保存颜色
【发布时间】:2021-01-10 16:08:27
【问题描述】:

我想保存自动 ggplot 函数分配给绘图中每个站点的颜色。我想将分配给每个站点的颜色保存在调色板中,以便在其他绘图中再次使用:

ggplot(DSF_moments, aes(x=year, y=max, group = station, colour = station)) + 
  geom_line(size = 1)  + 
  geom_point(size=1.5, shape=21, fill="white") + 
  labs(y ="Annual max flow [m3/s]", x = "year", title = "Annual Maximum Streamflow", size = 50) +
  theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(angle = 90, size=11)) + scale_x_continuous (breaks=seq(min(DSF_moments$year),max(DSF_moments$year),by=2)) +
  scale_y_continuous (breaks=seq(min(DSF_moments$max),max(DSF_moments$max),by=5000))
dev.copy(png,"Plot_Max_Annual_RawData.png",width=22,height=11,units="in",res=100)
dev.off()

使用上面代码中的颜色函数,ggplot为每个站分配一个颜色,我不想改变颜色,我只想知道每个站分配哪种颜色。我们的想法是在每个站点分别生成一个绘图后,但保持先前在第一个公共绘图中分配给所有站点的颜色。

for (i in 1:length(listDF2)) 
{
  df1 <- as.data.frame(listDF2[[i]])
  df1[is.na(df1)] <- 0
  temp_plot <- ggplot(df1, aes(x = day, y = DailyMeanStreamflow, colour=Station[i])) +
  geom_line(size = 1)  + 
  geom_point(size=1.5, shape=21, fill="white") + 
  facet_wrap(~ month, ncol = 3) +
  labs(title = "Daily Mean Streamflow",
       subtitle = "Data plotted by month",
       y = "Daily Mean Streamflow [m3/s]", x="Days") + 
  scale_x_continuous (breaks=seq(1,max(df1$day),by=1)) + theme(axis.text.x = element_text(size=9))

  print(temp_plot)

  name4<- paste("DailyStreamflow_byMonth","_", siteNumber[i], ".png", sep="")
  ggsave(temp_plot,filename = name4,width=22,height=11,units="in",dpi=500)
  dev.off()
}

我现在想为每个图表分配之前分配的颜色。如何将ggplot分配的默认颜色保存到每个站点?

电台的格式为 chr:"094985005","09498501","09489500"

【问题讨论】:

  • 创建您自己的调色板或使用上面链接中的函数重新生成 ggplots 默认颜色。
  • 不,这不是回答,因为我不想模仿,我想保存它们
  • 请从链接中查看这个答案 - stackoverflow.com/a/34241551/680068 如果它不起作用,请告诉我,我会重新打开。
  • 另外,提供可重现的数据。
  • 这个答案说明了如何创建调色板,而不是如何将每种颜色分配给每个站点。我将修改问题以更好地解释它

标签: r ggplot2 colors palette


【解决方案1】:

另一种方法是保持所有数据帧的因子水平相同,参见示例:

# example data
listdf <- list(
  data.frame(x = 1:1, y = 1:2, station = c("094985005","09498501")),
  data.frame(x = 1:1, y = 2:3, station = c("09498501","09489500"))
)

#fix levels
allStations <- sort(unique(unlist(lapply(listdf, "[[", "station"))))
listdf[[1]]$station <- factor(listdf[[1]]$station, levels = allStations)
listdf[[2]]$station <- factor(listdf[[2]]$station, levels = allStations)

#plot side by side to illustrate the same levels
cowplot::plot_grid(
  ggplot(listdf[[1]], aes(x, y, col = station)) +
    geom_point(size = 5) +
    scale_color_discrete(drop = FALSE) +
    ylim(0, 3),
  
  ggplot(listdf[[2]], aes(x, y, col = station)) +
    geom_point(size = 5) +
    scale_color_discrete(drop = FALSE) +
    ylim(0, 3)
)

【讨论】:

  • 我的回答完全符合 OP 的要求。这个答案显示了一种更好的方法——我将在自己的代码中使用它来实现 OP 的目标。 (除非他们想保存颜色以供ggplot 之外使用...)
  • @GregorThomas 是的,OP 想要绘制多个数据帧,其中级别不同但颜色应该匹配以使绘图具有可比性。因此,我们只需要确保每个站点都分配了完全相同的颜色。修复级别很好,在我自己的代码中,我会为每个站点分配自定义颜色,并使用 scale_color_identity。
【解决方案2】:

answers linked in the comments 有大量重要信息,我在这里展示的内容就是基于这些信息。

# Generate the colors 
stations = unique(DSF_moments$station)
station_cols = scales::hue_pal()(length(stations))
# Assign them alphabetically (ggplot's default, which you don't seem to modify) names(station_cols) = sort(stations)

# use these colors for (some) of these stations in a plot with 
scale_color_manual(values = station_cols)

由于您尚未共享任何数据,因此未经测试,但至少应该让您非常接近。如果您需要更多帮助,请分享一个可重现的示例。

【讨论】:

    猜你喜欢
    • 2015-01-28
    • 2014-11-01
    • 2012-06-03
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 2014-11-03
    相关资源
    最近更新 更多