简体   繁体   English

使用基本绘图功能翻转 x 和 y 轴

[英]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:但我想创建的输出如下所示:

final_plot

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 :结果

The resulting barplot would look like this:生成的条形图如下所示: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基本 x、y、z plot 在 R 中使用 CSV - Basic x, y, z plot using CSV in R 使用轮廓图的x,y轴值不正确 - incorrect values of x, y axis using contour plot 有没有办法使用 lidR 包在 x 和 y 轴上绘制“真实”坐标 - Is there a way to plot the ''true'' coordinates on the x and y axis using lidR package 如何使用辅助y轴但使用相同的x轴在BOX绘图中添加折线图 - How can I add a line plot in a BOX plot using secondary y axis but same x axis 如何使用R中的绘图功能更改散点图中x轴和y轴标签的字体大小和颜色? - How to change the font size and color of x-axis and y-axis label in a scatterplot with plot function in R? 是否有一个函数可以在 r 中的两个 x 轴和两个 y 轴变量上绘制堆叠图 - Is there a function that does a stacked plot on two x-axis and two y-axis variables in r 一个x和y轴上的9个图 - 9 graph plot in one x and y axis 仅使用x轴上的列和y轴上的整列中的某些行值来创建图 - Create a plot using only certain row values from a column in the x axis and the full column on the y axis 使用r中的ggplot在y轴上使用相同的x轴绘制多个变量 - Plot multiple variables on y-axis with the same x-axis using ggplot in r 如何使用 ggplot 在 x 轴上绘制日期,在 y 轴上绘制价格表和属性? - How can I plot date on x-axis and price list on y-axis and attribute by using ggplot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM