plot lines skipping values

6 views
Skip to first unread message

Alessandra Carioli

unread,
Jun 16, 2014, 12:25:11 PM6/16/14
to ggplot2
Dear bloggers,

I am plotting a lines graph and I want to skip plotting data for 3 years (1996-1998):

ggplot(dataframe, aes(Year,Value,colour=factor(Type)))+
geom_line(size=1,linetype=c(1))

I have tried subsetting for specific years but it produces a very funny graph.

Any help on the matter is greatly appreciated!

Ale

Ben Bond-Lamberty

unread,
Jun 16, 2014, 1:40:40 PM6/16/14
to Alessandra Carioli, ggplot2
If your dataframe has NAs for those missing data, ggplot will skip
them correctly.

df <- data.frame(x=1:5,y=c(1,2,NA,4,5))
qplot(x,y,data=df,geom='line')

Is this what you want? If not, please give a reproducible example.
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.

Alessandra Carioli

unread,
Jun 17, 2014, 9:08:59 AM6/17/14
to Ben Bond-Lamberty, ggplot2
I was thinking more about a solution that would allow me to skip plotting data without having to replace them each time with NAs. In this case, for instance, I would like to skip plotting 3 years, 2004, 2005 and 2006.

here is a reproducible sample (it’s a subset):

structure(list(Variable = structure(c(11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L
), .Label = c("MAC", "MAC1", "PI", "PI_Africa_Sub", "PI_Asia",
"PI_EU15", "PI_LA", "PI_Magreb", "PI_ROE", "PI_Spain", "TFR",
"TFR_1", "TFR_10", "TFR_15", "TFR_2", "TFR_20", "TFR_25", "TFR_3",
"TFR_30", "TFR_35", "TFR_4", "TFR_40", "TFR_45", "TFR_6", "TFR_8",
"TFR_9", "TFR1", "TFR2", "TFR3"), class = "factor"), Type = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L),
Year = c(2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L,
2000L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L,
2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2003L,
2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2004L, 2004L,
2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2005L, 2005L, 2005L,
2005L, 2005L, 2005L, 2005L, 2005L, 2006L, 2006L, 2006L, 2006L,
2006L, 2006L, 2006L, 2006L, 2007L, 2007L, 2007L, 2007L, 2007L,
2007L, 2007L, 2007L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
2008L, 2008L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L,
2009L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L,
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L),
Moran.I = c(0.6409056, 0.6407005, 0.4275571, 0.5936098, 0.5498624,
0.62525, 0.6036849, 0.5887174, 0.6572718, 0.6572855, 0.4470739,
0.6053142, 0.5531365, 0.6397758, 0.6110722, 0.5943911, 0.6561859,
0.6543732, 0.4455499, 0.5777671, 0.5482875, 0.6339992, 0.6074544,
0.5879112, 0.6425568, 0.6423889, 0.4202216, 0.5938255, 0.550055,
0.6170684, 0.5902681, 0.5852566, 0.6441274, 0.6446853, 0.4219629,
0.5774096, 0.537435, 0.622358, 0.5907318, 0.5733832, 0.6389138,
0.6382427, 0.4321163, 0.5953335, 0.5395664, 0.6192925, 0.5946383,
0.5808641, 0.6540744, 0.654014, 0.4551873, 0.6012926, 0.5388811,
0.632351, 0.609773, 0.5899993, 0.6132082, 0.6130471, 0.3827174,
0.541689, 0.4808921, 0.5819688, 0.5490175, 0.5330646, 0.6305054,
0.6297731, 0.4295609, 0.551404, 0.4923912, 0.5923736, 0.5632883,
0.5416156, 0.6128512, 0.612811, 0.3963124, 0.5439521, 0.4822124,
0.5863236, 0.5557979, 0.5324639, 0.6131376, 0.6133588, 0.4034572,
0.5371892, 0.4736599, 0.5859961, 0.5488886, 0.5208484, 0.5907943,
0.5908498, 0.3765681, 0.512962, 0.4589016, 0.5513458, 0.5206248,
0.5042716), Ordered = c(29L, 29L, 29L, 29L, 29L, 29L, 29L,
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L,
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L,
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L,
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L,
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L,
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L,
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L,
29L, 29L, 29L, 29L, 29L)), .Names = c("Variable", "Type",
"Year", "Moran.I", "Ordered"), row.names = 1617:1712, class = "data.frame")

Roman Luštrik

unread,
Jun 17, 2014, 9:19:14 AM6/17/14
to Alessandra Carioli, Ben Bond-Lamberty, ggplot2
Subset only years that you want to have in the final figure?
Something like xy[!xy$Year %in% c(2004, 2005, 2006), ]

Cheers,
Roman
--
In God we trust, all others bring data.

Ben Bond-Lamberty

unread,
Jun 17, 2014, 9:20:16 AM6/17/14
to Alessandra Carioli, ggplot2
I guess I still don't see the problem--can't you just do

dataframe[dataframe$Year %in% 2006:2008, "Moran.I" ] <- NA
ggplot(dataframe, aes(Year,Moran.I,colour=factor(Type)))
geom_line(size=1,linetype=c(1))

Ben

Alessandra Carioli

unread,
Jun 17, 2014, 9:40:56 AM6/17/14
to Ben Bond-Lamberty, ggplot2
Thanks to both!
Reply all
Reply to author
Forward
0 new messages