在ggplot中获取错误:一元运算符的参数无效

问题描述 投票:0回答:1

我正在研究几个月的一些数据值。

我为每种数据类型使用两个geom_lines()函数。我得到的情节和我想要的一切,但当我尝试用“月”标记x轴时,我收到一个错误。

Error in +xlab("Months") : invalid argument to unary operator

我的情节代码是:

library(ggplot2)
library(officer)

data <- "U://30-Power & Water//25 Renewables//WORK//Data//PVPlanner//PVPlanner.csv"

data <- read.table(data,skip = 36, header = T,  sep=";")

months <- c("Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec")
data <- data.frame(months, data[1:12,2:6])
data$months <-factor(data$months,levels = c("Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"))

plot <- ggplot(data) +geom_path(aes(x= months, y= GHId, col = "GHId", group = 1)) + geom_path( aes(x = months,y = Diffd,colour = "Diffd", group = 1) )+ scale_colour_manual("", breaks =c("GHId","Diffd"), values = c( "red","Blue"))
+xlab("Months")+ylabs("Irradiation")+ggtitle("Global and Diffuse horizontal irradiance")
plot

我的数据看起来像这样:

  GHId GHIm Diffd Diffm  T24
1     3.27  101  0.92    29 13.3
2     3.92  110  1.23    34 13.3
3     5.30  164  1.58    49 13.9
4     6.18  185  1.89    57 14.9
5     6.93  215  2.00    62 16.4
6     7.53  226  1.80    54 18.4
7     7.42  230  1.87    58 21.1
8     7.06  219  1.58    49 22.0
9     5.97  179  1.39    42 21.3
10    4.50  140  1.26    39 18.7
11    3.53  106  0.99    30 15.9
12    2.90   90  0.86    27 13.3

情节完全没有问题。我只是在尝试给轴标签时才会出错。我不知道为什么会这样。

我们将不胜感激。

问候

r plot ggplot2 label
1个回答
0
投票

对于要一起评估的ggplot2中的多个层,前一行应以+结束以指示继续到下一行,否则该前一行被视为完成并终止。

在这种情况下,下一行中的+xlab被读作新的和单独命令的开头,并独立评估(一元运算符),从而导致错误。

正确的代码是:

plot <- ggplot(data) +
    geom_path(aes(x= months, y= GHId, col = "GHId", group = 1)) + 
    geom_path( aes(x = months,y = Diffd,colour = "Diffd", group = 1) ) + 
    scale_colour_manual("", breaks =c("GHId","Diffd"), values = c( "red","Blue")) +
    xlab("Months") + ylabs("Irradiation") +
    ggtitle("Global and Diffuse horizontal irradiance")

plot
© www.soinside.com 2019 - 2024. All rights reserved.