带有多种颜色的小提琴图

发布于 2025-01-18 06:47:08 字数 780 浏览 0 评论 0原文

我想生成一个小提琴图,其中不同的区域充满了不同的颜色。 例如:

df <- data.frame("data" = runif(1000))
ggplot(df, aes(x ="DATA", y = data)) + geom_violin(aes(fill = if_else(data > 0.5, "green","red")))

上面的命令生成下图中显示的小提琴图。小提琴图由2个分开的零件组成。我期望在上部(数据&GT; 0.5)上有色绿色并且下部有色红色的完整形状(即无物理分离)。

我知道有一个着色问题。但是,我想问的是:

  • 为什么情节有2个分开的零件?
  • 有没有办法使其成为一个单一的“身体”,就像常规的小提琴图中一样?

我发现以下解决方案具有多色小提琴图,但在我的情况下它不起作用:填充geom_violin图中的特定区域

谢谢。

I want to generate a violin plot, in which, the different regions are filled with different colors.
As an example:

df <- data.frame("data" = runif(1000))
ggplot(df, aes(x ="DATA", y = data)) + geom_violin(aes(fill = if_else(data > 0.5, "green","red")))

The commands above generate the violin plot shown in the picture below. The violin plot consists of 2 separated parts. I was expecting one complete shape (i.e. no physical separation) where the upper part (data >0.5) is colored green and the lower part is colored red.

I know there is a coloring problem. But, what I want to ask is:

  • Why the plot comes in 2 separated parts?
  • Is there a way to make it a single "body" as it would be in a regular violin plot?

I found the following solution have a multi-colored violin plot but it did not work in my case: Fill specific regions in geom_violin plot

Thank you.

Violin Plot

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

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

发布评论

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

评论(1

海未深 2025-01-25 06:47:08

链接的答案显示了一种通过构建绘图和调整底层 grobs 来实现此目的的巧妙方法,但如果您想在不进行 grob-hacking 的情况下执行此操作,则需要获取自己的密度曲线并用多边形绘制它们:

df <- data.frame("data" = runif(1000))

dens <- density(df$data)
new_df1 <- data.frame(y = c(dens$x[dens$x < 0.5], rev(dens$x[dens$x < 0.5])),
                      x = c(-dens$y[dens$x < 0.5], rev(dens$y[dens$x < 0.5])),
                      z = 'red2')
new_df2 <- data.frame(y = c(dens$x[dens$x >= 0.5], rev(dens$x[dens$x >= 0.5])),
                      x = c(-dens$y[dens$x >= 0.5], rev(dens$y[dens$x >= 0.5])),
                      z = 'green3')

ggplot(rbind(new_df1, new_df2), aes(x, y, fill = z)) + 
  geom_polygon() +
  scale_fill_identity() +
  scale_x_continuous(breaks = 0, expand = c(1, 1), labels = 'DATA', name = '')

在此处输入图像描述

The linked answer shows a neat way to do this by building the plot and adjusting the underlying grobs, but if you want to do this without grob-hacking, you will need to get your own density curves and draw them with polygons:

df <- data.frame("data" = runif(1000))

dens <- density(df$data)
new_df1 <- data.frame(y = c(dens$x[dens$x < 0.5], rev(dens$x[dens$x < 0.5])),
                      x = c(-dens$y[dens$x < 0.5], rev(dens$y[dens$x < 0.5])),
                      z = 'red2')
new_df2 <- data.frame(y = c(dens$x[dens$x >= 0.5], rev(dens$x[dens$x >= 0.5])),
                      x = c(-dens$y[dens$x >= 0.5], rev(dens$y[dens$x >= 0.5])),
                      z = 'green3')

ggplot(rbind(new_df1, new_df2), aes(x, y, fill = z)) + 
  geom_polygon() +
  scale_fill_identity() +
  scale_x_continuous(breaks = 0, expand = c(1, 1), labels = 'DATA', name = '')

enter image description here

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