2012-02-20 55 views
0

我正在嘗試使用R來創建一個excel類型的線圖,其中我的x軸是文本(A,B,c..etc)和y軸(可以是負值和正值)下欄。我想放棄紅色和綠色。如何在R中製作線條圖?

如果有人能幫我解決這個問題,我將非常感激。我在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 
+5

[文檔是你的朋友。閱讀。愛它。使用它。](http://stat.ethz.ch/R-manual/R-devel/library/graphics/html/plot.html) – 2012-02-20 01:02:02

+0

我已閱讀此頁...問題是我的xaxis是文本。 .. – RnD 2012-02-20 01:25:17

+0

@ mathematical.coffe .i將一個csv文件導入到「nba」變量,然後輸入plot(nba [,1],nba [,2])..這不會給我我想要的.. – RnD 2012-02-20 01:52:52

回答

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) 

enter image description here

調整它,你想:?points?par?axis在這方面特別有幫助。

+0

謝謝你的幫助......這個效果很好......我遇到的一個問題是我的xaxis有2000個數據點,那些只是表現爲一個黑暗的樂隊。有沒有解決這個..?我認爲,即使我做垂直文本,並減少測試的大小...仍然坐不好...任何建議,高度讚賞和感謝您的幫助... – RnD 2012-02-20 02:46:53

+2

@maziz這聽起來像一個單獨的問題給我。 StackOverflow不適用於來回式幫助臺樣式支持。我們鼓勵您在某個時候提出一個單獨的具體問題,這個問題描述了(a)您迄今爲止所嘗試的內容,以及(b)爲什麼/如何以您想要的方式工作。 – joran 2012-02-20 02:51:07