2010-03-02 24 views
3

我想添加一个条件geom_point大小,我已经粘贴下面的示例。当n_in_stat大于等于4时,我希望geom_point大小为2,当n_in_stat小于4时size = 5。我尝试在geom_point中放置一个ifelse语句,但是失败了。也许我不能在这里包含逻辑运算符,我必须在data.frame中创建一个新列,并将其大小设置为?如何将条件添加到geom_point大小?

geom_point(大小= ifelse(n_in_stat < 4,5,2))+#试图用ifelse集大小

geom_point(AES(大小= n_in_stat))+#原始线性缩放

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", "p3s4", "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')) 

回答

3

scale_size_manual设置了离散变量的大小。

geom_point(aes(size =n_in_stat>4)) + scale_size_manual(values=c(2,5)) 
2

你也可以只使用一个功能:

ff <- function(x){ifelse(x < 4, 5, 2)} 

,然后改变

geom_point(aes(size = n_in_stat)) + 

geom_point(aes(size = ff(n_in_stat))) + 
+0

你可能还需要'+ scale_size_identity' – hadley

+0

奇怪,这给:在EVAL错误(表达式,ENVIR,enclos):找不到函数 “FF” 当我调用另一个函数内ggplot。 – gkcn

相关问题