How can i make a line plot in R? - 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. 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.
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.
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.

Related

gnuplot plot scientific notation (exponential value on x-axis)

I'm new using gnuplot, today I learned a lot on stackoverflow for a problem that I had with a plot, but now for my research I need to do another one like the following image:
The data are of the same tipe, I collected them and isolate only the column that I need in a file.dat, and a sample is the following:
5.38e-51 1
2.75e-81 1
5.3e-67 1
8.71e-170 4
3.62e-59 3
2.98e-52 2
3.2e-31 1
2.98e-54 2
3.85e-29 2
5.38e-57 1
3.2e-33 2
2.75e-88 1
3.2e-34 1
9.89e-37 1
5.38e-59 2
3.2e-35 2
1.68e-168 1
1.81e-101 1
9.89e-39 1
2.98e-59 2
3.2e-39 1
1.07e-110 3
1.07e-111 2
1.81e-107 2
2.82e-40 4
2.6e-108 1
1.07e-115 1
My problem is that I would like on the x-axis that my x is the exponent, as 1E-x.
I tried using set format set format x '%.0f'.sprintf('e%d') but it doesn't work.
How can I do that?
thank you.
If you want to plot values which span many order of magnitudes you either plot it on logarithmic scale
or take log10() of your values and plot them in linear scale.
From your description without code and details, I'm guessing that you have
the data you've shown and want to create a histogram and display the negative log10 value on the x-axis.
NB: In your earlier data you had also 0.0, which will mess-up the smooth freq option together with the logarithmic bins. Note that set datafile missing "0.0" will only exclude 0.0 as text, i.e. 0, 0.00, or 0e-10 will not be excluded. So, make sure you don't have zeros in your data, this will not work with logarithmic scale.
Code:
### plot histogram with logarithmic bins
reset session
$Data <<EOD
5.38e-51 1
2.75e-81 1
5.3e-67 1
8.71e-170 4
3.62e-59 3
2.98e-52 2
3.2e-31 1
2.98e-54 2
3.85e-29 2
5.38e-57 1
3.2e-33 2
2.75e-88 1
3.2e-34 1
9.89e-37 1
5.38e-59 2
3.2e-35 2
1.68e-168 1
1.81e-101 1
9.89e-39 1
2.98e-59 2
3.2e-39 1
1.07e-110 3
1.07e-111 2
1.81e-107 2
2.82e-40 4
2.6e-108 1
1.07e-115 1
EOD
set xlabel "-log_{10}(x)"
set xrange[-15:200]
set xtics 10 out
set ylabel "Sequences"
set yrange [0:]
set key noautotitle
# Histogram with logarithmic bins
BinWidth = 10
Bin(x) = floor(-log10(x)/BinWidth)*BinWidth + BinWidth*0.5
set boxwidth BinWidth
set style fill solid 0.3
set offset 1,1,1,0
set grid x,y
set datafile missing "0.0" # exclude zero, otherwise it will mess up "smooth freq"
plot $Data u (Bin($1)):2 smooth freq with boxes lc "red"
### end of code
Result:

Using gnuplot to create data files

Hey guys I am currently working with gnuplot.
I have this .csv file which I have been using to plot some formulas
(eg plot "filename.csv" u 0:day($0) = $0 ). The plots worked out; however, I was wondering if there was a way within gnuplot to save the output of my formulas as a data file too.
Please check the manual or in the gnuplot console type help table.
Code:
### save data as text
reset session
f(x) = x
g(x) = x**2
h(x) = x**3
set xrange[-5:5]
set samples 11
plot f(x) w lp, g(x) w lp, h(x) w lp
set table "myOutput.dat"
plot '+' u 1:(f($1)):(g($1)):(h($1)) w table
unset table
### end of code
Edit:
Actually, to be more flexible with data separators (e.g. comma or whatever) in the output file, you could change the plot ... w table command to something like the line below. However, I guess, gnuplot will always add a leading space " " and a trailing TAB \t for each line. But maybe this can also be changed.
plot '+' u (sprintf("%g,%g,%g,%g",$1,f($1),g($1),h($1))) w table
Result:
And myOutput.dat:
-5 -5 25 -125
-4 -4 16 -64
-3 -3 9 -27
-2 -2 4 -8
-1 -1 1 -1
0 0 0 0
1 1 1 1
2 2 4 8
3 3 9 27
4 4 16 64
5 5 25 125
Addition: (creating data in a loop)
With set print you are probably the most flexible, no leading space and trailing TAB.
Check the manual or in gnuplot console type help set print.
Code:
### save data as text, independent of range and samples
reset session
f(x) = x
g(x) = x**2
h(x) = x**3
set print "myOutput.dat"
do for [i=-5:5] {
# loop index only takes integers, multiply i with some factor if necessary
print sprintf("%g,%g,%g,%g",i,f(i),g(i),h(i))
}
set print
### end of code

Draw Lines from the origin of a coordinate system to specific points

I have data that have 3 points to display in a 3D coordinate system. I want a line between each point and the coordinate origin (0 0 0). How can I draw these lines not connecting the points with each other but only with the origin?
I'm not very into gnuplot yet: I'm using the following code to display my data:
splot "C:/a/Drehmatrizenxyz.txt" with lines
But this only connects the points, which is exactly what I do not want.
Thank you.
Data file contains six values for each line. From origin is (0 0 0 x y z)
Example - plot vectors <-1,2,-4>, <-2,0,1> & <2,9,2>
file.dat:
0 0 0 -1 2 -4
0 0 0 -2 0 1
0 0 0 2 9 2
gnuplot> splot 'file.dat' with vectors
vectors
In case this is still of interest...
Please check the gnuplot homepage and basic gnuplot tutorials and in gnuplot console help vectors or in general help <keyword>.
Code:
### plot with vectors from origin
reset session
$Data <<EOD
1 2 3
4 5 6
7 8 1
EOD
set view equal xyz
set view 56,48, 1.3
set xyplane relative 0
splot $Data u (0):(0):(0):1:2:3 w vectors
### end of code
Result:

divide not rectangle plot into subplots within spatstat package in R

I have data that contains information about sub-plots with different numbers and their corresponding species types (more than 3 species within each subplot). Every species have X & Y coordinates.
> df
subplot species X Y
1 1 Apiaceae 268675 4487472
2 1 Ceyperaceae 268672 4487470
3 1 Vitaceae 268669 4487469
4 2 Ceyperaceae 268665 4487466
5 2 Apiaceae 268662 4487453
6 2 Magnoliaceae 268664 4487453
7 3 Magnoliaceae 268664 4487453
8 3 Apiaceae 268664 4487456
9 3 Vitaceae 268664 4487458
with these data, I have created ppp for the points of each subplot within a window of general plot (big).
grp <- factor(data$subplot)
win <- ripras(data$X, data$Y)
p.p <- ppp(data$X, data$Y, window = window, marks = grp)
Now I want to divide a plot into equal 3 x 3 sub-plots because there are 9 subplots. The genetal plot is not rectangular looks similar to rombo shape when I plot.
I could use quadrats() funcion as below but it has divided my plot into unequal subplots. Some are quadrat, others are traingle etc which I don't want. I want all the subplots to be equal sized quadrats (divide it by lines that paralel to each sides). Can you anyone guide me for this?
divide <-quadrats(p.patt,3,3)
plot(divide)
Thank you!
Could you break up the plot canvas into 3x3, then run each plot?
> par(mfrow=c(3,3))
> # run code for plot 1
> # run code for plot 2
...
> # run code for plot 9
To return back to one plot on the canvas type
> par(mfrow=c(1,1))
This is a question about the spatstat package.
You can use the function quantess to divide the window into tiles of equal area. If you want the tile boundaries to be vertical lines, and you want 7 tiles, use
B <- quantess(Window(p.patt), "x", 7)
where p.patt is your point pattern.

Visualize igraph degree distribution with ggplot2

I want to visualise the degree distribution of an igraph object with ggplot2. Because ggplot2 doesn't take a the simple numeric vector generated by degree() I convert it to a frequency table. Then I pass it to ggplot(). Still I get: geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic? I can't set the table column degree to factors since I need to plot it also on a log scale.
library(igraph)
library(ggplot2)
g <- ba.game(20)
degree <- degree(g, V(g), mode="in")
degree
# [1] 6 2 7 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0
degree <- data.frame(table(degree))
degree
# degree Freq
# 1 0 13
# 2 1 4
# 3 2 1
# 4 6 1
# 5 7 1
ggplot(degree, aes(x=degree, y=Freq)) +
geom_line()
# geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
The problem is that you have turned degree$degree into a factor by using table. Two things to fix this:
make it a factor with all possible values (up to the largest degree) so that you don't miss the zeros.
convert the labels back to numbers before plotting
Implementing those (I used degree.df instead of overwriting degree to keep the different steps distinct):
degree.df <- data.frame(table(degree=factor(degree, levels=seq_len(max(degree)))))
degree.df$degree <- as.numeric(as.character(degree.df$degree))
Then the plotting code is what you had:
ggplot(degree.df, aes(x=degree, y=Freq)) +
geom_line()

Resources