feder80
feder80

Reputation: 1361

Plotting every second data point in ggplot2

I have the following dataset:

Data <- data.frame(
    week = c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4),
    variable = c("nuclear", "nuclear", "nuclear", "nuclear", "atomic", "atomic", "atomic", "atomic", "unemployment", "unemployment", "unemployment", "unemployment"),
    value = c(2, 3, 1, 4, 5, 1, 2, 3, 3, 2, 5, 1)
)

I need a black-white-graph. So I use the following code:

ggplot(Data, aes(week, value, group=variable)) + xlab("Year") + ylab("Frequency of searches") +
  geom_line(aes(linetype=variable))+ # Line type depends on cond
  geom_point(aes(shape=variable))+  # Shape depends on cond
  scale_shape_manual(values=c(0,1,2)) + # Change shapes
  scale_linetype_manual(values=c("solid", "dotted", "dotdash"))+
  theme(legend.position="top", legend.title=element_blank(), panel.background = element_rect(fill = "#FFFFFF", colour="#000000"))+
  stat_smooth(method="loess"))

That works for fine for this small dataset. But when it's bigger, it's difficult to distinguish the lines and points from each other. It all looks very similar. Is there a way to plot only every second or tenth data point?

Upvotes: 2

Views: 7322

Answers (3)

Matthew Lundberg
Matthew Lundberg

Reputation: 42679

Here's a way to take the even rows, relying on recycling of the index vector:

Data[c(FALSE,TRUE),]

And every 10th:

Data[c(rep(FALSE, 9), TRUE),]

Upvotes: 1

lukeA
lukeA

Reputation: 54247

Subset Data using seq():

Data[seq(1, nrow(Data), 4), ] # every 4th row
Data[seq(1, nrow(Data), 2), ] # every 2nd row
# ...

Upvotes: 7

Rosen Matev
Rosen Matev

Reputation: 1878

You can override the data for geom_point with a subset:

geom_point(aes(shape=variable), data=subset(Data, week %% 2 == 1))

Upvotes: 7

Related Questions