简体   繁体   English

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

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

I would like to plot a graph with different lines for each row, and that the column names are assigned to the X axis.我想为每一行绘制一个具有不同线条的图形,并且将列名分配给 X 轴。 For finishing, I would also like to make every line different from the other with a legend for the reader.为了完成,我还想使每一行都与另一行不同,并为读者提供一个图例。

Thank you in advance.先感谢您。

My data:我的数据:

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

Code:代码:

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

Thanks in advance.提前致谢。

We can specify the 'x' as numeric ie sequence of columns and then change the x labels with axis我们可以将“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))

在此处输入图片说明


If we need to plot lines for each row, use matplot如果我们需要为每一行绘制线条,请使用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))

在此处输入图片说明

data数据

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