[R] ggplot y-axis labels are not continuous

David Winsemius dwinsemius at comcast.net
Sun Sep 14 18:27:54 CEST 2014


On Sep 14, 2014, at 1:15 AM, Mohan Radhakrishnan wrote:

> Thanks Dennis.
>
> df.melted$value<-as.numeric(df.melted$value)

See the FAQ about converting factors to numeric.

The more typical way of making that conversion is:

  df.melted$value<-as.numeric(as.character(df.melted$value))

... although the FAQ points out that this is more efficient:

  df.melted$value<-levels(df.melted$value)[ df.melted$value]


> df.melted$value <- format(df.melted$value, scientific = FALSE)

So you started out with a factor and then you converted back to a  
character with `format`? Was that what was really desired? You  
probably want to leave it as numeric and then you plotting routines  
will "know" what type of axis to create for the data features. If you  
are in the ggplot world view then there will be functions like  
scale_x_continuous that appears to offer a 'labels' parameter. Reading  
the help page for scale_continuous it seems that you might want to try  
formating the result of the `waiver()` function, but the somewhat  
older version of the package I have on this aging laptop does not have  
any worked examples.


>
>
> Mohan
>
> On Sun, Sep 14, 2014 at 9:22 AM, Dennis Murphy <djmuser at gmail.com>  
> wrote:
>
>> Hi:
>>
>> Try
>>
>> str(df.melted)
>>
>> I'm guessing value is a factor. It needs to be numeric or integer.
>>
>> Dennis
>>
>>
>> On Sat, Sep 13, 2014 at 12:53 PM, Mohan Radhakrishnan
>> <radhakrishnan.mohan at gmail.com> wrote:
>>> Hi,
>>>
>>> This is the code to create a ggplot. The plot is rendered but the  
>>> y-axis
>>> labels are not continuous. So the lines are split.
>>>
>>> Moreover multiple y-axis labels overwrite each other due to this.  
>>> How
>> can I
>>> fix this?
>>>
>>> If I try to set ylim I get "
>>>
>>> *Discrete value supplied to continuous scale"*
>>>
>>> library(RJSONIO)
>>> library(ggplot2)
>>> this.dir <- dirname(parent.frame(2)$ofile)
>>> setwd(this.dir)
>>>
>>> airlines   = fromJSON("json")
>>> df <- sapply(airlines$data,unlist)
>>> df <- data.frame(t(df))
>>> colnames(df) <- c( (airlines[[1]][[1]])[[2]],
>>> gsub("[^A-Z]","",(airlines[[1]][[2]])[[2]]),
>>> gsub("[^A-Z]","",(airlines[[1]][[3]])[[2]] ),
>>> gsub("[^A-Z]","",(airlines[[1]][[4]])[[2]]), (airlines[[1]][[5]]) 
>>> [[2]],
>>> (airlines[[1]][[6]])[[2]], (airlines[[1]][[7]])[[2]],
>>> (airlines[[1]][[8]])[[2]],
>>> (airlines[[1]][[9]])[[2]],(airlines[[1]][[10]])[[2]] )
>>>
>>> df.melted <- melt(df, id = "YEAR")
>>> print(ggplot(data = df.melted, aes(x = YEAR, y = value, color =
>> variable))
>>> +geom_point() + theme(axis.text.x = element_text(angle = 90, hjust  
>>> = 1))
>> +
>>> ylab(""))
>>> dev.off()
>>>
>>>
>>>> head(df)
>>>
>>>     YEAR INTERNATIONALACMINNOS DOMESTICACMINNOS TOTALACMINNOS
>>>
>>> 1 1995-96                 92515           314727        407242
>>>
>>> 2 1996-97                 94884           324462        419346
>>>
>>> 3 1997-98                 98226           317531        415757
>>>
>>> 4 1998-99                 99563           325392        424955
>>>
>>> 5 1999-00                 99701           368015        467716
>>>
>>> 6 2000-01                103211           386575        489786
>>>
>>>  INTERNATIONAL PAX (IN NOS) DOMESTIC PAX (IN NOS) TOTAL PAX (IN NOS)
>>>
>>> 1                   11449756              25563998            
>>> 37013754
>>>
>>> 2                   12223660              24276108            
>>> 36499768
>>>
>>> 3                   12782769              23848833            
>>> 36631602
>>>
>>> 4                   12916788              24072631            
>>> 36989419
>>>
>>> 5                   13293027              25741521            
>>> 39034548
>>>
>>> 6                   14009052              28017568            
>>> 42026620
>>>
>>>  INTERNATIONAL FREIGHT (IN MT) DOMESTIC FREIGHT (IN MT) TOTAL  
>>> FREIGHT
>> (IN
>>> MT)
>>>
>>> 1                        452853                   196516
>>> 649369
>>>
>>> 2                        479088                   202122
>>> 681210
>>>
>>> 3                        488175                   217405
>>> 705580
>>>
>>> 4                        474660                   224490
>>> 699150
>>>
>>> 5                        531844                   265570
>>> 797414
>>>
>>> 6                        557772                   288373
>>> 846145
>>>
>>>
>>> Thanks,
>>>
>>> Mohan
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Alameda, CA, USA



More information about the R-help mailing list