Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何向几何点大小添加条件?_R_Size_Ggplot2_Scale - Fatal编程技术网

R 如何向几何点大小添加条件?

R 如何向几何点大小添加条件?,r,size,ggplot2,scale,R,Size,Ggplot2,Scale,我正在尝试向geom_point size添加一个条件,并将我的示例粘贴到下面。当n_in_stat为4或更多时,我希望geom_点大小为2,当n_in_stat小于4时,大小为5。我试着在geom_点内放一个ifelse语句,但失败了。也许我不能在这里包含逻辑运算符,我必须在data.frame中创建一个新列并将大小设置为该列 geom_点(size=ifelse(n_in_stat

我正在尝试向geom_point size添加一个条件,并将我的示例粘贴到下面。当n_in_stat为4或更多时,我希望geom_点大小为2,当n_in_stat小于4时,大小为5。我试着在geom_点内放一个ifelse语句,但失败了。也许我不能在这里包含逻辑运算符,我必须在data.frame中创建一个新列并将大小设置为该列

geom_点(size=ifelse(n_in_stat<4,5,2))+#尝试使用ifelse设置大小

几何点(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'))

您也可以只使用一个函数:

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


比例\大小\手动设置离散变量的大小

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

您可能还需要
+scale\u size\u identity
奇怪,这会导致:eval(expr、envir、enclose)中出错:在另一个函数中调用ggplot时找不到函数“ff”。
geom_point(aes(size = ff(n_in_stat))) +
geom_point(aes(size =n_in_stat>4)) + scale_size_manual(values=c(2,5))