qplot generates a legend with too many point types

14 views
Skip to first unread message

mark

unread,
Dec 2, 2010, 7:29:13 PM12/2/10
to ggplot2, msh...@sfbr.org
I am surprised by the legend that I am seeing with qplot and am
wondering if you can explain what is going on and perhaps help me get
the legend that is needed.

I am wanting to use an integer code (admit_code) as the parameter
value for colour in qplot.

admit_code is an integer that can have three values (0, 1, 3). In this
subset of the data, it only has two.
> class(colony_latency$admit_code)
[1] "integer"
> table(colony_latency$admit_code)

1 2
2906 2495

However, the legend that was generated by qplot has 1.0, 1.2, 1.4,
1.6, 1.8, 2.0 as possible values with the four colors of 1.2, 1.4. 1.6
and 1.8 being intermediate between those of 1.0 and 2.0. The points
that are actually plotted are displayed in the two colors
corresponding to 1.0 and 2.0.

The call to qplot is below.
qplot(proc_date[colony_latency$colony == colony],
latency_days[colony_latency$colony == colony],
data = colony_latency, geom = c("point", "smooth"),
color = admit_code, main = str_c(capitalize(colony),
" Colony: Procedure Data Entry Latency\n", start_date_str,
" through ", stop_date_str),
xlab = 'Years', ylab = 'Days Late', span = 0.2)

Brandon Hurr

unread,
Dec 2, 2010, 7:48:00 PM12/2/10
to mark, ggplot2, msh...@sfbr.org
Mark, 

I think it is taking it as a continuous numeric value. Try the following:

qplot(proc_date[colony_latency$colony == colony],
 latency_days[colony_latency$colony == colony],
 data = colony_latency, geom = c("point", "smooth"),
 color = factor(admit_code), main = str_c(capitalize(colony),

   " Colony: Procedure Data Entry Latency\n", start_date_str,
   " through ", stop_date_str),
   xlab = 'Years', ylab = 'Days Late', span = 0.2)

B

Dennis Murphy

unread,
Dec 2, 2010, 7:50:37 PM12/2/10
to mark, ggplot2, msh...@sfbr.org
Hi:

On Thu, Dec 2, 2010 at 4:29 PM, mark <rmsh...@gmail.com> wrote:
I am surprised by the legend that I am seeing with qplot and am
wondering if you can explain what is going on and perhaps help me get
the legend that is needed.

I am wanting to use an integer code (admit_code) as the parameter
value for colour in qplot.

admit_code is an integer that can have three values (0, 1, 3). In this
subset of the data, it only has two.
> class(colony_latency$admit_code)
[1] "integer"
> table(colony_latency$admit_code)

  1    2
2906 2495

However, the legend that was generated by qplot has 1.0, 1.2, 1.4,
1.6, 1.8, 2.0 as possible values with the four colors of 1.2, 1.4. 1.6
and 1.8 being intermediate between those of 1.0 and 2.0. The points
that are actually plotted are displayed in the two colors
corresponding to 1.0 and 2.0.

That's because admit_code is integer. If you want values of 1 or 2 in the legend, coerce it to factor, or better yet, create a factor in the data frame with a name that you want to associate with the legend title in case you want to use the numeric version for one purpose and the factor version for another (e.g., mapping aesthetics).

The call to qplot is below.
qplot(proc_date[colony_latency$colony == colony],
 latency_days[colony_latency$colony == colony],
 data = colony_latency, geom = c("point", "smooth"),
 color = admit_code, main = str_c(capitalize(colony),
   " Colony: Procedure Data Entry Latency\n", start_date_str,
   " through ", stop_date_str),
   xlab = 'Years', ylab = 'Days Late', span = 0.2)

HTH,
Dennis

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Reply all
Reply to author
Forward
0 new messages