2013-05-13 9 views
6

Vorrei chiedere come risolvere questo errore descritto nel titolo della domanda? Proprio ieri, il codice funzionava bene e la routine di tracciatura ha prodotto il grafico desiderato. Mi sono svegliato oggi e ho provato ad aggiungere alcune funzionalità e ho ricevuto il messaggio di errore.ggplot 2 "Errore: valore discreto fornito a scala continua"

Qualsiasi indizio sul perché e su come risolvere questo problema?

Thx

Data link: Data.csv

codice:

# Loading data 
    morStats <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/DissertationDraft/MoroccoCGE-CC_Stats.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE) 

    # Transferring the .csv data into data frames 
    moroccostats <- as.data.frame(morStats) 

    # Changing the data in the dataframe to "as.numeric" 
    moroccostats[3:38] <- sapply(moroccostats[3:38],as.numeric) 
    moroccostats <- droplevels(moroccostats) 

    # reorder 
    moroccostats <- transform(moroccostats,year=factor(year,levels=unique(year))) 

    # Load packages 
    library(reshape2) 
    library(ggplot2) 
    library(lattice) 
    library(grid) 
    library(plyr) 
    library(gridExtra) 
    library(scales) 

    #---------------------------------------------------------------------- 
    # Figure 1: Evolution of population (in absolute terms) 1960-2050 
    #--------------------------------------------------------------------- 

    #_Code_Begin... 

    moroccostats.f <- melt(moroccostats, id="year") 
    morstats.pop <- moroccostats.f[moroccostats.f$variable %in% c("pop_t","pop_ur","pop_ru"),] 

    Figure1 <- ggplot(data=morstats.pop,aes(x=factor(year), y=value,colour=variable)) 
    Figure1 + geom_line(aes(group=factor(variable)),size=1) + geom_point() + scale_colour_manual("Population",labels=c("Total","Urban","Rural"),values = c("black","red","blue")) + 
    labs(y="Population (in 1000)") + 
    theme(axis.text.x = element_text(colour = 'black', angle = 90, size = 13, hjust = 1, vjust = 0.5),axis.title.x = element_blank()) + 
    theme(axis.text.y = element_text(colour = 'black', size = 13, hjust = 0.5, vjust = 0.5), axis.title.y = element_text(size = 10, hjust = 0.5, vjust = 0.2, face = 'bold')) + 
    scale_x_discrete(breaks = seq(1960, 2050, by=2)) + 
    scale_y_continuous(breaks = seq(0, 42000, by=5000)) 

    #_Code_End... 

risposta

10

moroccostats ha la struttura

> str(moroccostats) 
'data.frame': 91 obs. of 38 variables: 
$ year   : Factor w/ 91 levels "1960","1961",..: 1 2 3 4 5 6 7 8 9 10 ... 
$ periodframe  : Factor w/ 4 levels "0","Phase 1 (1965 to 1985)",..: 1 1 1 1 1 2 2 2 2 2 ... 
$ pop_t   : num 11635 11897 12177 12473 12785 ... 
$ pop_ur   : num 3395 3547 3703 3862 4026 ... 
... 

Quando si sciolgono che per anno per ottenere moroccostats.f, si ottiene

> str(moroccostats.f) 
'data.frame': 3367 obs. of 3 variables: 
$ year : Factor w/ 91 levels "1960","1961",..: 1 2 3 4 5 6 7 8 9 10 ... 
$ variable: Factor w/ 37 levels "periodframe",..: 1 1 1 1 1 1 1 1 1 1 ... 
$ value : chr "0" "0" "0" "0" ... 

Nota che value è un personaggio, perché periodframe era un fattore. Immagino che ciò che è cambiato sia stato il fatto che lo periodframe passasse dall'essere un numero a un personaggio (i bit "Fase 1 (dal 1965 al 1985)").

Dal momento che si sta tramando solo le pop_t e pop_ur e pop_ru colonne, estrarre quelli fuori prima della fusione

morstats.pop <- melt(moroccostats[c("year","pop_t","pop_ur","pop_ru")], id="year") 

allora non dovete preoccuparvi di coercizioni ad altri tipi di quote alle colonne irrilevanti.

+0

Ha funzionato bene, grazie per l'aiuto :) – iouraich

Problemi correlati