填充 ggplot2 图的区域是否可以根据每个 x 轴坐标处的值进行更改?

huangapple go评论99阅读模式
英文:

Can the fill area of a ggplot2 plot change to match the values at each x-axis coordinate

问题

我有以下的数据框。

  1. S <- rep(c("A","B","C"), each = 30)
  2. Y <- rep(c("X", "Y", "Z"), 30)
  3. V <- as.factor(runif(90, min=0, max=100))
  4. df <- as.data.frame(cbind(S, Y, V))
  5. df$V <- as.numeric(V)

我正在如下绘制它。

  1. ggplot(data = df, aes(x = Y,
  2. y = V, group = 1, fill=V)) +
  3. geom_line(linewidth=3) +
  4. theme_classic()+
  5. labs(title="",
  6. x=paste(""),
  7. y=paste(""),
  8. )+
  9. theme(plot.title=element_text(size=18, face="bold",hjust = 0.7, vjust=0.2,
  10. margin = margin(t = 0, r = 10, b = 10, l = 0)),
  11. axis.text.x=element_text(size=14, face="bold", angle=90),
  12. axis.text.y=element_blank(),
  13. axis.title.x=element_text(size=16, face="bold",
  14. margin = margin(t = 10, r = 0, b = 0, l = 0)),
  15. axis.title.y=element_blank(),
  16. legend.text=element_text(size=14, face="bold"),
  17. legend.title =element_text(size=14, face="bold"))+
  18. facet_wrap(~ S, ncol= 1, strip.position="left")+
  19. geom_area()+
  20. scale_fill_gradient(low="blue", high="red")

有没有一种方式可以使区域颜色根据V的值更改。目前绘图如下,但我希望值与绘图通过每个X坐标时相对应。

填充 ggplot2 图的区域是否可以根据每个 x 轴坐标处的值进行更改?

英文:

I have the following dataframe.

  1. S &lt;- rep(c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;), each = 30)
  2. Y &lt;- rep(c(&quot;X&quot;, &quot;Y&quot;, &quot;Z&quot;), 30)
  3. V &lt;- as.factor(runif(90, min=0, max=100))
  4. df &lt;- as.data.frame(cbind(S, Y, V))
  5. df$V &lt;- as.numeric(V)

I am plotting it as follows.

  1. ggplot(data = df, aes(x = Y,
  2. y = V, group = 1, fill=V)) +
  3. geom_line(linewidth=3) +
  4. theme_classic()+
  5. labs(title=&quot;&quot;,
  6. x=paste(&quot;&quot;),
  7. y=paste(&quot;&quot;),
  8. )+
  9. theme(plot.title=element_text(size=18, face=&quot;bold&quot;,hjust = 0.7, vjust=0.2,
  10. margin = margin(t = 0, r = 10, b = 10, l = 0)),
  11. axis.text.x=element_text(size=14, face=&quot;bold&quot;, angle=90),
  12. axis.text.y=element_blank(),
  13. axis.title.x=element_text(size=16, face=&quot;bold&quot;,
  14. margin = margin(t = 10, r = 0, b = 0, l = 0)),
  15. axis.title.y=element_blank(),
  16. legend.text=element_text(size=14, face=&quot;bold&quot;),
  17. legend.title =element_text(size=14, face=&quot;bold&quot;))+
  18. facet_wrap(~ S, ncol= 1, strip.position=&quot;left&quot;)+
  19. geom_area()+
  20. scale_fill_gradient(low=&quot;blue&quot;, high=&quot;red&quot;)

Is there a way for the area colour to change by the value of V. Currently the plot looks as so but I would like values to correspond as the plot goes through each X coordinate.

填充 ggplot2 图的区域是否可以根据每个 x 轴坐标处的值进行更改?

答案1

得分: 1

以下是代码部分的翻译:

  1. library(tidyverse)
  2. df %>%
  3. group_by(S) %>%
  4. mutate(Y = factor(Y)) %>%
  5. summarize(xval = seq(1, nlevels(Y), length.out = 500),
  6. yval = approx(as.numeric(Y), V, ties = "ordered", xout = xval)$y,
  7. Y = levels(Y)[floor(xval)], .groups = "drop") %>%
  8. ggplot(aes(xval, yval, fill = yval)) +
  9. geom_col(width = 0.01, position = "identity") +
  10. geom_line(linewidth = 1) +
  11. facet_wrap(~ S, ncol = 1, strip.position = "left") +
  12. scale_fill_gradient(low = "blue", high = "red") +
  13. scale_x_continuous(breaks = seq(length(unique(df$Y))),
  14. labels = unique(df$Y), expand = c(0, 0.5, 0, 0.5)) +
  15. labs(x = NULL, y = NULL, fill = "V") +
  16. theme_classic(base_size = 14) +
  17. theme(axis.text.x = element_text(face = "bold", angle = 90),
  18. axis.text.y = element_blank(),
  19. legend.text = element_text(face = "bold"),
  20. legend.title = element_text(face = "bold"))

希望这对你有所帮助。

英文:

I would usually do this by interpolating hundreds of values and having an individual column for each. Because you have a discrete axis, you will have to convert this to numeric then make it look discrete again using scale_x_continuous

  1. library(tidyverse)
  2. df %&gt;%
  3. group_by(S) %&gt;%
  4. mutate(Y = factor(Y)) %&gt;%
  5. summarize(xval = seq(1, nlevels(Y), length.out = 500),
  6. yval = approx(as.numeric(Y), V, ties = &quot;ordered&quot;, xout = xval)$y,
  7. Y = levels(Y)[floor(xval)], .groups = &quot;drop&quot;) %&gt;%
  8. ggplot(aes(xval, yval, fill = yval)) +
  9. geom_col(width = 0.01, position = &quot;identity&quot;) +
  10. geom_line(linewidth = 1) +
  11. facet_wrap(~ S, ncol = 1, strip.position = &quot;left&quot;) +
  12. scale_fill_gradient(low = &quot;blue&quot;, high = &quot;red&quot;) +
  13. scale_x_continuous(breaks = seq(length(unique(df$Y))),
  14. labels = unique(df$Y), expand = c(0, 0.5, 0, 0.5)) +
  15. labs(x = NULL, y = NULL, fill = &quot;V&quot;) +
  16. theme_classic(base_size = 14) +
  17. theme(axis.text.x = element_text(face = &quot;bold&quot;, angle = 90),
  18. axis.text.y = element_blank(),
  19. legend.text = element_text(face = &quot;bold&quot;),
  20. legend.title = element_text(face = &quot;bold&quot;))

填充 ggplot2 图的区域是否可以根据每个 x 轴坐标处的值进行更改?

huangapple
  • 本文由 发表于 2023年3月8日 17:52:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定