如何在R中制作线图?

时间:2012-02-20 00:58:01

标签: r plot line

我正在尝试使用R来创建一个excel类型的线图,其中我的x轴是文本(A,B,c..etc),y轴(可以是负数和正数)都是up和列。我想放弃一种红色和绿色。

如果有人能帮助我,我真的很感激。我在excel中绘制了这个,但是我的数据中有数千行,而excel并没有显示我的绘图中的所有文本点。

我的数据如下所示:

Name    UP  Downs
A   10  -3
B   2   -4
C   1   -1
D   4   -1
E   5   0
F   0   -1
G   6   -5
H   0   -1
I   7   -1
J   0   -1
K   0   -11
L   3   -1
M   0   -13
N   2   -1
O   0   -1
P   1   -1
Q   0   0
R   1   -1
S   0   0
T   12  -1

1 个答案:

答案 0 :(得分:1)

这可能不是最优雅的方式,但您可以使用plotpointsaxis axis?axis主要的一个,它解释了如何更改轴上的标签):?plot?points# make a data frame similar to yours mydf <- data.frame( Name=LETTERS, Up=sample.int(15,size=26,replace=T), Down=-sample.int(15,size=26,replace=T) )

首先制作一个类似于你的数据框,这样我才能证明......

# set up a plot: x axis goes from 1 to 26,
# y limit goes from -15 to 15 (picked manually, you can work yours out
#   programmatically)
# Disable plotting of axes (axes=FALSE)
# Put in some x and y labels and a plot title (see ?plot...)
plot(0,xlim=c(1,26),ylim=c(-15,15),type='n',
     axes=FALSE,                 # don't draw axis -- we'll put it in later.
     xlab='Name',ylab='Change',  # x and y labels
     main='Ups and Downs')       #,frame.plot=T -- try if you like. ?plot.default
# Plot the 'Up' column in green (see ?points)
points(Up~Name,mydf,col='green')
# Plot the 'Down' column in red
points(Down~Name,mydf,col='red')
# ***Draw the x axis, with labels being A-Z 
#  (type in 'LETTERS' to the prompt to see what they are)
# see also ?axis
axis(1,at=1:26,labels=LETTERS)
# Draw the y axis
axis(2)

现在情节。

?points

enter image description here

按照您的意愿调整它:?par?axis以及{{1}}在这方面特别有用。