简体   繁体   English

如何在R中绘制线图?

[英]How can i make a line plot in R?

I am trying to use R to make an excel kind of a line plot, where my x axis is text (A,B,c..etc) and the y axis(which can be both negative and positive) are up and down columns. 我正在尝试使用R制作一种excel的折线图,其中我的x轴是文本(A,B,c..etc),而y轴(可以是负数也可以是正数)在上下两列。 I want to give up a red color and down green. 我要放弃红色,放弃绿色。

I would really appreciate if anyone can help me regarding this. 如果有人可以帮助我,我将非常感谢。 I have plotted this in excel but i have thousands of rows in my data and excel doesnot show all the text point in my plot. 我已经在excel中对此进行了绘制,但是我的数据中有成千上万的行,而excel却无法在我的绘图中显示所有文本点。

My data looks like the following: 我的数据如下所示:

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

This is probably not the most elegant way to do it, but you can work it all out using with plot , points , and axis ( axis is the main one, it explains how you can change the labels on the axis): ?axis , ?plot , ?points . 这可能不是最优雅的方式来做到这一点,但你可以使用解决全部问题plotpointsaxisaxis是主要的一个,它解释了如何更改轴线上的标签): ?axis?plot?points

First make a data frame similar to yours so I can demonstrate... 首先制作一个与您相似的数据框,以便我进行演示...

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

Now plot. 现在绘图。

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

在此处输入图片说明

Tweak it as you wish: ?points and ?par and ?axis are particularly helpful in this respect. 根据需要进行调整: ?points以及?par?axis在这方面特别有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM