如何根据我的geom_violin中的y轴值给出颜色?

发布于 2025-02-13 23:20:40 字数 798 浏览 2 评论 0原文

如何根据我的geom_violin中的y轴值给出颜色? 我想表明,Y值较大的因素具有更强烈的蓝色,并且Y值较小的因素的红色较小。我想保留那个调色板。请有人可以帮我吗?

我在r:

ggplot(df4, aes(x =orden, y = size, fill = orden)) +
  geom_violin(trim = FALSE) +
  scale_fill_brewer(palette="RdBu")+
  geom_boxplot(width = 0.07) +   
  scale_x_discrete(labels = c("Insectos palo","Hormigas", "Escarabajos",
                                "Áfidos", "Moscas", "Mariposas", "Polillas", 
                                "Saltamontes")) +
  labs(x=c(""),y="Tamaño del genoma (Mb)")+
  #scale_y_continuous(trans = "log2")+#, breaks = c(20, 40, 80))
  theme_classic()

”在此处输入图像描述”

非常感谢

How to give colors based on the Y axis values in my geom_violin?
I would like to show that factors with larger y values have a more intense blue and factors with smaller y values have a less intense red. I would like to keep that color palette. Please can someone help me?

I have de following script in R:

ggplot(df4, aes(x =orden, y = size, fill = orden)) +
  geom_violin(trim = FALSE) +
  scale_fill_brewer(palette="RdBu")+
  geom_boxplot(width = 0.07) +   
  scale_x_discrete(labels = c("Insectos palo","Hormigas", "Escarabajos",
                                "Áfidos", "Moscas", "Mariposas", "Polillas", 
                                "Saltamontes")) +
  labs(x=c(""),y="Tamaño del genoma (Mb)")+
  #scale_y_continuous(trans = "log2")+#, breaks = c(20, 40, 80))
  theme_classic()

enter image description here

Many thanks

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

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

发布评论

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

评论(1

森末i 2025-02-20 23:20:40

尝试一下。我将alpha放在geom_violin中,以改变该地理的强度。

library(tidyverse)
library(RColorBrewer)

# Made-up data
df <- tribble(
  ~orden, ~size,
  1, 1,
  1, 2,
  1, 1.2,
  2, 1.7,
  2, 1.3,
  2, 1.5,
  3, 4.7,
  3, 4.3,
  3, 5.5
) 

# Plot
df |>
  group_by(orden) |> 
  mutate(orden = factor(orden),
         alpha = mean(size)) |> 
  ungroup() |> 
  ggplot(aes(orden, size, fill = orden)) +
  geom_violin(aes(alpha = alpha), trim = FALSE) +
  scale_fill_brewer(palette = "RdBu") +
  geom_boxplot(width = 0.07) +
  scale_x_discrete(labels = c(
    "Insectos palo", "Hormigas", "Escarabajos",
    "Áfidos", "Moscas", "Mariposas", "Polillas",
    "Saltamontes"
  )) +
  labs(x = NULL, y = "Tamaño del genoma (Mb)", fill = "orden") +
  theme_classic() +
  guides(alpha = "none")

“”

在2022-07-07创建的 reprex软件包(v2.0.1)

Try this. I've put alpha in the geom_violin to vary the intensity of that geom.

library(tidyverse)
library(RColorBrewer)

# Made-up data
df <- tribble(
  ~orden, ~size,
  1, 1,
  1, 2,
  1, 1.2,
  2, 1.7,
  2, 1.3,
  2, 1.5,
  3, 4.7,
  3, 4.3,
  3, 5.5
) 

# Plot
df |>
  group_by(orden) |> 
  mutate(orden = factor(orden),
         alpha = mean(size)) |> 
  ungroup() |> 
  ggplot(aes(orden, size, fill = orden)) +
  geom_violin(aes(alpha = alpha), trim = FALSE) +
  scale_fill_brewer(palette = "RdBu") +
  geom_boxplot(width = 0.07) +
  scale_x_discrete(labels = c(
    "Insectos palo", "Hormigas", "Escarabajos",
    "Áfidos", "Moscas", "Mariposas", "Polillas",
    "Saltamontes"
  )) +
  labs(x = NULL, y = "Tamaño del genoma (Mb)", fill = "orden") +
  theme_classic() +
  guides(alpha = "none")

Created on 2022-07-07 by the reprex package (v2.0.1)

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