如何根据 R 中的列名绘制一行

How to plot a row against the column names in R

我想用每行不同的线绘制一个图表,并将列名分配给 X 轴。为了完成,我还想让每一行都与另一行不同,并带有 reader.

的图例

提前致谢。

我的数据:

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 即列序列,然后将 x 标签更改为 axis

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))