geom_bar 的渐变填充相对于每个条缩放并且未映射到变量
Gradient fill for geom_bar scaled relative to each bar and not mapped to a variable
我的目标是重现此图,而我的问题是重现每个条形中的渐变填充。
在评论后添加
@PavoDive 的好评将我们引向 a question,基本上说 "You can't do it with ggplot2 and furthermore it is a bad idea even if you could do it." 同意这是一个糟糕的图形选择,但出于教学目的,我想重新创建原始和然后显示改进。那么,有没有编程解决方案呢?
使用 ggplot
代码之后的数据,我已经接近了,除了每个条形(和微小的刻度线)都相同的一致渐变着色。但是我的努力导致填充的条与 y 值相匹配,而在原始图中每个条都填充有相同的模式。我如何实现这种效果?
ggplot(df, aes(x = x, y = y, fill = y)) +
geom_hline(yintercept = seq(0, .35, .05), color = "grey30", size = 0.5, linetype = "solid") +
geom_bar(stat = "identity", width = 0.4) +
scale_fill_gradient(low='green4', high='green1', guide = FALSE) +
theme(legend.position = "none") +
theme_minimal() +
geom_text(data = df, aes(label = scales::percent(y), vjust = -.5)) +
theme(axis.text.y = element_blank()) +
theme(axis.ticks = element_blank()) +
labs(y = "", x = "") +
ggtitle("Question 15: Do you feel prepared to comply with the upcoming December
2015 updated requirements of the FRCP that relate to ediscovery") +
theme(plot.title = element_text(face = "bold", size = 18)) +
theme(panel.border = element_blank())
数据
df <- data.frame(x = c("Prepared", "Somewhat\nprepared", "Not prepared", "Very prepared"),
y = c(.32, .31, .20, .17))
df$x <- factor(df$x, levels = c("Prepared", "Somewhat\nPrepared", "Not Prepared", "Very Prepared"))
这是一个选项,使用 geom_path
和缩放的 y 来着色而不是条形。这将创建一些新数据 (dat
),从 0 到每个 df$y
值的序列(此处长度为 100,在列 dat$y
中)。然后,创建每个序列的缩放版本(从 0 到 1),用作颜色渐变(称为 dat$scaled
)。缩放是通过简单地将每个序列除以其最大值来完成的。
## Make the data for geom_path
mat <- mapply(seq, 0, df$y, MoreArgs = list(length=100)) # make sequences 0 to each df$y
dat <- stack(data.frame(lapply(split(mat, col(mat)), function(x) x/tail(x,1)))) # scale each, and reshape
dat$x <- rep(df$x, each=100) # add the x-factors
dat$y <- stack(as.data.frame(mat))[,1] # add the unscaled column
names(dat)[1] <- "scaled" # rename
## Make the plot
ggplot(dat, aes(x, y, color=scaled)) + # use different data
## *** removed some code here ***
geom_hline(yintercept = seq(0, .35, .05), color = "grey30", size = 0.5, linetype = "solid") +
theme(legend.position = "none") +
theme_minimal() +
geom_text(data = df, aes(label = scales::percent(y), vjust = -.5), color="black") +
theme(axis.text.y = element_blank()) +
theme(axis.ticks = element_blank()) +
labs(y = "", x = "") +
ggtitle("Question 15: Do you feel prepared to comply with the upcoming December
2015 updated requirements of the FRCP that relate to ediscovery") +
theme(plot.title = element_text(face = "bold", size = 18)) +
theme(panel.border = element_blank()) +
## *** Added this code ***
geom_path(lwd=20) +
scale_color_continuous(low='green4', high='green1', guide=F)
这可以通过包 gridSVG
中的函数来实现。我使用你的例子的精简版,只包含解决实际问题最必要的部分:
# load additional packages
library(grid)
library(gridSVG)
# create a small data set
df <- data.frame(x = factor(1:3), y = 1:3)
# a basic bar plot to be modified
ggplot(data = df, aes(x = x, y = y)) +
geom_bar(stat = "identity")
# create a linear color gradient
cols <- linearGradient(col = c("green4", "green1"),
x0 = unit(0.5, "npc"), x1 = unit(0.5, "npc"))
# create a definition of a gradient fill
registerGradientFill(label = "cols", gradient = cols)
# list the names of grobs and look for the relevant geometry
grid.ls()
# GRID.gTableParent.76
# ...snip...
# panel.3-4-3-4
# geom_rect.rect.2 # <~~~~ this is the grob! Note that the number may differ
# apply the gradient to each bar
grid.gradientFill("geom_rect.rect", label = rep("cols", length(unique(df$x))),
group = FALSE, grep = TRUE)
# generate SVG output from the grid graphics
grid.export("myplot.svg")
我的目标是重现此图,而我的问题是重现每个条形中的渐变填充。
在评论后添加 @PavoDive 的好评将我们引向 a question,基本上说 "You can't do it with ggplot2 and furthermore it is a bad idea even if you could do it." 同意这是一个糟糕的图形选择,但出于教学目的,我想重新创建原始和然后显示改进。那么,有没有编程解决方案呢?
使用 ggplot
代码之后的数据,我已经接近了,除了每个条形(和微小的刻度线)都相同的一致渐变着色。但是我的努力导致填充的条与 y 值相匹配,而在原始图中每个条都填充有相同的模式。我如何实现这种效果?
ggplot(df, aes(x = x, y = y, fill = y)) +
geom_hline(yintercept = seq(0, .35, .05), color = "grey30", size = 0.5, linetype = "solid") +
geom_bar(stat = "identity", width = 0.4) +
scale_fill_gradient(low='green4', high='green1', guide = FALSE) +
theme(legend.position = "none") +
theme_minimal() +
geom_text(data = df, aes(label = scales::percent(y), vjust = -.5)) +
theme(axis.text.y = element_blank()) +
theme(axis.ticks = element_blank()) +
labs(y = "", x = "") +
ggtitle("Question 15: Do you feel prepared to comply with the upcoming December
2015 updated requirements of the FRCP that relate to ediscovery") +
theme(plot.title = element_text(face = "bold", size = 18)) +
theme(panel.border = element_blank())
数据
df <- data.frame(x = c("Prepared", "Somewhat\nprepared", "Not prepared", "Very prepared"),
y = c(.32, .31, .20, .17))
df$x <- factor(df$x, levels = c("Prepared", "Somewhat\nPrepared", "Not Prepared", "Very Prepared"))
这是一个选项,使用 geom_path
和缩放的 y 来着色而不是条形。这将创建一些新数据 (dat
),从 0 到每个 df$y
值的序列(此处长度为 100,在列 dat$y
中)。然后,创建每个序列的缩放版本(从 0 到 1),用作颜色渐变(称为 dat$scaled
)。缩放是通过简单地将每个序列除以其最大值来完成的。
## Make the data for geom_path
mat <- mapply(seq, 0, df$y, MoreArgs = list(length=100)) # make sequences 0 to each df$y
dat <- stack(data.frame(lapply(split(mat, col(mat)), function(x) x/tail(x,1)))) # scale each, and reshape
dat$x <- rep(df$x, each=100) # add the x-factors
dat$y <- stack(as.data.frame(mat))[,1] # add the unscaled column
names(dat)[1] <- "scaled" # rename
## Make the plot
ggplot(dat, aes(x, y, color=scaled)) + # use different data
## *** removed some code here ***
geom_hline(yintercept = seq(0, .35, .05), color = "grey30", size = 0.5, linetype = "solid") +
theme(legend.position = "none") +
theme_minimal() +
geom_text(data = df, aes(label = scales::percent(y), vjust = -.5), color="black") +
theme(axis.text.y = element_blank()) +
theme(axis.ticks = element_blank()) +
labs(y = "", x = "") +
ggtitle("Question 15: Do you feel prepared to comply with the upcoming December
2015 updated requirements of the FRCP that relate to ediscovery") +
theme(plot.title = element_text(face = "bold", size = 18)) +
theme(panel.border = element_blank()) +
## *** Added this code ***
geom_path(lwd=20) +
scale_color_continuous(low='green4', high='green1', guide=F)
这可以通过包 gridSVG
中的函数来实现。我使用你的例子的精简版,只包含解决实际问题最必要的部分:
# load additional packages
library(grid)
library(gridSVG)
# create a small data set
df <- data.frame(x = factor(1:3), y = 1:3)
# a basic bar plot to be modified
ggplot(data = df, aes(x = x, y = y)) +
geom_bar(stat = "identity")
# create a linear color gradient
cols <- linearGradient(col = c("green4", "green1"),
x0 = unit(0.5, "npc"), x1 = unit(0.5, "npc"))
# create a definition of a gradient fill
registerGradientFill(label = "cols", gradient = cols)
# list the names of grobs and look for the relevant geometry
grid.ls()
# GRID.gTableParent.76
# ...snip...
# panel.3-4-3-4
# geom_rect.rect.2 # <~~~~ this is the grob! Note that the number may differ
# apply the gradient to each bar
grid.gradientFill("geom_rect.rect", label = rep("cols", length(unique(df$x))),
group = FALSE, grep = TRUE)
# generate SVG output from the grid graphics
grid.export("myplot.svg")