gpt4 book ai didi

r - 使用 ggplot 和 base plot 函数获得不同的结果

转载 作者:行者123 更新时间:2023-12-04 20:05:06 25 4
gpt4 key购买 nike

我这里有一张 table :http://ulozto.cz/xAeP3Ahn/res2-txt .我试图从中制作一个点图。

我读了我的表:

res2<-read.table("res2.txt", header = TRUE, sep="\t")

并创建 2 个图。

(1) 这是单绘图函数的脚本:
plot(res2$V2, res2$dist06, type = "n")
points(subset(res2$V2, year == 2006), subset(res2$dist06, year == 2006), pch = 19, col = "red", cex = 1)
points(subset(res2$V2, year == 2007), subset(res2$dist06, year == 2007), pch = 19, col = "green", cex = 1)
points(subset(res2$V2, year == 2008), subset(res2$dist06, year == 2008), pch = 19, col = "black", cex = 1)
points(subset(res2$V2, year == 2009), subset(res2$dist06, year == 2009), pch = 19, col = "blue", cex = 1)
points(subset(res2$V2, year == 2011), subset(res2$dist06, year == 2011), pch = 19, col = "yellow", cex = 1)
legend("topright", c("2006", "2007", "2008", "2009", "2011"),
col= c("red", "green", "black", "blue", "yellow"),
pch = c(19,19,19,19,19))

(2) 和 ggplot2:
res2$year<-as.factor(res2$year)  # consider year variable as discrete
ggplot(data=res2, aes(x=V2, y=dist06, color=year)) + geom_point(shape=16, pch=50) +
xlab("threshold") + ylab("Euclidean distance") +
scale_fill_hue(name="year") + # set legend title
scale_colour_manual(values=c("red", "green", "black", "blue", "yellow")) +
theme_bw()

这是我的结果:

results from simple plot function (1) and froom ggplot2 (2)

我的问题是, 为什么我在不同生成的图中有不同的点位置? 问题只是在不同的颜色和图例中吗?那么“子集”定义错误吗?为什么 2006 在两者中都被标记为红色,但在图表中的位置不同?与2011年和其他人一样吗?我哪里错了?感谢您的每一个建议,第三天我在这里迷路了。

这是我从 excel 中得到的结果,所以来自 ggplot2 (2) 的情节必须是正确的
plot from same data in excel

最佳答案

我想这是不正确使用 subset 的副作用。 .它的第一个参数应该是整个数据框,如下所示:

subset(res2, year == 2006)$V2
或者
subset(res2, year == 2006, select = V2)
(旁注:这些命令返回的对象不同,但都适用于您的情节)
我建议使用括号表示法:
res2$V2[res2$year == 2006]
无论哪种方式,你都会得到一个正确的情节:
enter image description here
您可能已经注意到,您不必使用 ggplot 进行大量复制/粘贴操作。方法。好的!

关于r - 使用 ggplot 和 base plot 函数获得不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26523634/

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