英文:
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坐标时相对应。
英文:
I have the following dataframe.
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)
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="",
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")
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.
答案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 %>%
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"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论