目的是使用 ggplot 在图表上的 3 个站绘制至少 3 个我的每月变量。 x 轴显示月份

the aim is to plot at least 3 of my monthly variables across the 3 stations on a graph using ggplot. with the x axis showing the months

这是我的数据集的副本:通过 5 月、6 月、7 月、8 月、9 月在 3 个站点收集的参数值。我正在尝试绘制整个月份的降雨量、气温和水温对于使用 ggplot 的每个站点。我不断收到如下所示的错误消息。

我该如何处理正确绘制这些变量的代码。有没有可能我没有正确安排列或行?

这是我尝试过的:

library(readxl)
badagry_stations <- read_excel("~/903 -/badagry stations.xlsx")
View(badagry_stations)
require(ggplot2)
library(ggplot2)
data.frame(badagry_stations)
tidyr::gather(badagry_stations)
summary(data.frame(badagry_stations))
ggplot(badagry_stations, aes(rainfall)) + geom_line(aes(air temperature)), colour = "red")
ggplot(dat, aes(rain, turbidity, colour = 'station'))

所以我尝试重新排列数据集,如下所示,这可能是问题所在

   Station month Air.temperature Water.temperature...C. Rainfall..mm.
1  Station 1   MAY              31                  30.00         275.6
2  Station 1  JUNE              28                  28.67         236.2
3  Station 1  JULY              28                  29.00         142.7
4  Station 1   AUG              28                  27.00         257.6
5  Station 1  SEPT              28                  30.00         329.5
6  Station 2   MAY              29                  28.00         273.6
7  Station 2  JUNE              28                  28.67         236.2
8  Station 2  JULY              28                  29.00         142.7
9  Station 2   AUG              28                  27.00         257.6
10 Station 2  SEPT              28                  30.00         329.5
11 Station 3   MAY              30                  29.00         277.6
12 Station 3  JUNE              28                  28.67         236.2
13 Station 3  JULY              28                  29.00         142.7
14 Station 3   AUG              28                  27.00         257.6
15 Station 3  SEPT              28                  30.00         329.5
   Turbidity..NTU. TSS..mg.L. TDS..mg.L. pH...25.C Conductivity..µS.cm.
1             4.40          2    10199.0      7.22              18100.0
2             6.72          2    14701.0      7.29              24100.0
3             6.66         12     8618.0      7.35              13900.0
4             8.35          4    10450.1      7.34              16855.0
5            42.70         33     1712.0      7.34               2920.2
6             3.95          3     9873.0      7.76              15800.0
7             3.19          1    13620.0      7.45              22700.0
8            16.25         18     5144.1      7.45               8870.0
9            27.40         18     4912.6      7.61               8470.0
10           29.60         26     2482.7      7.31               4070.0
11            4.15          2     9964.0      7.16              16700.0
12            4.56          2    14100.0      7.48              23500.0
13           14.07         12     6182.0      7.44              10660.0
14           22.31         17     5063.4      7.63               8730.3
15           35.31         31     2696.2      7.32               4420.1

我也试过这个数据集重排

data.frame(badagry_stations) 
                  month                 MAY.1                JUNE.1
    1                  Air temperature                    31                    28
    2           Water temperature (°C)                    30                 28.67
    3                    Rainfall (mm)    275.60000000000002                 236.2

但我不断收到此错误代码:

ggplot(badagry_stations, aes(rainfall)) + 
geom_line(aes(air temperature)), colour = "red")
        
#> Error: unexpected symbol in "ggplot(badagry_stations, aes(rainfall)) + 
#> geom_line(aes(air temperature"

处理 multi-word 列名称的一种简单方法是使用下划线分隔它们。

示例代码:

#badagry_stations<-data.frame(badagry_stations) 
    #tidyr::gather(badagry_stations)
    #summary(data.frame(badagry_stations))
 

    library(ggplot2)
    library(ggthemes)
       
    ggplot(badagry_stations, aes(x=`Rainfall..mm.`,y=`Turbidity..NTU.`, group=Station)) + 
      geom_line(aes(color=Station), size=2)+
      theme_wsj()+
      labs(x="Rainfall(mm)", y="Turbidity(NTU)", fcolor="Station number" )+
      theme(axis.text.x = element_text(hjust = 1, face="bold", size=12, color="black"), 
            axis.title.x = element_text(face="bold", size=16, color="black"),
            axis.text.y = element_text(face="bold", size=12, color="black"),
            axis.title.y = element_text(face="bold", size=16, color="black"),
            strip.text = element_text(size=10, face="bold"),
            plot.title = element_text(hjust = 0.5,size=20, face="bold"),
            legend.text = element_text( color = "black", size = 16,face="bold"),
            legend.position="bottom",
            legend.box = "horizontal",
            legend.title = element_blank())

剧情:

或按 month 变量分组

示例代码:

    library(ggplot2)
    library(ggthemes)

badagry_stations$month<-factor(badagry_stations$month,levels=c("MAY","JUNE","JULY","AUG","SEPT"))

ggplot(badagry_stations, aes(x=`Rainfall..mm.`,y=`Turbidity..NTU.`, group=month)) + 
  geom_bar(aes(fill=month), stat="identity", width=1)+
  theme_wsj()+
  labs(x="Rainfall(mm)", y="Turbidity(NTU)", fcolor="Station number" )+
  theme(axis.text.x = element_text(hjust = 1, face="bold", size=12, color="black"), 
        axis.title.x = element_text(face="bold", size=16, color="black"),
        axis.text.y = element_text(face="bold", size=12, color="black"),
        axis.title.y = element_text(face="bold", size=16, color="black"),
        strip.text = element_text(size=10, face="bold"),
        plot.title = element_text(hjust = 0.5,size=20, face="bold"),
        legend.text = element_text( color = "black", size = 16,face="bold"),
        legend.position="bottom",
        legend.box = "horizontal",
        legend.title = element_blank())

剧情:

示例数据:

badagry_stations<-structure(list(Station = c("Station 1", "Station 1", "Station 1", 
"Station 1", "Station 1", "Station 2", "Station 2", "Station 2", 
"Station 2", "Station 2", "Station 3", "Station 3", "Station 3", 
"Station 3", "Station 3"), month = c("MAY", "JUNE", "JULY", "AUG", 
"SEPT", "MAY", "JUNE", "JULY", "AUG", "SEPT", "MAY", "JUNE", 
"JULY", "AUG", "SEPT"), Air.temperature = c(31, 28, 28, 28, 28, 
29, 28, 28, 28, 28, 30, 28, 28, 28, 28), Water.temperature = c(30, 
28.67, 29, 27, 30, 28, 28.67, 29, 27, 30, 29, 28.67, 29, 27, 
30), Rainfall..mm. = c(275.6, 236.2, 142.7, 257.6, 329.5, 273.6, 
236.2, 142.7, 257.6, 329.5, 277.6, 236.2, 142.7, 257.6, 329.5
), Turbidity..NTU. = c(4.4, 6.72, 6.66, 8.35, 42.7, 3.95, 3.19, 
16.25, 27.4, 29.6, 4.15, 4.56, 14.07, 22.31, 35.31), TSS..mg.L. = c(2L, 
2L, 12L, 4L, 33L, 3L, 1L, 18L, 18L, 26L, 2L, 2L, 12L, 17L, 31L
), TDS..mg.L. = c(10199, 14701, 8618, 10450.1, 1712, 9873, 13620, 
5144.1, 4912.6, 2482.7, 9964, 14100, 6182, 5063.4, 2696.2), pH...25.C = c(7.22, 
7.29, 7.35, 7.34, 7.34, 7.76, 7.45, 7.45, 7.61, 7.31, 7.16, 7.48, 
7.44, 7.63, 7.32), Conductivity..µS.cm. = c(18100, 24100, 13900, 
16855, 2920, 15800, 22700, 8870, 8470, 4070, 16700, 23500, 10660, 
8730.3, 4420.1)), class = "data.frame", row.names = c(NA, -15L
))