subset not sticking, all data shows up in plot

25 views
Skip to first unread message

Henry Drexler

unread,
Mar 7, 2012, 10:49:31 AM3/7/12
to ggplot2
Hello,

When I plot my data, I am getting all the data points for the data set as text labels and all are showing up in the legend, but only those from my subset should be showing up.
Perhaps I am missing something simple.

I have attached the dataset.

Here is my R script:

library(ggplot2)
library(maps)
double <- read.csv(file="z_plain.csv",header=TRUE)
double$month <- as.Date(as.character(double$month), "%m/%d/%Y")
double$rolling_rate <- double$rolling_rate*100
blast_ps <- subset(double,double$type == "typeA")
p <- qplot(month,rolling_rate,data=blast_ps,shape=15,color=report,ylab="rolling_rate (percent)") + geom_line() + geom_text(aes(label=double$rolling_rate),position = "jitter")
p


As you can see I have blast_ps <- subset(double,double$type == "typeA") so only a,b,c should be shown in the key and only its text data values should be shown in the plot - although it is showing all the data values.



dput(double)
structure(list(report = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 
6L), .Label = c("a", "b", "c", "d", "e", "f"), class = "factor"), 
    type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("typeA", 
    "typeB"), class = "factor"), month = structure(c(15248, 15279, 
    15309, 15340, 15248, 15279, 15309, 15340, 15248, 15279, 15309, 
    15340, 15248, 15279, 15309, 15340, 15248, 15279, 15309, 15340, 
    15248, 15279, 15309, 15340), class = "Date"), rolling_rate = c(10.4, 
    10.1, 9.8, 9.6, 29, 26.8, 26.3, 25.3, 39.8, 38.1, 37.6, 36, 
    7.9, 7.2, 6.6, 6.6, 7.7, 7.2, 7.1, 7.3, 12.1, 11.3, 11.2, 
    11.3)), .Names = c("report", "type", "month", "rolling_rate"
), row.names = c(NA, -24L), class = "data.frame")


dput(blast_ps)
structure(list(report = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L), .Label = c("a", "b", "c", "d", "e", "f"), class = "factor"), 
    type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L), .Label = c("typeA", "typeB"), class = "factor"), 
    month = structure(c(15248, 15279, 15309, 15340, 15248, 15279, 
    15309, 15340, 15248, 15279, 15309, 15340), class = "Date"), 
    rolling_rate = c(10.4, 10.1, 9.8, 9.6, 29, 26.8, 26.3, 25.3, 
    39.8, 38.1, 37.6, 36)), .Names = c("report", "type", "month", 
"rolling_rate"), row.names = c(NA, 12L), class = "data.frame")


Thank you in advance for any insights.
z_plain.csv

Justin Haynes

unread,
Mar 7, 2012, 11:11:33 AM3/7/12
to Henry Drexler, ggplot2
look at levels(double$type) and levels blast_ps$type. Because you are
subsetting a factor, it carries all the levels of the factor along.

blast_ps$type <- droplevels(blast_ps$type)
blast_ps$report <- droplevels(blast_ps$report)

you can also use ?str to examine structures which can be incredibly useful.

From there you should be able to plot like you wanted.


Hope that helps,

Justin

2012/3/7 Henry Drexler <alon...@gmail.com>:

> --
> 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

Art

unread,
Mar 7, 2012, 11:18:50 AM3/7/12
to ggp...@googlegroups.com
Hello Henry,

Using the ggplot structure is sometimes easier when adding components to the graph.

I've modified your geom_text to reference the subset data.frame and I've added colour grouping to the geom_line argument to graph the a, b, and c reports.

See script below.

Hope that helps.

Best,
Art 


p <- ggplot(blast_ps, aes(month, rolling_rate)) +
  geom_line(aes(colour = report)) +
  geom_text(aes(label=blast_ps$rolling_rate),position = "jitter")

p

Henry Drexler

unread,
Mar 7, 2012, 11:33:49 AM3/7/12
to Justin Haynes, ggplot2
Justin, thank you for the hints, I will read up on those.
Reply all
Reply to author
Forward
0 new messages