geom_errorbar() 无法将标准差读取为数值,也不会添加误差线
geom_errorbar() cannot read standard deviation as numerical value and would not add error bars
我无法可视化我的数据。我希望有人能帮帮忙。我实际上有很多问题。
数据
crap <- tribble(~Person, ~Group, ~Measurement_1, ~Measurement_2, ~Measurement_3,
"Alex","A",14.6,52,115,
"Alex","B",12.3,63,116,
"Alex","C",15.5,45,186,
"Alex","D",16.3,35,146,
"Alex","E",18.7,66,149,
"Dante","A",19.6,57,185,
"Dante","B",15.2,75,187,
"Dante","C",12.9,64,199,
"Dante","D",11.3,69,142,
"Dante","E",13.4,43,134,
"Dante","A",18.7,44,132,
"Clyde","B",12.9,62,135,
"Clyde","C",11.5,65,165,
"Clyde","D",10.5,60,183,
"Clyde","E",11.9,55,145)
目标:
根据 Measurement_1、Measurement_2、Measurement_3
的值对“人物”重新排序
因为我想要 Y 值递增顺序的条形图,所以我根据列“Measurement_1”、“Measurement_2”、“Measurement_3" 使用 fct_reorder()
blue <- crap %>%
mutate(reorder_M1 = fct_reorder(Person, Measurement_1)) %>%
mutate(reorder_M2 = fct_reorder(Person, Measurement_1)) %>%
mutate(reorder_M3 = fct_reorder(Person, Measurement_1))
使用下面找到的列的配对,基于单个数据框创建条形图。
plot1 < x = reorder_M1, y = Measurement_1
plot2 < x = reorder_M2, y = Measurement_2
plot3 < x = reorder_M3, y = Measurement_3
但是,这是我出错的地方,当使用 geom_errorbar()
添加误差条时,我收到一条错误消息,说 Error in Measurement_1 - sd : non-numeric argument to binary operator
似乎无法将 sd
识别为标准偏差值.
此外,有没有一种方法可以使用单个代码块使用上面的配对创建多个条形图。在不融化数据的情况下这可能吗?哪个 apply
最适合这个
谢谢,希望有人能赐教。
这是我正在尝试的代码:
crap_plot <- ggplot(data = blue,
aes(x = reorder_M1,
y = Measurement_1,
fill = Group)) +
geom_bar(stat = "identity", position = "dodge") +
geom_errorbar((aes(ymin=Measurement_1-sd, ymax=Measurement+sd)))
请注意,像这样使用 geom_errorbar 你可以解决 geom_errorbar((aes(ymin=Measurement_1-sd(Measurement_1), ymax=Measurement+sd(Measurement_1))))
但你确实会在每个组中获得相同的栏,它不会明智地进行分组。
我建议改用它,它只会显示“A”组中“Dante”的错误,因为您的示例数据对于其他组使 SD=0 只有一个值。
ggplot(blue, aes(x = reorder_M1, y = Measurement_1, fill = Group)) +
stat_summary(fun = mean, geom = "bar", position = "dodge") +
stat_summary(fun.data = "mean_se", geom = "errorbar", position = position_dodge(width = 0.90), width = 0.3)
我无法可视化我的数据。我希望有人能帮帮忙。我实际上有很多问题。
数据
crap <- tribble(~Person, ~Group, ~Measurement_1, ~Measurement_2, ~Measurement_3,
"Alex","A",14.6,52,115,
"Alex","B",12.3,63,116,
"Alex","C",15.5,45,186,
"Alex","D",16.3,35,146,
"Alex","E",18.7,66,149,
"Dante","A",19.6,57,185,
"Dante","B",15.2,75,187,
"Dante","C",12.9,64,199,
"Dante","D",11.3,69,142,
"Dante","E",13.4,43,134,
"Dante","A",18.7,44,132,
"Clyde","B",12.9,62,135,
"Clyde","C",11.5,65,165,
"Clyde","D",10.5,60,183,
"Clyde","E",11.9,55,145)
目标:
根据 Measurement_1、Measurement_2、Measurement_3
的值对“人物”重新排序因为我想要 Y 值递增顺序的条形图,所以我根据列“Measurement_1”、“Measurement_2”、“Measurement_3" 使用
fct_reorder()
blue <- crap %>%
mutate(reorder_M1 = fct_reorder(Person, Measurement_1)) %>%
mutate(reorder_M2 = fct_reorder(Person, Measurement_1)) %>%
mutate(reorder_M3 = fct_reorder(Person, Measurement_1))
使用下面找到的列的配对,基于单个数据框创建条形图。
plot1 < x = reorder_M1, y = Measurement_1
plot2 < x = reorder_M2, y = Measurement_2
plot3 < x = reorder_M3, y = Measurement_3
但是,这是我出错的地方,当使用 geom_errorbar()
添加误差条时,我收到一条错误消息,说 Error in Measurement_1 - sd : non-numeric argument to binary operator
似乎无法将 sd
识别为标准偏差值.
此外,有没有一种方法可以使用单个代码块使用上面的配对创建多个条形图。在不融化数据的情况下这可能吗?哪个 apply
最适合这个
谢谢,希望有人能赐教。
这是我正在尝试的代码:
crap_plot <- ggplot(data = blue,
aes(x = reorder_M1,
y = Measurement_1,
fill = Group)) +
geom_bar(stat = "identity", position = "dodge") +
geom_errorbar((aes(ymin=Measurement_1-sd, ymax=Measurement+sd)))
请注意,像这样使用 geom_errorbar 你可以解决 geom_errorbar((aes(ymin=Measurement_1-sd(Measurement_1), ymax=Measurement+sd(Measurement_1))))
但你确实会在每个组中获得相同的栏,它不会明智地进行分组。
我建议改用它,它只会显示“A”组中“Dante”的错误,因为您的示例数据对于其他组使 SD=0 只有一个值。
ggplot(blue, aes(x = reorder_M1, y = Measurement_1, fill = Group)) +
stat_summary(fun = mean, geom = "bar", position = "dodge") +
stat_summary(fun.data = "mean_se", geom = "errorbar", position = position_dodge(width = 0.90), width = 0.3)