weiß jemand wie ich diese Fehlermeldung in meinem unten angehängten Script beheben kann:
Das Format meiner CSV sieht so aus:Error in charToDate(x) :
Zeichenkette ist nicht in einem eindeutigen Standardformat
yyyy-mm,variableA,variableB
der erzeugte Datensatz so:
Monat AA BB year country
1 2004-01 53 13 2004 United States
2 2004-02 54 13 2004 United States
Ich möchte einfach die beiden Variablen AA und BB für die Monate plotten und das Jahr als Tick haben:
Code: Alles auswählen
wants <- c("splines","MASS","ggplot2","cowplot", "scales","ggsci");
has <- wants %in% rownames(installed.packages());
if(any(!has)) install.packages(wants[!has]);
lapply(wants,require,character.only=TRUE);
## Datensatz einlesen
filename <- "USA.csv"
con <- file(filename, open = "r")
linecount <- 0
stringdata <- ""
while (length(oneLine <- readLines(con, n = 1, warn = FALSE)) > 0) {
linecount <- linecount + 1
if (linecount < 3) {
filename <- paste0(filename,oneLine)
}
# get headers at line 5
if (linecount == 3) rowheaders = strsplit(oneLine, ",")[[1]]
# skip firt 5 lines
if (linecount > 3) {
# break when there is no more main data
if (gsub(pattern=",", x=oneLine, replacement="") == "") break
stringdata <- paste0(stringdata,oneLine,"\n")
}
}
close(con)
filename <- gsub(x=filename, pattern=' |;|:','_')
USA <- read.table(textConnection(stringdata), sep=",", header=FALSE, stringsAsFactors = FALSE)
names(USA) <- rowheaders
USA$country<-"United States"
head(USA)
newData <- rbind(USA)
newData$year <- sapply(strsplit(as.character(newData$Monat), "-"), `[`, 1)
newData$country <- newData$country
newData<- newData[c("Monat", "AA", "BB", "year","country")]
head(newData)
pl<- ggplot(newData,aes(x="Monat")) +
theme_minimal()+
xlab("Year")+
ylab("Popularity")+
scale_colour_manual("Search queries", labels = c("Variable A", "Variable B"), breaks = c("AA", "BB"), values = c("gray","black"))+
geom_point (aes(y = AA, colour = "AA"), size=1.3, alpha=5/10) +
geom_point (aes(y = BB, colour ="BB"), size=6)+
#stat_smooth(aes(y=AA, group=1, colour="Urt"), method=lm, formula = y ~ poly(x,2, raw=FALSE), level=0.95)+
#stat_smooth(aes(y=BB, group=1, colour="AH"), method=lm, formula = y ~ poly(x,2, raw=FALSE), level=0.95) +
scale_x_date(labels = date_format("%Y"), breaks = date_breaks("year"),limits = c(as.Date("2004-01"), as.Date("2017-08"))) +
#facet_grid(country ~.) +
theme_replace()+
theme(legend.position="bottom")
pl
Vielen Dank