如何在ggplot

时间:2016-12-20 14:27:35

标签: r ggplot2 time-series timeserieschart

我想绘制单个散点图,显示两个月降雨量的不同颜色。例如A站Jan降雨量值为红色,2月为黄色,B站为1月降雨量值为蓝色,2月为绿色等等,其中显示为图例。此外,我想为两个站点数据添加一条平滑线,这些数据也出现在图例中,如A站的红色平滑线和B站的蓝色。 在此链接中,您可以找到两个站的CSV数据: https://drive.google.com/file/d/0B3fQ9_46L-O0TjJwYmF6UThNSGs/view?usp=sharing https://drive.google.com/file/d/0B3fQ9_46L-O0ZXVYb3lzZDBZaHM/view?usp=sharing

以下是我尝试但无法成功的代码。

#reading csv file of ramoili station of rautahat[Scatterplot of two stations][1]
ram = read.csv('preci_ramoili.csv',header=TRUE, stringsAsFactors=FALSE)
#reading CSV file of gaur station of rautahat
gaur= read.csv('preci_Gaur.csv',header=TRUE, stringsAsFactors=FALSE)
#gaur rainfall
rain <- data.frame(index(agg),stack(as.data.frame(coredata(agg))))
rain
head(rain)
tail(rain)
names(rain)[1] <- "Year"
names(rain)[2] <- "Rainfall"
names(rain)[3] <- "Month"
#ramoili rainfall
rain1<-data.frame(index(core),stack(as.data.frame(coredata(core))))
rain1
head(rain1)
names(rain1)[1] <- "Year"
names(rain1)[2] <- "Rainfall"
names(rain1)[3] <- "Month"
head(rain1)
#ramoili premonsoon rainfall
rain1_pre<-data.frame(index(core[,3:5]),stack(as.data.frame(coredata(core[,3:5]))))
head(rain_pre)
tail(rain1_pre)
names(rain1_pre)[1] <- "Year"
names(rain1_pre)[2] <- "Rainfall"
names(rain1_pre)[3] <- "Month"
#ggplot of two stations gaur and ramoili yearly rainfall of rautahat in same plot
p9 <- ggplot(rain, aes(x =Year, y=Rainfall, size=Rainfall)) + geom_point(shape = 21,color = "#000000", fill = "#40b8d0") + 
  geom_smooth(aes(fill="Gaur"), colour="darkblue", size=1)

p10 <- p9 + geom_point(data=rain1, aes(x =Year, y=Rainfall, color=Month )) + 
  geom_smooth(data=rain1, aes(fill="Ramoili"), colour="red", size=1)+ 
  ggtitle(" Yearly rainfall at two stations of Rautahat")+
  scale_fill_manual(name="Stations", values=c("blue", "red"))
print(p10)

1 个答案:

答案 0 :(得分:-2)

如果没有完整的样本数据,并且使用您提供的样本数据,我已经说明了一种方法。

我不确定为什么你想要每个电台的不同月份颜色,我认为使用facet可以更好地说明这种差异。如果没有,我仍然建议保持月份颜色一致并删除构面网格。

您需要根据自己的喜好修改轴,标题,比例等。

library(dplyr)
library(ggplot2)
library(purrr)
library(tidyr)
library(trend)

gaur <- list.files("~/Desktop", pattern = "Gaur", full.names = TRUE)  %>% 
  read.csv() %>% 
  mutate(station = "Gaur")

ramoili <- list.files("~/Desktop", pattern = "ramoili", full.names = TRUE)  %>% 
  read.csv() %>% 
  mutate(station = "Ramoli")

plot_data <- bind_rows(gaur, ramoili) %>% 
  gather(month, rainfall, -Year, -station) 


ggplot(plot_data, aes(x = Year, y = rainfall)) +
  geom_line(aes(color = month)) +
  geom_point(aes(color = month), show.legend = FALSE) +
  geom_smooth(aes(fill = station), size = 0.1) +
  scale_x_continuous(breaks = scales::pretty_breaks()) +
  scale_fill_manual(name = "Stations", values = c("blue", "red")) +
  facet_grid(month ~ station) +
  theme_minimal()

enter image description here

对于模特:

models <- bind_rows(gaur, ramoili) %>% 
      select(-Year) %>% 
      nest(-station) %>% 
      mutate(ts_data = map(data, ~ts(.x, frequency = 1, start = c(1984,1)))) %>% 
      mutate(mk_model = map(ts_data, mk.test),
             sens_slope = map(ts_data, sens.slope))

> models$mk_model[1]
[[1]]
Mann-Kendall Test

two-sided homogeinity test
H0: S = 0 (no trend)
HA: S != 0 (monotonic trend)

Statistics for total series
      S     varS    Z    tau     pvalue
1 -1637 39765.67 -8.2 -0.275 2.2289e-16

> models$sens_slope[1]
[[1]]

Sen's slope and intercept


slope:  0
95 percent confidence intervall for slope
0 0

intercept: 14.9
nr. of observations: 384