I have a problem where I need to import a two column (first column being the x-axis and the second being the y-axis) .txt-file into R and I need to do this so that R reads it from bottom to top.
Here is what I did so far:
data<-read.table("data.txt",skip=1910,nrow=132982)
plot(data,type="l")
After this, I have the desired plot, but I wish this to be reversed horizontally. What would be the most convenient way to do this?
I tried
datar<-rev(data)
after import but it reversed the columns by switching the x-values to y-axis and y-values to x-axis. I wish to reverse the columns so that the last value in both columns will be the first in their columns without the columns switching places with each other.
I think that most convenient would be to revert the file during import as the file has over 130 000 rows and it is very cumbersome to work with.
Thank you in advance!
I may be daft - but I don't see how order of reading x-y data would affect the position x and y coordinates.
maybe you want something like this:
data <- read.csv(text= "
x,y
1,1
2,2
3,4")
plot(data,xlim=c(3,1))
(x axis goes down from 3 left to 1 right)
Related
I have the following code to plot a large dataset (450k) in ggplot2
x<-ggplot()+
geom_point(data=data_Male,aes(x=a,y=b),color="Turquoise",position=position_jitter(w=0.2,h=1),alpha=0.1,size=.5,show.legend=TRUE)+
geom_point(data=data_Female,aes(x=a,y=b),color="#FF9999",position=position_jitter(w=0.2,h=1),alpha=0.1,size=.5,show.legend=TRUE)+
theme_bw()
x<-x+geom_smooth(data=data_Male,aes(x=a,y=b,alpha="Male"),method="lm",colour="Blue",linetype=1,se=T)+
geom_smooth(data=data_Female,aes(x=a,y=b,alpha="Female"),method="lm",colour="Dark Red",linetype=5,se=T)+
geom_smooth(data=data_All,aes(x=a,y=b,alpha="All"),method="lm",colour="Black",linetype=3,se=T)+
scale_fill_discrete(name="Key",labels=c("Female","Male","All"))+
scale_colour_discrete(name="Plot Colour",labels=c("Female","Male","All"))+
scale_alpha_manual(name="Key",
values=c(1,1,1),
breaks=c("Female","Male","All"),
guide=guide_legend(override.aes=list(linetype=c(5,1,3),name="Key",
shape=c(16,16,NA),
color=c("Dark Red","Blue","Black"),
fill=c("#FF9999","Turquoise",NA))))
How can I change the order in which points are plotted? I have seen answered questions here dealing with a single dataframe but I am working with several dataframes so I cannot re-order the rows or ask ggplot to plot by certain criteria from within the dataframe. You can see an example of the kind of problem that this causes in the attached picture: the Female points are plotted on top of the Male points. Ideally I would like to be able to plot all the points in a random order, so that one "cloud" of points is not plotted on top of the other, obscuring it (N.B. the image shown doesn't include the "All" line).
Any help would be appreciated. Thank you.
I belive this is not possible. The following should work though:
You'd have to paste the two data frames together to df. The new data frame will appear sorted by male and female.
You can then suffle the new data frame:
set.seed(42)
rows <- sample(nrow(df))
male_female_mixed <- df[rows, ]
Then you can plot male_female_mixed
I have a data frame (pLog) containing the number of reads per nucleotide for a chip-seq experiment done for a E. coli genome (4.6MB). I want to be able to plot on the X axis the chromosomal position and on the Y axis the number of reads. To make it easier, I binned the data in windows of 100bp. That makes the data frame of 46,259 rows and 2 columns. One column is named "position" and has a number representing a chromosomal position (1,101,201,....) and the other column is named "values" and contains the number of reads found on that bin e.g.(210,511,315,....). I have been using ggplot for all my analysis and I would like to use it for this plot, if possible.
I am trying for the graph to look something like this:
but I haven't been able to plot it.
This is how my data looks like
I tried
ggplot(pLog,aes(position))+
geom_histogram(binwidth=50)
ggsave(file.jpg)
And this is how it looks like :(
Many thanks!
You cannot use geom_histogram(), try geom_line:
pLog=data.frame(position=seq(1,100000,by=100),
value=rnbinom(10000,mu=100,size=20))
ggplot(pLog,aes(x=position,y=value))+geom_line(alpha=0.7,col="steelblue")
Most likely you need to play around to get the visualization you need
Hi all and apologies if this came up but I could not find 100% match answer to my question...
I have a csv file named zed made of 3 cols and 366 observation:
FTD_DAY, FTD_DATE, FTD imported in R.
FTD_DATE looks like a date (dd/mm/yyy) but R reads it as integer (typeof(zed$FTD_DATE) displays integer in GUID).
I then geom_point the data using:
x <-ggplot(data=zed, aes(x=FTD_DATE,y=FTD)) and the x axis looks ugly since cannot fit 366 dates making it unreadable.
Questions I have:
How can I order dates in x in ascending order?
How can I break coordinates showing only (say) 7 coords maintaining all 366 observations?
How can I align the dates in x axis vertically?
I tried scale_discrete(), scale_log() etc. but went nowhere or got somewhere in the wrong place... can someone please help and let me know what is the solution using ggplot?
I am not sure I can accomplish what I am trying to do with gnuplot.
I am not sure whether it would be better to plot my data as a colored surface plot or in the following way:
Firstly, here is an explanation of my data format.
The first column contains time values. The time typically does something like 0, 1, 2, 3, 4, 5, .... For each time, the row next to the time value contains the data I would like to plot. (This is the y-data on a 2D plot, the x-data is the x index (row index) of the data minus 1 for the first column which contains the time index.
I have many (~ 1000) time values.
So it looks something like this:
Sorry for awful color choice.
Essentially I would like to plot a line graph for each time value, and put these 2d plots side by side in a 3d plot.
Can I even do this with gnuplot?
I hope the question is clear, if not I can try and add more information.
plot 'data.txt' matrix using ($1-1):3:2 every 1::1 with linespoints palette
your Excel data are copied to data.txt
matrix mode plots lines series (it's that you want)
the first and the second parameter in using when plotting matrix are mode specifiers: 1:3 is "plot rows of matrix".
Citing #Cristoph's comment,
"With the matrix option, the column number is available as first
column, the row number as second column and the actual matrix value as
third column."
($1) would be equal to 1 here, but ($1 - 1) means "subtract unity from a column number" - this way x would start from 0 for each row.
every 1::1 - do not include the first column with time values into plot.
at last using ..:..:2 allows you to set different colors (equal to row number) for the palette option.
The answer is based on similar question and answer
I've cleaned up a larger dataframe to a simple table that looks something like this (note this is a small sample of a couple hundred rows):
Name<-c("Bob","Bob","Bob","Bob","Bob","Anne","Anne","Anne","Anne","Anne","Anne","Joe","Joe")
start_event <-c(0,266,352,354,553,0,36,192,206,458,997,1102,1198)
end_event <-c(27.5,296,354,402,561,27.5,71,203,217,515,1033,1109,1215)
duration <-c(27.5,30,2,48,8,27.5,35,11,11,57,36,7,17)
run<-c(1,2,3,4,5,1,2,3,4,5,6,1,2)
df<-data.frame(Name,run,start_event,end_event,duration)
My goal is to create a graph that has the names on the y-axis, the total event duration on the x-axis (the min. would be the start_event and the max would be the final end_event).
For each person, a bar would represent the duration of their activity, from start to end. There would be gaps with no bars for the times they were not active.
I've tried mashing together some code from another example (link below) using either geom_rect, geom_bar, and attempts with geom_line, but am having issues with discrete/continuous values.
For reference to help visually frame this, this answer provided for this Q produces a similar result I would like to achieve: https://stackoverflow.com/a/17130467
Dodging the bars/rectangles is not needed, stacked in a single horizontal line is preferred.
Thank you in advance for any guidance/help!
You could use geom_segment :
ggplot(df,aes(y=Name,yend=Name,x=start_event,xend=end_event,color=Name)) + geom_segment(size=6)