[英]Plot with labels on the x axis
我想得到10個不同分子的強度值的直線圖,其中x軸為分子名,y軸為強度值。
我試過了:
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)
但它返回此錯誤消息?
plot.window(...)中的錯誤:需要有限'xlim'值另外:警告消息:1:在xy.coords(x,y,xlabel,ylabel,log):由強制2引入的NA:在min( x):min沒有非缺失的參數; 返回Inf 3:在max(x)中:max沒有非缺失參數; 返回-Inf
因為你的x變量是離散的,所以你需要做一點不同的事情:
plot(seq_along(x),intensity,type = "l",axes = FALSE)
axis(side = 2)
axis(side = 1,at = seq_along(x),labels = x)
我們的想法是使用x軸的數值創建繪圖,然后只需添加特定標簽。
如果您錯過了圖表周圍的完整框,則可以添加對box()
的調用。
你可以通過將“x”作為一個因素來做到這一點。
xFact<-factor(x, levels=x)
plot.default(xFact,intensity)
編輯將plot
改為plot.default
。 正如喬蘭所指出的,如果你只是使用plot
你會得到一個箱線圖。
重新編輯:要使x軸顯示正確的標簽,請使用:
plot.default(xFact,intensity,type="p",xaxt="n")
axis(side=1, at=xFact,labels=x)
使用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.