gpt4 book ai didi

r - 如何在 R 中绘制因子变量

转载 作者:行者123 更新时间:2023-12-02 13:11:43 26 4
gpt4 key购买 nike

我的数据框是这样的:

ind.heights <- structure(list(names = structure(c(3L, 4L, 5L, 1L, 2L), .Label = c("bagmati", 
"bardiya", "Jumla", "kalikot", "ramechap"), class = "factor"),
heights = c(5.6, 4.5, 6.1, 5.5, 5.1), placesSD = c(2, 2,
2, 2, 2), lower_boundary = c(3.84695491884684, 2.74695491884684,
4.34695491884684, 3.74695491884684, 3.34695491884684), upper_boundary = c(7.35304508115316,
6.25304508115316, 7.85304508115316, 7.25304508115316, 6.85304508115316
)), row.names = c(NA, -5L), class = "data.frame")

我尝试绘制该数据的散点图,如下所示:

plot(
# x= names, y = heights,
heights ~ names,
data = ind.heights,
col = "blue",
pch = 19,
main = "Heights",
ylab = "Heights",
ylim = c(
min(ind.heights$lower_boundary),
max(ind.heights$upper_boundary)
)
)

这里的问题是我的变量是一个因素,无法正确绘制,但如果我用数值更改变量并运行相同的代码,我会得到所需的绘图。

ind.heights$names <- 1:5
plot(
# x= names, y = heights,
heights ~ names,
data = ind.heights,
col = "blue",
pch = 19,
main = "Heights",
ylab = "Heights",
ylim = c(
min(ind.heights$lower_boundary),
max(ind.heights$upper_boundary)
)
)

如何使用具有原始名称的因子绘制此图?

最佳答案

不知道如何使用基本 R,但可以使用 ggplot 来完成:

ggplot(ind.heights, aes(x = names, y = heights)) +
geom_point(size = 4) +
geom_errorbar(aes(ymax = upper_boundary, ymin = lower_boundary))

关于r - 如何在 R 中绘制因子变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59537445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com