How to add a condition to the geom_point size?
I am trying to add a condition to geom_point size and I've pasted my example below. I would like geom_point size to be 2 when n_in_stat is 4 or more, and size = 5 when n_in_stat is less than 4. I tried putting an ifelse statement inside the geom_point, but this failed. Perhaps I can't include logical operators here and I have to create a new column in the data.frame and set the size to that?
geom_point(size = ifelse(n_in_stat < 4, 5, 2)) + # trying to set size with an ifelse
geom_point(aes(size = n_in_stat)) + # original scaled linearly
library(ggplot2)
# Create a long data.frame to store data...
growth_series = data.frame ("read_day" = c(0, 3, 9, 0, 3, 9, 0, 2, 8),
"series_id" = c("p1s1", "p1s1", "p1s1", "p1s2", "p1s2", "p1s2", "p3s4", "p3开发者_运维百科s4", "p3s4"),
"mean_od" = c(0.6, 0.9, 1.3, 0.3, 0.6, 1.0, 0.2, 0.5, 1.2),
"sd_od" = c(0.1, 0.2, 0.2, 0.1, 0.1, 0.3, 0.04, 0.1, 0.3),
"n_in_stat" = c(8, 8, 8, 8, 7, 5, 8, 7, 2)
)
# Plot using ggplot...
ggplot(data = growth_series, aes(x = read_day, y = mean_od, group = series_id, color = series_id)) +
geom_line(size = 1.5) +
geom_point(aes(size = n_in_stat)) +
geom_errorbar(aes(ymin = mean_od - sd_od, ymax = mean_od + sd_od), size = 1, width = 0.3) +
xlab("Days") + ylab("Log (O.D. 730 nm)") +
scale_y_log2() +
scale_colour_hue('my legend', breaks = levels(growth_series$series_id), labels=c('t1', 't2', 't3'))
scale_size_manual sets the sizes for a discrete variable.
geom_point(aes(size =n_in_stat>4)) + scale_size_manual(values=c(2,5))
also you can just use a function:
ff <- function(x){ifelse(x < 4, 5, 2)}
and then change
geom_point(aes(size = n_in_stat)) +
with
geom_point(aes(size = ff(n_in_stat))) +
精彩评论