[英]Flip x and y axis using basic plot function
I have aa vector v3 with levels: setosa versicolor virginica我有一个带有级别的向量 v3:setosa versicolor virginica
When I plot the vector using the basic plot function;当我使用基本绘图函数绘制向量时;
plot(v3, type = "s", xlim = NULL, ylim = c(0,50),
main = "Plot 2", ylab = 'Frequency', col = "blue")
I get the following plot我得到以下情节
But the output I would like to create looks like this:但我想创建的输出如下所示:
I know certain (more elegant) solutions exist but I would like to create this without installing & loading additional packages.我知道存在某些(更优雅的)解决方案,但我想在不安装和加载其他软件包的情况下创建它。 I tried the following with axis:
我用轴尝试了以下操作:
axis(1, at = c(0,50), labels = FALSE, tick = TRUE)
axis(2, at = levels(v3), labels = FALSE, tick = TRUE)
But R would not accept it.但R不会接受它。
Thanks for any input!感谢您提供任何意见!
It seems what you are looking for is a barplot rather than a scatter plot.看起来您正在寻找的是条形图而不是散点图。 Let's say you have data like this:
假设您有这样的数据:
DATA :数据:
set.seed(321)
v3 <- sample(c("setosa", "versicolor", "virginica"), 100, replace = T)
v3
[1] "versicolor" "versicolor" "setosa" "setosa" "setosa" "versicolor" "versicolor" "setosa"
[9] "virginica" "virginica" "setosa" "virginica" "versicolor" "virginica" "versicolor" "virginica"
[17] "setosa" "versicolor" "virginica" "virginica" "versicolor" "versicolor" "virginica" "versicolor"
[25] "virginica" "versicolor" "setosa" "versicolor" "setosa" "virginica" "setosa" "setosa"
[33] "virginica" "versicolor" "setosa" "virginica" "versicolor" "setosa" "versicolor" "setosa"
[41] "virginica" "versicolor" "setosa" "virginica" "setosa" "versicolor" "versicolor" "setosa"
[49] "setosa" "virginica" "virginica" "virginica" "setosa" "virginica" "versicolor" "versicolor"
[57] "setosa" "setosa" "virginica" "setosa" "setosa" "versicolor" "virginica" "virginica"
[65] "virginica" "setosa" "virginica" "versicolor" "versicolor" "versicolor" "virginica" "versicolor"
[73] "virginica" "setosa" "setosa" "versicolor" "virginica" "versicolor" "versicolor" "versicolor"
[81] "versicolor" "virginica" "setosa" "virginica" "setosa" "versicolor" "virginica" "setosa"
[89] "versicolor" "versicolor" "virginica" "setosa" "virginica" "virginica" "virginica" "versicolor"
[97] "setosa" "virginica" "virginica" "setosa"
What you cannot do is plot factor levels;你不能做的是绘制因子水平; you can only count the number of times the levels occur in your data: these frequencies you can plot.
您只能计算数据中出现级别的次数:您可以绘制这些频率。 You can do this by tabulating the vector
v3
using the table
function.您可以通过使用
table
函数将向量v3
制表来完成此操作。 To flip the bars into horizontal position you can use the argument horiz = TRUE
(doing that will also necessitate that you put the label Frequency
onto the x-axis rather than the y-axis):要将条形翻转到水平位置,您可以使用参数
horiz = TRUE
(这样做还需要将标签Frequency
放在 x 轴而不是 y 轴上):
barplot(table(v3), horiz = T, main = "Plot 2",
xlab = 'Frequency',
ylab = 'Species',
col = 'blue')
RESULT :结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.