如何为 geom_point 大小添加条件?

发布于 2024-08-23 06:18:08 字数 1185 浏览 7 评论 0原文

我正在尝试向 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)) + # 原始线性缩放

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肤浅与狂妄 2024-08-30 06:18:08

scale_size_manual 设置离散变量的大小。

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

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))
錯遇了你 2024-08-30 06:18:08

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

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

然后

geom_point(aes(size = n_in_stat)) +

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

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))) +
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文