将颜色保存在 R 的调色板中
Save colours in a palette in R
我想保存 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()
利用上面代码中的color函数,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”
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)
由于您尚未共享任何数据,因此未经测试,但至少应该让您非常接近。如果您需要更多帮助,请分享可重现的示例。
另一种方法是保持所有数据帧的因子水平相同,参见示例:
# 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)
)
我想保存 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()
利用上面代码中的color函数,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”
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)
由于您尚未共享任何数据,因此未经测试,但至少应该让您非常接近。如果您需要更多帮助,请分享可重现的示例。
另一种方法是保持所有数据帧的因子水平相同,参见示例:
# 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)
)