在ggplot中发布填充直方图

Issue filling histogram in ggplot

我试图用与线条相同的颜色填充下图所示的直方图数据,我正在使用以下代码。我使用 fillscale_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()
  
}

在你的循环中尝试这个改变。由于缺少数据,没有产生输出。我还将 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()
  
}