gpt4 book ai didi

使用基本图形重新创建ggplot2 geom_point()

转载 作者:行者123 更新时间:2023-12-04 10:35:52 25 4
gpt4 key购买 nike

我有以下ggplot2代码,我想使用基本图形而不是ggplot2来生成类似的输出-但我似乎找不到找到一种方法来区分正常绘图中的多个“属性”。我错过了什么吗?

Ggplot2:

ggplot(data.df, aes(x=Axis1, y=Axis2, shape=Plant, color=Type)) +
geom_point()

我的情节尝试(内联帮助使我颇有收获):
data.ma <- as.matrix(data.df)

plot(range(data.ma[,6]), range(data.ma[,7]),xlab="Axis 1",ylab="Axis 2")
points(data.ma[data.ma[,1] == 'Plant1',6],
data.ma[data.ma[,1] == 'Plant1',7], pch=2)
points(data.ma[data.ma[,1] == 'Plant2',6],
data.ma[data.ma[,1] == 'Plant2',7], pch=3)
legend(0,legend=c("Plant1","Plant2"))

这给了我一个图,在图中至少可以区分“Plant”类型,但是看起来确实很复杂,我无法弄清楚如何根据“Type”行更改所有点的颜色。

有什么建议么?

编辑-一个带有数据//的示例,//我意识到我第一次进行绘图甚至都没有给出正确的示例:(:
library(ggplot2)
data.df <- data.frame(
Plant=c('Plant1','Plant1','Plant1','Plant2','Plant2','Plant2'),
Type=c(1,2,3,1,2,3),
Axis1=c(0.2,-0.4,0.8,-0.2,-0.7,0.1),
Axis2=c(0.5,0.3,-0.1,-0.3,-0.1,-0.8)
)


ggplot(data.df, aes(x=Axis1, y=Axis2, shape=Plant, color=Type)) +
geom_point()

data.ma <- as.matrix(data.df)
plot(range(data.ma[,3]), range(data.ma[,4]),xlab="Axis 1",ylab="Axis 2")
points(data.ma[data.ma[,1] == 'Plant1',3],
data.ma[data.ma[,1] == 'Plant1',4], pch=2)
points(data.ma[data.ma[,1] == 'Plant2',3],
data.ma[data.ma[,1] == 'Plant2',4], pch=3)
legend(0,legend=c("Plant1","Plant2"))

最佳答案

使用基本图和数据集:

with(data.df, 
plot(x = Axis1,
y = Axis2,
col = factor(Type),
pch = as.integer(factor(Plant))))

那能满足您的需求吗?我将把图例留给读者练习。

关于使用基本图形重新创建ggplot2 geom_point(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11679019/

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