Eror消息:ggdraw(J)中的错误:对象' J'没有找到另外:`axis.ticks.margin`

时间:2016-12-06 16:05:02

标签: r ggplot2

我在使用此代码时遇到问题,之前一切正常。

我的数据框是:

mydata.Date mydata.B10  mydata.B11  mydata.B12  mydata.B13  mydata.B14
1   2014-10-01  NA  NA  NA  NA  NA
2   2014-10-07  NA  NA  NA  172.5   54.4
3   2014-10-14  NA  NA  669 174.8   65.8
4   2014-10-28  148.50  110.5   620 125.3   42.6
5   2014-10-31  NA  NA  NA  NA  NA
6   2014-11-04  138.70  102.8   497 114.8   44.8
7   2014-11-11  139.10  102.7   558 116.2   42.2
8   2014-11-18  178.40  89.3    766 143.0   48.5
9   2014-11-25  182.71  98.3    809 124.2   17.9
10  2014-12-16  121.20  125.6   644 138.5   46.6
11  2015-02-03  156.40  NA  732 133.8   52.9
12  2015-02-11  160.10  NA  844 114.7   65.2
13  2015-02-17  NA  NA  NA  NA  NA
14  2015-02-25  173.00  123.0   415 137.0   54.0
15  2015-03-03  161.00  141.0   637 153.0   115.0
16  2015-03-10  150.90  109.6   577 140.6   47.9
17  2015-03-17  162.00  143.0   824 139.0   39.0
18  2015-03-19  NA  NA  NA  NA  NA
19  2015-04-14  163.00  95.0    825 115.0   47.0
20  2015-04-17  NA  NA  NA  NA  NA
21  2015-04-22  177.00  282.0   969 96.0    77.0
22  2015-04-28  161.60  189.0   849 104.3   62.3
23  2015-05-19  177.00  129.0   479 210.0   98.0
24  2015-05-21  NA  NA  NA  NA  NA


rm (list=ls())
library(ggplot2)
library(dplyr)
library(tidyr)
library (scales)
library(knitr)
library(cowplot)
setwd("~/Documents/Results")
mydata<-read.csv("ECbar1.csv",header=T)
df <- data.frame(mydata$Date, mydata$B10, mydata$B11,  mydata$B12,mydata$B13, mydata$B14)
df$mydata.Date <- as.Date(df$mydata.Date, "%d/%m/%Y")
L<- ggplot(df, aes(mydata.Date, y=value, xaxt = "n", colour = "red")) +
geom_point(aes(y = mydata$B10, col = "B10") +
geom_point(aes(y = mydata$B11, col = "B11")) +
geom_point(aes(y = mydata$B12, col = "B12")) +
geom_point(aes(y = mydata$B13, col = "B13")) +
geom_point(aes(y = mydata$B14, col = "B14")) +
labs(ggtitle ="Turbidity.", xlab = "Time (month/2015)",
   ylab = "Turbidity(NTU)" ) + scale_x_date(date_breaks = "1 month",   labels = date_format("%m"))
Z<- L + coord_trans(y = "log10") + annotation_logticks(scaled = FALSE)
J<- Z + scale_y_continuous(breaks = c(1,10,100,500,1000), labels =c(1,10,100,500,1000)) 
ggdraw(J) + draw_figure_label(label = "(d)", position ="top.left")

我收到以下错误消息。代码不能像这样工作。

Error: object 'Z' not found
ggdraw(J) + draw_figure_label(label = "(d)", position ="top.left")
Error in ggdraw(J) : object 'J' not found

另外:

Warning message:
axis.ticks.margin is deprecated. Please set margin property  of axis.text instead.

1 个答案:

答案 0 :(得分:1)

我对你的数据采取了一些自由,因为我不确定你想要实现的确切类型的情节。 这有一些帮助吗?

library(ggplot2)
library(dplyr)
library(tidyr)
library (scales)
library(knitr)
library(cowplot)

if (!require("reshape2")) install.packages("reshape2")

mydata$Date <- as.Date(mydata$Date)

melt<-melt(mydata, id='Date')

melt$value<-as.numeric(melt$value)

L<-ggplot(melt) +
  geom_point(aes(x=Date, y=value, col=variable))+
  labs(title="Turbidity", x = "Time (month/2015)",
  y = "Turbidity(NTU)" ) + 
  scale_x_date(date_breaks = "1 month",   labels = date_format("%m"))+
  scale_y_log10(limits=c(1,1000),breaks = c(1,10,100,500,1000))+
  annotation_logticks(scaled = FALSE)

ggdraw(L) + draw_figure_label(label = "(d)", position ="top.left")

enter image description here

数据:

mydata<-structure(list(Date = structure(c(16344, 16350, 16357, 16371, 
16374, 16378, 16385, 16392, 16399, 16420, 16469, 16477, 16483, 
16491, 16497, 16504, 16511, 16513, 16539, 16542, 16547, 16553, 
16574, 16576), class = "Date"), B10 = c("NA", "NA", "NA", "148.5", 
"NA", "138.69999999999999", "139.1", "178.4", "182.71", "121.2", 
"156.4", "160.1", "NA", "173", "161", "150.9", "162", "NA", "163", 
"NA", "177", "161.6", "177", "NA"), B11 = c("NA", "NA", "NA", 
"110.5", "NA", "102.8", "102.7", "89.3", "98.3", "125.6", "NA", 
"NA", "NA", "123", "141", "109.6", "143", "NA", "95", "NA", "282", 
"189", "129", "NA"), B12 = c("NA", "NA", "669", "620", "NA", 
"497", "558", "766", "809", "644", "732", "844", "NA", "415", 
"637", "577", "824", "NA", "825", "NA", "969", "849", "479", 
"NA"), B13 = c("NA", "172.5", "174.8", "125.3", "NA", "114.8", 
"116.2", "143", "124.2", "138.5", "133.80000000000001", "114.7", 
"NA", "137", "153", "140.6", "139", "NA", "115", "NA", "96", 
"104.3", "210", "NA"), B14 = c("NA", "54.4", "65.8", "42.6", 
"NA", "44.8", "42.2", "48.5", "17.899999999999999", "46.6", "52.9", 
"65.2", "NA", "54", "115", "47.9", "39", "NA", "47", "NA", "77", 
"62.3", "98", "NA")), row.names = c(NA, -24L), .Names = c("Date", 
"B10", "B11", "B12", "B13", "B14"), class = c("tbl_df", "tbl", 
"data.frame"))