ggplot2:根据值添加渐变彩色方块

发布于 2024-12-25 07:39:12 字数 226 浏览 1 评论 0原文

关于我想要做什么,我有一个棘手的问题。 我有一个图,上面有两条线(两个条件的平均值)。我想在同一个图上添加一个反映 t 值的正方形(并根据这些值以渐变方式着色)。我怎样才能添加这个正方形?

好吧,因为我不知道我是否清楚,这是我试图实现的目标的一个数字。

感谢您的帮助!

在此处输入图像描述

I have a tricky question regarding to what i'm trying to do.
I have a plot with two lines (the mean of two conditions) on it. I want to add on the same plot a square reflecting the t-values (and colored according to these values in a gradient way). How could i add this square?

Well since i don't know if i'm clear, here is a figure of what i try to achieve.

Thank you for any help!

enter image description here

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

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

发布评论

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

评论(2

隔纱相望 2025-01-01 07:39:12

尝试使用 ggplot2 方式:

x <- seq(-10, 10, 0.1)
df <- data.frame(x, y1 = pnorm(x), y2 = pnorm(x) * 2)
df$t <- df$y2 - df$y1
dfm <- melt(df, id = "x")

ggplot(NULL, aes(x, value)) + 
  geom_line(aes(colour = variable), 
            droplevels(subset(dfm, variable %in% c("y1", "y2")))) +
  geom_rect(aes(xmin = x - 0.05, xmax = x + 0.05, ymin = -0.5, ymax = -0.4, fill = value),
            subset(dfm, variable == "t"))

在此处输入图像描述

已更新

您可以使用scale_fill_XXX< /代码>。这是一个jet-color版本:

jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan","#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))

# panel on the left side
p <- ggplot(NULL, aes(x, value)) + 
  geom_line(aes(colour = variable), 
            droplevels(subset(dfm, variable %in% c("y1", "y2")))) +
  geom_rect(aes(xmin = x - 0.05, xmax = x + 0.05, ymin = -0.5, ymax = -0.4, fill = value),
            subset(dfm, variable == "t")) + 
  scale_fill_gradientn(colours = jet.colors(7))
p

在ggplot2的下一个版本中,您可以使用colorbar作为图例。

  # panel on the right side
  p + guides(fill = "colourbar")   

在此处输入图像描述

Try this for ggplot2 way:

x <- seq(-10, 10, 0.1)
df <- data.frame(x, y1 = pnorm(x), y2 = pnorm(x) * 2)
df$t <- df$y2 - df$y1
dfm <- melt(df, id = "x")

ggplot(NULL, aes(x, value)) + 
  geom_line(aes(colour = variable), 
            droplevels(subset(dfm, variable %in% c("y1", "y2")))) +
  geom_rect(aes(xmin = x - 0.05, xmax = x + 0.05, ymin = -0.5, ymax = -0.4, fill = value),
            subset(dfm, variable == "t"))

enter image description here

UPDATED

You can use scale_fill_XXX. Here is a jet-color version:

jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan","#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))

# panel on the left side
p <- ggplot(NULL, aes(x, value)) + 
  geom_line(aes(colour = variable), 
            droplevels(subset(dfm, variable %in% c("y1", "y2")))) +
  geom_rect(aes(xmin = x - 0.05, xmax = x + 0.05, ymin = -0.5, ymax = -0.4, fill = value),
            subset(dfm, variable == "t")) + 
  scale_fill_gradientn(colours = jet.colors(7))
p

and in the next version of ggplot2, you can use colorbar as the legend.

  # panel on the right side
  p + guides(fill = "colourbar")   

enter image description here

笑梦风尘 2025-01-01 07:39:12

对于基础图形,您可以使用 rasterImage 函数将带有渐变的矩形添加到图形中。

For base graphics you can use the rasterImage function to add a rectangle with the gradient in it to a graph.

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