#r #database #colors #histogram
#r #База данных #Цвет #гистограмма
Вопрос:
Я пытаюсь заполнить тем же цветом, что и строки, данные гистограмм, показанных на рисунке ниже, я использую следующий код. Я пробовал много чего использовать fill
, scale_fill_manual
но безуспешно. Есть идеи, как это исправить?
(stations = unique(DSF_moments$Station))
(station_cols = scales::hue_pal()(length(stations)))
(names(station_cols) = sort(stations))
for (i in 1:length(listDF2))
{
df1 <- as.data.frame(listDF2[[i]])
df1[is.na(df1)] <- 0
plot1 <- ggplot(df1, aes(x = Date, y = DailyMeanStreamflow, colour=Station))
geom_line(size = 1, show.legend = FALSE)
geom_point(size=1.5, shape=21, fill="white",na.rm = TRUE, show.legend = FALSE)
labs(title = "Daily Mean Streamflow", y = "Q[m3/s/Day]", x = "Date")
theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(size=11))
scale_color_manual(values = station_cols)
plot2 <- ggplot(df1, aes(DailyMeanStreamflow, colour=Station))
geom_histogram(show.legend = FALSE)
labs(title = "Daily Mean Streamflow Histogram", y = "Frequency", x="Q[m3/s/Day]")
scale_colour_manual(values = station_cols) scale_fill_manual(values = station_cols)
(Monthly_Streamflow_Station <- df1 %>% group_by(month) %>% summarise(Monthly_Streamflow_Station = mean(DailyMeanStreamflow, na.rm=TRUE)))
plot3 <- ggplot(Monthly_Streamflow_Station, aes(x = month, y = Monthly_Streamflow_Station, colour=unique(df1$Station)))
geom_line(size = 1, show.legend = FALSE)
geom_point(size=1.5, shape=21, fill="white",na.rm = TRUE, show.legend = FALSE)
labs(title = "Monthly Mean Streamflow", y = "Q[m3/s/Month]", x = "Month")
theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(size=11))
scale_x_continuous (breaks=seq(1,12,by=1))
scale_color_manual(values = station_cols)
plot4 <- ggplot(Monthly_Streamflow_Station, aes(Monthly_Streamflow_Station, colour=unique(df1$Station)))
geom_histogram(show.legend = FALSE)
labs(title = "Monthly Mean Streamflow Histogram", y = "Frequency", x="Q[m3/s/Month]")
scale_colour_manual(values = station_cols)
(Annual_Streamflow_Station <- df1 %>% group_by(year) %>% summarise(Annual_Streamflow_Station = mean(DailyMeanStreamflow, na.rm=TRUE)))
plot5 <- ggplot(Annual_Streamflow_Station, aes(x = year, y = Annual_Streamflow_Station, colour=unique(df1$Station)))
geom_line(size = 1, show.legend = FALSE)
geom_point(size=1.5, shape=21, fill="white",na.rm = TRUE, show.legend = FALSE)
labs(title = "Annual Mean Streamflow", y = "Q[m3/s/Year]", x = "Year")
theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(size=11))
scale_color_manual(values = station_cols)
plot6 <- ggplot(Annual_Streamflow_Station, aes(Annual_Streamflow_Station,colour=unique(df1$Station)))
geom_histogram(show.legend = FALSE)
labs(title = "Annual Mean Streamflow Histogram", y = "Frequency", x="Q[m3/s/Year]")
scale_colour_manual(values = station_cols)
grid.arrange(grobs=list(plot1, plot2, plot3, plot4, plot5, plot6), ncol = 2, nrow = 3)
name5<- paste("Plots","_", siteNumber[i], ".png", sep="")
g <- arrangeGrob(plot1, plot2, plot3, plot4, plot5, plot6, ncol = 2, nrow = 3)
ggsave(g,filename = name5,width=22,height=11,units="in",dpi=500)
dev.off()
}
Комментарии:
1. Пожалуйста, поделитесь образцом ваших данных, чтобы воспроизвести проблему!
2. Если вы хотите сопоставить цвета заливки, которые вам нужны
fill
, нетcolor
.В гистограммах цвета — это контуры штрихов, а заливки — это внутренности штрихов.
Ответ №1:
Попробуйте это изменение в своем цикле. Вывод не получен из-за нехватки данных. Я также изменил scale_color_*()
, scale_fill_*()
где необходимо, как сказал великий @aosmith, что для гистограмм требуется включить опцию заполнения:
#Code
for (i in 1:length(listDF2))
{
df1 <- as.data.frame(listDF2[[i]])
df1[is.na(df1)] <- 0
plot1 <- ggplot(df1, aes(x = Date, y = DailyMeanStreamflow, colour=Station))
geom_line(size = 1, show.legend = FALSE)
geom_point(size=1.5, shape=21, fill="white",na.rm = TRUE, show.legend = FALSE)
labs(title = "Daily Mean Streamflow", y = "Q[m3/s/Day]", x = "Date")
theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(size=11))
scale_color_manual(values = station_cols)
plot2 <- ggplot(df1, aes(DailyMeanStreamflow, fill=Station))
geom_histogram(show.legend = FALSE)
labs(title = "Daily Mean Streamflow Histogram", y = "Frequency", x="Q[m3/s/Day]")
scale_fill_manual(values = station_cols)
(Monthly_Streamflow_Station <- df1 %>% group_by(month) %>% summarise(Monthly_Streamflow_Station = mean(DailyMeanStreamflow, na.rm=TRUE)))
plot3 <- ggplot(Monthly_Streamflow_Station, aes(x = month, y = Monthly_Streamflow_Station, colour=unique(df1$Station)))
geom_line(size = 1, show.legend = FALSE)
geom_point(size=1.5, shape=21, fill="white",na.rm = TRUE, show.legend = FALSE)
labs(title = "Monthly Mean Streamflow", y = "Q[m3/s/Month]", x = "Month")
theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(size=11))
scale_x_continuous (breaks=seq(1,12,by=1))
scale_color_manual(values = station_cols)
plot4 <- ggplot(Monthly_Streamflow_Station,
aes(Monthly_Streamflow_Station,
fill=unique(df1$Station)))
geom_histogram(show.legend = FALSE)
labs(title = "Monthly Mean Streamflow Histogram", y = "Frequency", x="Q[m3/s/Month]")
scale_fill_manual(values = station_cols)
(Annual_Streamflow_Station <- df1 %>% group_by(year) %>% summarise(Annual_Streamflow_Station = mean(DailyMeanStreamflow, na.rm=TRUE)))
plot5 <- ggplot(Annual_Streamflow_Station, aes(x = year, y = Annual_Streamflow_Station, colour=unique(df1$Station)))
geom_line(size = 1, show.legend = FALSE)
geom_point(size=1.5, shape=21, fill="white",na.rm = TRUE, show.legend = FALSE)
labs(title = "Annual Mean Streamflow", y = "Q[m3/s/Year]", x = "Year")
theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(size=11))
scale_color_manual(values = station_cols)
plot6 <- ggplot(Annual_Streamflow_Station,
aes(Annual_Streamflow_Station,
fill=unique(df1$Station)))
geom_histogram(show.legend = FALSE)
labs(title = "Annual Mean Streamflow Histogram", y = "Frequency", x="Q[m3/s/Year]")
scale_fill_manual(values = station_cols)
grid.arrange(grobs=list(plot1, plot2, plot3, plot4, plot5, plot6), ncol = 2, nrow = 3)
name5<- paste("Plots","_", siteNumber[i], ".png", sep="")
g <- arrangeGrob(plot1, plot2, plot3, plot4, plot5, plot6, ncol = 2, nrow = 3)
ggsave(g,filename = name5,width=22,height=11,units="in",dpi=500)
dev.off()
}