简体   繁体   English

在x轴上绘制标签

[英]Plot with labels on the x axis

I want to have a straight line plot of intensity values for 10 different molecules with molecule name on the x axis and intensity value on the y axis. 我想得到10个不同分子的强度值的直线图,其中x轴为分子名,y轴为强度值。

I tried: 我试过了:

x = c("Mol 1","Mol 2","Mol 3","Mol 4","Mol 5","Mol 6","Mol 7","Mol 8","Mol 9","Mol 10")
intensity = c(428,409,388,378,373,140,137,138,139,144)
plot(x,intensity)

But it returned this error message? 但它返回此错误消息?

Error in plot.window(...) : need finite 'xlim' values In addition: Warning messages: 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion 2: In min(x) : no non-missing arguments to min; plot.window(...)中的错误:需要有限'xlim'值另外:警告消息:1:在xy.coords(x,y,xlabel,ylabel,log):由强制2引入的NA:在min( x):min没有非缺失的参数; returning Inf 3: In max(x) : no non-missing arguments to max; 返回Inf 3:在max(x)中:max没有非缺失参数; returning -Inf 返回-Inf

Because your x variable is discrete, you need to do this a little differently: 因为你的x变量是离散的,所以你需要做一点不同的事情:

plot(seq_along(x),intensity,type = "l",axes = FALSE)
axis(side = 2)
axis(side = 1,at = seq_along(x),labels = x)

The idea being that you create the plot with numerical values for the x axis, and then simply add your particular labels. 我们的想法是使用x轴的数值创建绘图,然后只需添加特定标签。

You can add a call to box() if you miss the full box around the plot. 如果您错过了图表周围的完整框,则可以添加对box()的调用。

You can do this by making your "x" a factor. 你可以通过将“x”作为一个因素来做到这一点。

xFact<-factor(x, levels=x)
plot.default(xFact,intensity)

Edited to change plot to plot.default . 编辑将plot改为plot.default As Joran points out, if you just use plot you get a boxplot. 正如乔兰所指出的,如果你只是使用plot你会得到一个箱线图。

Re-edit: To get the x axis to display the proper labels use: 重新编辑:要使x轴显示正确的标签,请使用:

plot.default(xFact,intensity,type="p",xaxt="n")
axis(side=1, at=xFact,labels=x)

Using ggplot: 使用ggplot:

library(ggplot2)
df <- data.frame(id=1:length(x),x,intensity)
ggplot(df)+                               # set default dataset
  geom_point(aes(x=id,y=intensity))+      # plot the points
  scale_x_discrete(labels=x)+             # label the x-axis ticks
  labs(x="Molecule")+                     # label the x axis
  theme(axis.text.x=element_text(angle=90,vjust=.2,hjust=1))  # rotate and align tick labels

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM