如何将数据框内容中指定的颜色分配给ggplot geoms?
How to assign colors specified within dataframe contents to ggplot geoms?
我正在尝试绘制一些点的图形,这些点后面有一些彩色背景区域。我希望填充颜色取决于 "quadrant" 名称并在数据框内指定(例如,"Indifferent" 在下面的示例中为粉红色)。我曾尝试将此列分配给 scale_fill_manual 的值参数,但这似乎没有效果,例如:
quadrant_names <- c("Indifferent", "Must be present", "Attractive", "Performance", "Dislike")
quadrant_xmin <- c(-1, 2, -1, 2, -2)
quadrant_xmax <- c(2, 4, 2, 4, 0)
quadrant_ymin <- c(0, 0, 2, 2, -2)
quadrant_ymax <- c(2, 2, 4, 4, 0)
quadrant_colors <-c("pink", "yellow", "blue", "green", "purple")
kano_quadrants <- data.frame(quadrant_names, quadrant_xmin, quadrant_xmax, quadrant_ymin, quadrant_ymax, quadrant_colors)
xpoints <- runif(10, -2, 4)
ypoints <- runif(10, -2, 4)
points <- data.frame(xpoints, ypoints)
ggplot(data=points, aes(x=xpoints, y=ypoints)) +
geom_rect(data=kano_quadrants, aes(xmin=quadrant_xmin, xmax=quadrant_xmax, x=NULL,
ymin=quadrant_ymin, ymax=quadrant_ymax, y=NULL,
fill=quadrant_names), alpha=0.2) +
geom_point() +
geom_text(data=kano_quadrants, aes(label=quadrant_names,
y=(quadrant_ymin+quadrant_ymax)/2,
x=(quadrant_xmin+quadrant_xmax)/2))+
geom_hline(yintercept=0) +
geom_vline(xintercept=0) +
theme_bw() +
scale_fill_manual(values=kano_quadrants$quadrant_colors)
结果:
我怎样才能将填充颜色分配给数据框中指定的 quadrant_name 的适当级别,而不是随机分配?
将 fill
映射到 quadrant_colors
并使用 scale_fill_identity()
library(ggplot2)
ggplot(data=points, aes(x=xpoints, y=ypoints)) +
geom_rect(data=kano_quadrants, aes(xmin=quadrant_xmin, xmax=quadrant_xmax, x=NULL,
ymin=quadrant_ymin, ymax=quadrant_ymax, y=NULL,
fill=quadrant_colors), alpha=0.2) +
geom_point() +
geom_text(data=kano_quadrants, aes(label=quadrant_names,
y=(quadrant_ymin+quadrant_ymax)/2,
x=(quadrant_xmin+quadrant_xmax)/2))+
geom_hline(yintercept=0) +
geom_vline(xintercept=0) +
theme_bw() +
scale_fill_identity()
我正在尝试绘制一些点的图形,这些点后面有一些彩色背景区域。我希望填充颜色取决于 "quadrant" 名称并在数据框内指定(例如,"Indifferent" 在下面的示例中为粉红色)。我曾尝试将此列分配给 scale_fill_manual 的值参数,但这似乎没有效果,例如:
quadrant_names <- c("Indifferent", "Must be present", "Attractive", "Performance", "Dislike")
quadrant_xmin <- c(-1, 2, -1, 2, -2)
quadrant_xmax <- c(2, 4, 2, 4, 0)
quadrant_ymin <- c(0, 0, 2, 2, -2)
quadrant_ymax <- c(2, 2, 4, 4, 0)
quadrant_colors <-c("pink", "yellow", "blue", "green", "purple")
kano_quadrants <- data.frame(quadrant_names, quadrant_xmin, quadrant_xmax, quadrant_ymin, quadrant_ymax, quadrant_colors)
xpoints <- runif(10, -2, 4)
ypoints <- runif(10, -2, 4)
points <- data.frame(xpoints, ypoints)
ggplot(data=points, aes(x=xpoints, y=ypoints)) +
geom_rect(data=kano_quadrants, aes(xmin=quadrant_xmin, xmax=quadrant_xmax, x=NULL,
ymin=quadrant_ymin, ymax=quadrant_ymax, y=NULL,
fill=quadrant_names), alpha=0.2) +
geom_point() +
geom_text(data=kano_quadrants, aes(label=quadrant_names,
y=(quadrant_ymin+quadrant_ymax)/2,
x=(quadrant_xmin+quadrant_xmax)/2))+
geom_hline(yintercept=0) +
geom_vline(xintercept=0) +
theme_bw() +
scale_fill_manual(values=kano_quadrants$quadrant_colors)
结果:
我怎样才能将填充颜色分配给数据框中指定的 quadrant_name 的适当级别,而不是随机分配?
将 fill
映射到 quadrant_colors
并使用 scale_fill_identity()
library(ggplot2)
ggplot(data=points, aes(x=xpoints, y=ypoints)) +
geom_rect(data=kano_quadrants, aes(xmin=quadrant_xmin, xmax=quadrant_xmax, x=NULL,
ymin=quadrant_ymin, ymax=quadrant_ymax, y=NULL,
fill=quadrant_colors), alpha=0.2) +
geom_point() +
geom_text(data=kano_quadrants, aes(label=quadrant_names,
y=(quadrant_ymin+quadrant_ymax)/2,
x=(quadrant_xmin+quadrant_xmax)/2))+
geom_hline(yintercept=0) +
geom_vline(xintercept=0) +
theme_bw() +
scale_fill_identity()