ggplot:geom_polygon - 美学长度错误
ggplot: geom_polygon - Error in length of Aestetics
我想绘制一个散点图,其中包含三个多边形,背景颜色为特定区域。它应该看起来像 this。该代码适用于 y 轴上的 4 个变量,但是当我添加第五个变量时会抛出错误。我想不通原因。
错误:
错误:美学必须是长度1或与数据(5)相同:x,y
可重现代码:
library(dplyr)
library(ggplot2)
v1 <- c(1, 1, 1)
v2 <- c(1, 0, 0)
v3 <- c(1, 0, 1)
v4 <- c(0, 1, 1)
v5 <- c(1, 0, 1)
xG <- c(3, 3, 3, 3, 3)
input <- c(v1, v2, v3, v4, v5)
df <- data_frame(values = input,
module = c(rep("A", length(v1)),
rep("B", length(v2)),
rep("C", length(v3)),
rep("D", length(v4)),
rep("E", length(v5))))
perWorkField <- df %>%
group_by(module) %>%
summarise(sums = sum(values)) %>%
mutate(percent = round((sums / xG) * 100, 2))
ggplot(data = perWorkField) +
geom_point(mapping = aes(x = percent, y = module)) + xlim(c(0, 100)) +
geom_polygon(aes(x = c(0, 30, 30, 0), y = c(0.5, 0.5, 5.5, 5.5)),
fill="#F5817A",
color = NA) +
geom_polygon(aes(x = c(30, 75, 75, 30), y = c(0.5, 0.5, 5.5, 5.5)),
fill="#FFFF0044",
color = NA) +
geom_polygon(aes(x = c(75, 100, 100, 75), y = c(0.5, 0.5, 5.5, 5.5)),
fill="#00FF0044",
color = NA) +
geom_point(mapping = aes(x = percent, y = module), shape = 20, size = 5) + theme_classic()
我很感激任何建议。提前致谢!
你可以试试这个让它工作,我替换了数据参数以使其正常工作
ggplot() +
geom_point(data = perWorkField, mapping = aes(x = percent, y = module)) + xlim(c(0, 100)) +
geom_polygon(aes(x = c(0, 30, 30, 0), y = c(0.5, 0.5, 5.5, 5.5)),
fill="#F5817A",
color = NA) +
geom_polygon(aes(x = c(30, 75, 75, 30), y = c(0.5, 0.5, 5.5, 5.5)),
fill="#FFFF0044",
color = NA) +
geom_polygon(aes(x = c(75, 100, 100, 75), y = c(0.5, 0.5, 5.5, 5.5)),
fill="#00FF0044",
color = NA) +
geom_point(data = perWorkField, mapping = aes(x = percent, y = module), shape = 20, size = 5) + theme_classic()
由于您正在创建矩形阴影区域,geom_rect()
可能比 geom_polygon()
更易于阅读/维护。
此外,annotate()
可用于避免从数据框继承美学的问题:
ggplot(data = perWorkField) +
geom_point(aes(x = percent, y = module)) +
annotate("rect", xmin = 0, xmax = 30, ymin = 0.5, ymax = 5.5, fill = "#F5817A", color = NA) +
annotate("rect", xmin = 30, xmax = 75, ymin = 0.5, ymax = 5.5, fill = "#FFFF0044", color = NA) +
annotate("rect", xmin = 75, xmax = 100, ymin = 0.5, ymax = 5.5, fill = "#00FF0044", color = NA) +
geom_point(aes(x = percent, y = module), shape = 20, size = 5) +
theme_classic()
我想绘制一个散点图,其中包含三个多边形,背景颜色为特定区域。它应该看起来像 this。该代码适用于 y 轴上的 4 个变量,但是当我添加第五个变量时会抛出错误。我想不通原因。
错误:
错误:美学必须是长度1或与数据(5)相同:x,y
可重现代码:
library(dplyr)
library(ggplot2)
v1 <- c(1, 1, 1)
v2 <- c(1, 0, 0)
v3 <- c(1, 0, 1)
v4 <- c(0, 1, 1)
v5 <- c(1, 0, 1)
xG <- c(3, 3, 3, 3, 3)
input <- c(v1, v2, v3, v4, v5)
df <- data_frame(values = input,
module = c(rep("A", length(v1)),
rep("B", length(v2)),
rep("C", length(v3)),
rep("D", length(v4)),
rep("E", length(v5))))
perWorkField <- df %>%
group_by(module) %>%
summarise(sums = sum(values)) %>%
mutate(percent = round((sums / xG) * 100, 2))
ggplot(data = perWorkField) +
geom_point(mapping = aes(x = percent, y = module)) + xlim(c(0, 100)) +
geom_polygon(aes(x = c(0, 30, 30, 0), y = c(0.5, 0.5, 5.5, 5.5)),
fill="#F5817A",
color = NA) +
geom_polygon(aes(x = c(30, 75, 75, 30), y = c(0.5, 0.5, 5.5, 5.5)),
fill="#FFFF0044",
color = NA) +
geom_polygon(aes(x = c(75, 100, 100, 75), y = c(0.5, 0.5, 5.5, 5.5)),
fill="#00FF0044",
color = NA) +
geom_point(mapping = aes(x = percent, y = module), shape = 20, size = 5) + theme_classic()
我很感激任何建议。提前致谢!
你可以试试这个让它工作,我替换了数据参数以使其正常工作
ggplot() +
geom_point(data = perWorkField, mapping = aes(x = percent, y = module)) + xlim(c(0, 100)) +
geom_polygon(aes(x = c(0, 30, 30, 0), y = c(0.5, 0.5, 5.5, 5.5)),
fill="#F5817A",
color = NA) +
geom_polygon(aes(x = c(30, 75, 75, 30), y = c(0.5, 0.5, 5.5, 5.5)),
fill="#FFFF0044",
color = NA) +
geom_polygon(aes(x = c(75, 100, 100, 75), y = c(0.5, 0.5, 5.5, 5.5)),
fill="#00FF0044",
color = NA) +
geom_point(data = perWorkField, mapping = aes(x = percent, y = module), shape = 20, size = 5) + theme_classic()
由于您正在创建矩形阴影区域,geom_rect()
可能比 geom_polygon()
更易于阅读/维护。
此外,annotate()
可用于避免从数据框继承美学的问题:
ggplot(data = perWorkField) +
geom_point(aes(x = percent, y = module)) +
annotate("rect", xmin = 0, xmax = 30, ymin = 0.5, ymax = 5.5, fill = "#F5817A", color = NA) +
annotate("rect", xmin = 30, xmax = 75, ymin = 0.5, ymax = 5.5, fill = "#FFFF0044", color = NA) +
annotate("rect", xmin = 75, xmax = 100, ymin = 0.5, ymax = 5.5, fill = "#00FF0044", color = NA) +
geom_point(aes(x = percent, y = module), shape = 20, size = 5) +
theme_classic()