简体   繁体   English

如何创建在y轴上显示对象并在x轴上显示数字的图

[英]How to create a plot which shows objects on y-axis and number on x-axis

I do have a lots of data which I want to plot in a special way. 我确实有很多数据想要以特殊方式绘制。 But I don't know how to do this on R. The input is a csv file containing several columns. 但是我不知道如何在R上执行此操作。输入是包含几列的csv文件。 The columns I want to plot are A and D. A contains text and D numbers. 我要绘制的列是A和D。A包含文本和D数字。 The usesd text in column A can be there several times. A列中使用过的文本可以存在几次。 But the does not matter 但是没关系 在此处输入图片说明

In the end I want to get a plot which shall demonstrate the following: 最后,我想得到一个图,该图将演示以下内容:

在此处输入图片说明

I have actually no idea how to plot this: 我实际上不知道如何绘制此图:

I've tried: plot(data1$COLUMND,data1$COLUMNA,xlab = "COLUMND", ylab = "COLUMNA"); 我试过了: plot(data1$COLUMND,data1$COLUMNA,xlab = "COLUMND", ylab = "COLUMNA"); But the result is that the text in column A is replaced by a number. 但是结果是A列中的文本被数字替换。 So the axis get the label from 0-3 in this case. 因此,在这种情况下,轴从0-3获得标签。 I also tried to change the lable with the labels command. 我还尝试使用标签命令更改标签。 But this lead to the problem that the lables were in an aceding row. 但是,这导致了问题,即标签位于前排。 But the data in the column are not (in my example above they are, but not in my real data). 但是列中的数据不是(在上面的示例中是,但在我的真实数据中)。 Therefore R should replace 0 with the corresponding text from column A. For this I used the methods shown in Quick-R guide but they work not as desired and replaced the entries with null. 因此,R应该用A列中的相应文本替换0。为此,我使用了Quick-R指南中显示的方法,但是它们无法按预期运行,并用null代替了条目。

you have to do two steps. 您必须执行两个步骤。

1) Make a list of vectors. 1)列出向量。 Every vector is names after an unique element of column A and contains the corresponding values form column D. 每个向量都是在A列的唯一元素之后的名称,并包含D列的相应值。

2) Use the stripchart() function with this list. 2)在此列表中使用stripchart()函数。

My code approach: 我的代码方法:

## your data
data <- data.frame(A = c("AAA", "AAB", "AAC", "AAA", "AAE", "AAC"),
                   B = rep(12.3),
                   C = rep(20160729),
                   D = c(100,80,10,0,5,20))

## empty list to fill in the following loop
list <- list()
## get the values in column D for every unique value in column A
## an add it to the list
for (i in unique(data$A)) list[[i]] <- data$D[data$A == i]

## plot the list
stripchart(list,
           xlab = "Column D", ylab = "Column A",
           pch = 16, col = "red")

The result: 结果:

Stripchart 带状图

Have you tried using the axis function? 您是否尝试过使用axis功能?

First, note that "AAD" was not in the sample data that you provided. 首先,请注意,您提供的示例数据中没有“ AAD”。 We have to tell R about the values in Column A and how we want them to be ordered: 我们必须向R告知A列中的值以及如何对它们进行排序:

data1 <- data.frame(A=c('AAA', 'AAB', 'AAC', 'AAA', 'AAE', 'AAC'),
    D=c(100, 80, 10, 0, 5, 20))
data1$A <- factor(data1$A, levels=paste0('AA',LETTERS[1:5]))

Now we can plot. 现在我们可以绘图。 We tell R to leave out the Y-axis for now (using the yaxt argument); 我们告诉R现在yaxt使用Y轴(使用yaxt参数); we'll add them in manually later. 我们将在以后手动添加它们。

par(mar=c(6,6,4,2)) # Set margins for plot
plot(data1$D, data1$A, xlab = "Column D", ylab = "", yaxt="n", las=1)

Finally we add in the Y-axis labels, using the actual values instead of factor levels (ie the numbers). 最后,我们使用实际值代替因子水平(即数字)添加Y轴标签。

axis(2, at=1:length(levels(data1$A)), labels=levels(data1$A), las=2)
mtext("Column A", side=2, line=1, las=2, at=3.2)

输出量

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

相关问题 y 轴上的名称和 x 轴上的值的 R 图 - R plot with names on the y-axis and values on the x-axis R图,x轴和y轴接触 - R plot, x-axis and y-axis touching Plot 日期在 x 轴和 y 轴“时间序列”R - Plot dates on x-axis and y-axis 'timeseries' R 如何在具有一个x轴和两个y轴的图中将误差线缩放到右y轴的尺寸 - How to scale the errorbars to the dimensions of the right y-axis in a plot with one x-axis and two y-axis 如何在以年为x轴和y轴为计数的R中绘制线形图? - How to plot line graph in R with years as x-axis and y-axis as count? 如何使用 ggplot 在 x 轴上绘制日期,在 y 轴上绘制价格表和属性? - How can I plot date on x-axis and price list on y-axis and attribute by using ggplot? 如何按收集日期(x 轴)和其他因素(R)plot % 阳性病例(y 轴)? - How to plot % positive cases (y-axis) by collection date (x-axis) and by other factors (R)? 如何使用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? 如何在 x 轴上制作 R plot 的 SNP,在 y 轴上制作信念? - How to make an R plot of SNPs on the x-axis and beliefs on the y-axis? 如何在 ggplot2 中在 x 轴上绘制两个向量,在 y 轴上绘制另一个向量 - How do you plot two vectors on x-axis and another on y-axis in ggplot2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM