about using ggplot with data

51 views
Skip to first unread message

lily li

unread,
Feb 27, 2015, 11:54:58 AM2/27/15
to ggp...@googlegroups.com
Hi ggplot users,

I want to ask that how to set axis limits or how to remove lines with missing values? Question1: The df is extended from 2008 to 2011, but I want to use only data from 2008 to 2010. When set limits like this, it gives "Error: Discrete value supplied to continuous scale". How to set limits in this condition? Question 2: how to drop off rows with NA values in the plotting process? Thanks for your help.

plotbc1=ggplot()+ 
  geom_point(data=df,aes(x=date,y=precip,color='red'))+
  xlab('Time')+ylab('Precipitation')+scale_x_continuous(limits = c(2008,2010), oob=squish)
show(plotbc1)

My dataframe is like this:
df
year    month    day    temp    precip       date
2008      1           1       -10         0.0    2008-01-01
2008      1           2         5          NA    2008-01-02
2008      1           3         2          0.1    2008-01-03
2008      1           4         1          0.2    2008-01-04
2008      1           5         2          0.1    2008-01-05
2008      1           6        -3          0.2    2008-01-06
2008      1           7        -8          NA    2008-01-07

lily li

unread,
Feb 27, 2015, 12:10:06 PM2/27/15
to ggp...@googlegroups.com
Continue with the former email.

when change +scale_x_continuous(limits = c(2008,2010), oob=squish) to +xlim('2008-01-01','2010-12-31'), it doesn't work either.


Ben Bond-Lamberty

unread,
Feb 27, 2015, 12:14:31 PM2/27/15
to ggp...@googlegroups.com
Generally to set axis limits you'll want to use

+ coord_cartesian(xlim=c(x1, x2))

There's also a '+xlim()' command but this tosses out data, which is
not usually what people want.

Re dropping NA values, what about na.omit(df)?

Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

lily li

unread,
Feb 27, 2015, 12:17:27 PM2/27/15
to Ben Bond-Lamberty, ggp...@googlegroups.com
Thanks, Ben.

But na.omit(df) drops off every row with missing values. So if I just want to plot date~temp, there will be dropped data. I want to know how to deal with such a problem that treat the two columns (precip and temp) separately?

Dennis Murphy

unread,
Feb 27, 2015, 12:34:42 PM2/27/15
to lily li, ggp...@googlegroups.com
Hi:

There are a few things going on here:

1. Look at the class of your date variable; I suspect yours is factor,
perhaps character. You probably want it to be of class Date.
2. The NA rows don't matter for plotting purposes because ggplot2 will
fail to plot points there. You'll get a warning about rows removed
that contain missing values.

Here's one approach to the problem:

DF <- read.table(header = TRUE, stringsAsFactors = FALSE, text = "
year month day temp precip date
2008 1 1 -10 0.0 2008-01-01
2008 1 2 5 NA 2008-01-02
2008 1 3 2 0.1 2008-01-03
2008 1 4 1 0.2 2008-01-04
2008 1 5 2 0.1 2008-01-05
2008 1 6 -3 0.2 2008-01-06
2008 1 7 -8 NA 2008-01-07")

str(DF) # Note the class of date

# In the above, date is of class "character", so the following works
# to convert it to a Date object:
# (uncomment the next line if date is of class "character")
# DF$date <- as.Date(DF$date)

# If yours is of class factor, you need to coerce it to character first,
# then Date: (uncomment if your date is of class "factor")
# DF$date <- as.Date(as.character(DF$date))

str(DF) # Check that the conversion went correctly


# Plot without extending the date axis
# ggplot2 'knows' that date is of class "Date"
# and plots the x-axis accordingly
library(ggplot2)
ggplot(data=DF,aes(x=date, y=precip))+
geom_point(color = "red")+
labs(x = 'Time', y = 'Precipitation')

# Extend the date axis to the end of 2010
ggplot(data=DF,aes(x=date, y=precip))+
geom_point(color = "red")+
labs(x = 'Time', y = 'Precipitation') +
scale_x_date(limits = c(as.Date("2008-01-01"),as.Date("2010-12-31")))


# Ditto, but add the squish argument
ggplot(data=DF,aes(x=date, y=precip))+
geom_point(color = "red")+
labs(x = 'Time', y = 'Precipitation') +
scale_x_date(limits = c(as.Date("2008-01-01"),as.Date("2010-12-31")),
oob=squish)

As with anything in R, you need to understand the types of objects
with which you are dealing. One of the reasons you were getting error
messages is because you were trying to pass character strings as
limits, as in

xlim('2008-01-01','2010-12-31')

If you're trying to define a date range, the two dates need to be
converted to Date objects in concert with scale_x_date(), per my last
two examples above.

When I plot either of the last two graphs, the R evaluator returns the
following warning:

Warning message:
Removed 2 rows containing missing values (geom_point).

These correspond to the two rows in your input data frame that have NA
precip values.

HTH,
Dennis

lily li

unread,
Feb 27, 2015, 12:43:07 PM2/27/15
to Dennis Murphy, ggp...@googlegroups.com
Hi Dennis,

Thanks very much. It helps a lot.
Reply all
Reply to author
Forward
0 new messages