r - ggplot 2 "Error: Discrete value supplied to continuous scale" -
i ask how fix bug described in question title? yesterday, code working fine , plotting routine produced desired graph. woke today , tried add features , got error message.
any clue why , how fix this?
thx
data link: data.csv
code:
# 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 .csv data data frames moroccostats <- as.data.frame(morstats) # changing data in dataframe "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...
moroccostats
has structure
> 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 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 ... ...
when melt year moroccostats.f
, get
> 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" ...
note value
character because periodframe
factor. i'm guessing changed periodframe
went being number character (the "phase 1 (1965 1985)" bits).
since plotting pop_t
, pop_ur
, pop_ru
columns, pull out before melting
morstats.pop <- melt(moroccostats[c("year","pop_t","pop_ur","pop_ru")], id="year")
then don't have worry coercions other types dues irrelevant columns.
Comments
Post a Comment