Reputation: 301
I am wanting to plot changes in weight(X) per day(Y). I would like to label the very first datapoint, then every seventh.
I am currently using geom_point() + geom_label(aes(label = weight))
. I was thinking maybe rep()
would do this, but I can't figure out how; if it even will.
Upvotes: 1
Views: 1570
Reputation: 66825
my_data = data.frame(timecount = 1:210,
value = 1:210 + 20*sin((1:30)/10))
library(dplyr); library(ggplot2)
my_data %>%
ggplot(aes(timecount, value)) +
geom_point()
my_data %>%
mutate(row = row_number()) %>%
filter(row %% 7 == 1) %>% # modulo operator: only keep rows
# where the remainder of row / 7 is 1,
# i.e. 1, 8, 15, etc.
ggplot(aes(timecount, value)) +
geom_point()
Upvotes: 0
Reputation: 33792
You can pass the data to geom_label
and filter for row numbers that match your condition.
Assuming that by "every seventh" you mean 7, 14, 21... use %% 7 == 0
. Otherwise use %% 7 == 1
for 8, 15, 22...
library(dplyr)
library(ggplot2)
mydata <- data.frame(x = 2 * (1:49),
y = 3 * (1:49))
mydata %>%
ggplot(aes(x, y)) +
geom_point() +
geom_label(aes(label = x),
data = . %>%
filter(row_number() %% 7 == 0 | row_number() == 1))
Result:
Upvotes: 4