r - 错误:美学必须是长度 1 或与数据 (4) 相同:颜色:在绘制 geom_line() 并在 1 个图中设置 ggplot(aes(colour=)) 时
问题描述
我在下面的代码中创建了一个条形图:FC_TS 是一个数据框 (36 x 3),由 3 列组成:姓氏、特征(Action、Reflection、Flexibility_thinking、Structure 之一)、值(每列 1-7姓氏,用于每个功能(屏幕截图 FC_TS)和 team_mean_diff(个人价值与团队平均值之间的差异。
FC_TS:
structure(list(`Last Name` = c("x", "y", "z",
"u", "v", "t", "s", "g", "h",
"x", "y", "z", "u", "v", "t",
"s", "g", "h", "x", "y",
"z", "u", "v", "t", "s", "g",
"h", "x", "y", "z", "u",
"v", "t", "s", "g", "h"), Feature = c("Action",
"Action", "Action", "Action", "Action", "Action", "Action", "Action",
"Action", "Reflection", "Reflection", "Reflection", "Reflection",
"Reflection", "Reflection", "Reflection", "Reflection", "Reflection",
"Flexibility_Thinking", "Flexibility_Thinking", "Flexibility_Thinking",
"Flexibility_Thinking", "Flexibility_Thinking", "Flexibility_Thinking",
"Flexibility_Thinking", "Flexibility_Thinking", "Flexibility_Thinking",
"Structure", "Structure", "Structure", "Structure", "Structure",
"Structure", "Structure", "Structure", "Structure"), Value = c(4,
2, 3, 2, 4, 7, 4, 5, 4, 4, 6, 5, 6, 4, 1, 4, 3, 4, 7, 6, 6, 1,
5, 6, 4, 7, 6, 1, 2, 2, 7, 3, 2, 4, 1, 2), team_mean_diff = c(0.111111111111111,
-2.11111111111111, -2.33333333333333, -0.666666666666667, 0.111111111111111,
2.88888888888889, -1.33333333333333, 2.33333333333333, 0.111111111111111,
-0.111111111111111, 0.666666666666667, 2.33333333333333, 2.11111111111111,
-0.111111111111111, -4.33333333333333, 1.33333333333333, -0.888888888888889,
-0.111111111111111, 1.66666666666667, 3.33333333333333, 2.11111111111111,
-3.11111111111111, -0.333333333333333, 3.33333333333333, 0.111111111111111,
2.88888888888889, 0.666666666666667, -1.66666666666667, -1.88888888888889,
-2.11111111111111, 1.66666666666667, 0.333333333333333, -1.88888888888889,
-0.111111111111111, -4.33333333333333, -0.666666666666667)), row.names = c(NA,
-36L), class = c("tbl_df", "tbl", "data.frame"))
TS_means 是一个数据框 (4x2),其中包含 mean_name 列(Action、Reflection、Flexibility_thinking、Structure)和 mean(每个特征姓氏的平均值)。I_TS_means 和 S_TS_means 是 4 个特征中每个特征的行业平均值和 4 个特征中每个特征的阶段平均值(也是 4x2)。这三个均值在条形图上绘制为线(屏幕截图均值线)。
TS_means:
structure(list(mean_name = c("Action", "Reflection", "Flexibility_Thinking",
"Structure"), means = c(3.88888888888889, 4.11111111111111, 5.33333333333333,
2.66666666666667)), class = "data.frame", row.names = c(NA, -4L
))
TS_bar <-
ggplot(FC_TS, aes(x = factor(Feature, level = level_order), y = Value)) + geom_col(aes(fill = `Last Name`), position = "dodge") + coord_cartesian(ylim = c(1, 7)) + scale_y_continuous(n.breaks = 7) + theme_bw() + geom_line(data = TS_means, aes(x = `mean_name`,y = `means`,group = 1,colour = "#28d7ac")) + geom_line(data = I_TS_means,aes(x = `mean_name`,y = `means`,group = 1,colour = "#2a2b63")) + geom_line(data = S_TS_means,aes(x = `mean_name`,y = `means`,group = 1,colour = "#f2eff2")) + scale_color_manual(values = c("#28d7ac", "#2a2b63", "#f2eff2"),name = "Means",labels = c("Team Mean", "Industry Mean", "Stage Mean")
) + ggtitle("Founder Check Evaluation: Thinking Style") +
xlab("Thinking Style") + ylab("") + scale_x_discrete(labels = c('Action','Flexibility', 'Reflection','Structure'))
现在,除了我已经绘制的平均线之外,如果它们高于 5,我想包括突出显示“结构”中的列的功能。到目前为止,我只设法突出显示而不同时绘制平均线(屏幕截图突出显示),因为如果我这样做,它会给我以下错误:“错误:美学必须是长度 1 或与数据相同 (4) : 颜色”。
TS_bar_1 <- ggplot(
FC_TS,
aes(
x = factor(FC_TS$Feature, level = level_order),
y = FC_TS$Value,
colour = (FC_TS$team_mean_diff >= 2 | FC_TS$team_mean_diff <= - 2))
) +
scale_colour_manual(
name = 'High Structure',
values = setNames(c('red', NA),c(T, F))
) +
geom_col(
aes(fill = `Last Name`),
position = "dodge"
) +
coord_cartesian(ylim = c(1, 7)) +
scale_y_continuous(n.breaks = 7) +
theme_bw() +
geom_line(
data = TS_means,
aes(x = `mean_name`, y = `means`, group = 1)
) +
geom_line(
data = I_TS_means,
aes(x = `mean_name`, y = `means`, group = 1)
) +
geom_line(
data = S_TS_means,
aes( x = `mean_name`, y = `means`, group = 1)
) +
ggtitle("Founder Check Evaluation: Thinking Style") +
xlab("Thinking Style") +
ylab("") +
scale_x_discrete(labels = c('Action','Flexibility', 'Reflection','Structure'))
有没有办法在一个图中同时显示平均线和突出显示的条形?
解决方案
推荐阅读
- c++ - 标准差公式
- python - 安装 Tensorflow 时遇到问题 - 为 wrapt 构建轮子(setup.py)...错误
- python - django-admin.py startproject mysite 不工作
- intellij-idea - IdeaVim:从命令更改为插入模式并返回等时,有没有办法更改背景颜色?例如每种模式都有自己的背景颜色
- java - 由于只读消息和 Mac 上的灰色编辑器,我无法在 NetBeans 中编辑文件
- swift - 如何在 SwiftUI 中删除主要内容视图和标签栏之间的间距?
- graphql - NestJS/graphql - 拦截 Apollo HTTP 错误
- postgresql - 无法将主机名“postgres”转换为地址:名称或服务未知
- python - 如何在机器学习中使用 cross_val_score 进行预测
- vba - 1个记录集可以同时执行2个选择查询吗?