![](/img/trans.png)
[英]Construct a scatterplot of the selling price against the living area for the two groups
[英]Scatterplot numerical y against two x groups
根据我的数据,我创建了以下向量:
ratio_automatic <- c(2.031, 2.24, 2.00, 0.46, 2.75, 0.86, 2.69, 0.44)
ratio_manual <- c(1.02, 2.40, 1.53, 0.50, 1.38, 0.70, 1.69, 0.54)
method <- rep(c("Manual", "Automatic"), each = 8)
创建以下 dataframe:
df <- data.frame(method, c(ratio_automatic, ratio_manual))
names(df) <- c("method", "ratio")
所以我认为这就是所谓的窄数据? 总之:两个变量有 16 个观察值。
我想要 plot 散点图中的数据,因此您可以看到按类别(“手动”或“自动”)分组的比率数据点。 当我运行以下代码时:
plot(1:nrow(df), df$ratio, xaxt="n", xlab="Method", ylab="Ratio", pch=19)
axis(1, at=1:16, labels=df$method)
这不是我想要的,因为我想要在 x 轴上只是“自动”和“手动”,然后是上面的相应点,而不是像现在这样每个点分开。
有没有办法来解决这个问题?
(我对 R 还是很陌生,如果这是一个明显的问题,我很抱歉。)
也许你想要这样的东西,你从一个空的 plot 开始,然后在你的因子方法的 1 和 2 上使用 plot 并使用points
来添加这样的数据,这会导致抖动 plot(你可以调整标签的位置axis
):
plot(1, type="n", xlim=c(0, 3), xaxt="n", ylim = c(0, 3), xlab="Method", ylab="Ratio", pch=19)
axis(1, at=1:2, labels=unique(df$method))
points(factor(df$method), df$ratio, xaxt="n", xlab="Method", ylab="Ratio", pch=19)
Output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.