如何将条件添加到geom_point大小? [英] How to add a condition to the geom_point size?

查看:109
本文介绍了如何将条件添加到geom_point大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

geom_point(aes(size = n_in_stat))+#原始线性缩放

p>

  library(ggplot2)

#创建一个long data.frame来存储数据...
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 ,
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)


#使用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. ()')+
scale_y_log2()+
scale_colour_hue('my legend',breaks = levels(growth_series $ series_id),labels = c('t1','t2','t3') )


解决方案

scale_size_manual设置离散变量的大小。 / p>

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


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

解决方案

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))

这篇关于如何将条件添加到geom_point大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆