[英]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: 结果:
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.