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

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

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

问题

我有以下的数据框。

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

我正在如下绘制它。

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

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

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

英文:

I have the following dataframe.

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

I am plotting it as follows.

ggplot(data = df, aes(x = Y, 
                      y = V, group = 1, fill=V)) +
  geom_line(linewidth=3) +
  theme_classic()+
  labs(title=&quot;&quot;,
       x=paste(&quot;&quot;),
       y=paste(&quot;&quot;),
  )+
  theme(plot.title=element_text(size=18, face=&quot;bold&quot;,hjust = 0.7, vjust=0.2,
                                margin = margin(t = 0, r = 10, b = 10, l = 0)),
        axis.text.x=element_text(size=14, face=&quot;bold&quot;, angle=90),
        axis.text.y=element_blank(),
        axis.title.x=element_text(size=16, face=&quot;bold&quot;,
                                  margin = margin(t = 10, r = 0, b = 0, l = 0)),
        axis.title.y=element_blank(),
        legend.text=element_text(size=14, face=&quot;bold&quot;),
        legend.title =element_text(size=14, face=&quot;bold&quot;))+
  facet_wrap(~ S, ncol= 1, strip.position=&quot;left&quot;)+
  geom_area()+
  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

以下是代码部分的翻译:

library(tidyverse)

df %>%
  group_by(S) %>%
  mutate(Y = factor(Y)) %>%
  summarize(xval = seq(1, nlevels(Y), length.out = 500),
            yval = approx(as.numeric(Y), V, ties = "ordered", xout = xval)$y,
            Y = levels(Y)[floor(xval)], .groups = "drop") %>%
  ggplot(aes(xval, yval, fill = yval)) +
  geom_col(width = 0.01, position = "identity") +
  geom_line(linewidth = 1) +
  facet_wrap(~ S, ncol = 1, strip.position = "left") +
  scale_fill_gradient(low = "blue", high = "red") +
  scale_x_continuous(breaks = seq(length(unique(df$Y))),
                     labels = unique(df$Y), expand = c(0, 0.5, 0, 0.5)) +
  labs(x = NULL, y = NULL, fill = "V") +
  theme_classic(base_size = 14) +
  theme(axis.text.x = element_text(face = "bold", angle = 90),
        axis.text.y = element_blank(),
        legend.text = element_text(face = "bold"),
        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

library(tidyverse)

df %&gt;%
  group_by(S) %&gt;%
  mutate(Y = factor(Y)) %&gt;%
  summarize(xval = seq(1, nlevels(Y), length.out = 500),
            yval = approx(as.numeric(Y), V, ties = &quot;ordered&quot;, xout = xval)$y,
            Y = levels(Y)[floor(xval)], .groups = &quot;drop&quot;) %&gt;%
  ggplot(aes(xval, yval, fill = yval)) +
  geom_col(width = 0.01, position = &quot;identity&quot;) +
  geom_line(linewidth = 1) +
  facet_wrap(~ S, ncol = 1, strip.position = &quot;left&quot;) +
  scale_fill_gradient(low = &quot;blue&quot;, high = &quot;red&quot;) +
  scale_x_continuous(breaks = seq(length(unique(df$Y))),
                     labels = unique(df$Y), expand = c(0, 0.5, 0, 0.5)) +
  labs(x = NULL, y = NULL, fill = &quot;V&quot;) +
  theme_classic(base_size = 14) +
  theme(axis.text.x = element_text(face = &quot;bold&quot;, angle = 90),
        axis.text.y = element_blank(),
        legend.text = element_text(face = &quot;bold&quot;),
        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:

确定