簡體   English   中英

如何根據R中的列名繪制一行

[英]How to plot a row against the column names in R

我想為每一行繪制一個具有不同線條的圖形,並且將列名分配給 X 軸。 為了完成,我還想使每一行都與另一行不同,並為讀者提供一個圖例。

先感謝您。

我的數據:

Average 2003-2005 Average 2006-2008 Average 2009-2010 Average 2011-2013 Average 2014-2016
         31.48489          32.53664          30.41938          30.53870          31.15550   
         18.78799           17.78141         17.58791          17.03071          17.25654   
        107.46615          107.71512        109.55090         110.31438         109.66492   

> str(Table_1_2003_2018_All)
'data.frame':   3 obs. of  6 variables:
 $ Average 2003-2005: num  31.5 18.8 107.5
 $ Average 2006-2008: num  32.5 17.8 107.7
 $ Average 2009-2010: num  30.4 17.6 109.6
 $ Average 2011-2013: num  30.5 17 110.3
 $ Average 2014-2016: num  31.2 17.3 109.7
 $ Average 2017-2018: num  31.8 16.8 109.8

代碼:

# Plot 1

colnames(Table_1_2003_2018_All) <- c("2003-2005","2006-2008","2009-2010","2011-2013","2014-2016","2017-2018")

    plot(seq_along(Table_1_2003_2018_All), 
         Table_1_2003_2018_All[1,], type="l", xaxt = 'n',xlab = 'Time Periods', ylab = 'Average',
         main = "MARKET WORK", ylim = c(30,35)
         )

    axis(1, at = 1:6, colnames(Table_1_2003_2018_All))

提前致謝。

我們可以將“x”指定為numeric即列序列,然后使用axis更改 x 標簽

plot(seq_along(Table_1_2003_2018_All), 
    Table_1_2003_2018_All[1,], type="l", xaxt = 'n', 
      xlab = 'colnames', ylab = 'first row')
axis(1, at = 1:5, colnames(Table_1_2003_2018_All))

在此處輸入圖片說明


如果我們需要為每一行繪制線條,請使用matplot

matplot(t(Table_1_2003_2018_All), type = 'l', xaxt = 'n')
legend("top", legend = seq_len(nrow(Table_1_2003_2018_All)),
   col= seq_len(nrow(Table_1_2003_2018_All)),cex=0.8,
   fill=seq_len(nrow(Table_1_2003_2018_All)))
axis(1, at = 1:5, colnames(Table_1_2003_2018_All))

在此處輸入圖片說明

數據

Table_1_2003_2018_All <- structure(list(Average2003.2005 = c(31.48489, 18.78799, 107.46615
), Average2006.2008 = c(32.53664, 17.78141, 107.71512), Average2009.2010 = c(30.41938, 
17.58791, 109.5509), Average2011.2013 = c(30.5387, 17.03071, 
110.31438), Average2014.2016 = c(31.1555, 17.25654, 109.66492
)), class = "data.frame", row.names = c(NA, -3L))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM