I have tried to find a way to plot a graph where the x-axis wraps at a specified position and then continues at 0 to the right of the max value.
I am attaching an example where the x-axis wraps at 720, e.g. 720 == 0.
It does not matter which tool to use as long it is easy to install on Linux (Ubuntu)
I am trying to assign a x-label column to my data serie where the x axis is in column B and data in column D and x-labels in column C.
but that does not work for me, e.g. the x-axis in above example spans from 685 to 735 instead of 685 to 720 then to 15
So using line diagram instead of scatter as #pnuts did solved my problem.
Instead of x values for the x-axis apply the remainder after x has been divided by 720:
Related
I am new to image processing. I want to detect a specific line in this image, which is the horizontal line in the middle of it. I am wondering how to approach it. This is the map, and it has already been simplified to the edge map.
Here's one method.
Let's start by reading in your image. We'll use the png library for this, so if you don't have it installed, you'll want to install.packages("png") first.
library(png)
img_link <- "https://i.stack.imgur.com/PygvJ.png"
img <- readPNG(readBin(img_link, "raw", 1e6))
The readPNG function extracts the image as an array. In this case, the array is 360 by 371 by 4:
dim(img)
#> [1] 360 371 4
This means the image is 360 by 371 pixels and contains four channels: red, blue, green and alpha (transparency).
We can plot this array at any time by calling:
plot(as.raster(img))
To find a horizontal red line, all we need to do is look at the row sums of the pixel values in the green channel, which will drop sharply at the target row.
sum_green <- apply(img[,,2], 1, sum)
We can plot this vector to ensure there is a row with a large drop-off in one of the rows, as we would expect.
plot(sum_green, type = "l")
To get this row, we just find the minimum row sum:
target_row <- which.min(sum_green)
target_row
#> [1] 183
So our red line is on the 183rd row. To prove this, let's make the 183rd row of our image black and draw it again:
img[target_row, , 1:3] <- 0
plot(as.raster(img))
This looks correct.
I am learning graphical analysis using R. Here is the code, which I can not understand.
barplotVS <- barplot(table(mtcarsData$vs), xlab="Type of engine")
text(barplotVS,table(mtcarsData$vs)/2,table(mtcarsData$vs),cex=1.25)
The output is like below. I can not understand the function of text(), I googled the text() function, which shows that the parameter of text(x,y) is numeric vectors of coordinates where the text labels should be written. Can anyone tell me what is barplotVS,table(mtcarsData$vs)/2,table(mtcarsData$vs),cex=1.25 in my code.
barplotVS <- barplot(table(mtcarsData$vs), xlab="Type of engine")
print(barplotVS)
outputs:
[,1]
[1,] 0.7
[2,] 1.9
These are the positions where the center of the bars in the barplot are on the x axis.
print(table(mtcarsData$vs))
outputs:
0 1
18 14
the numbers below are the occurrences of each value that is present in mtcarsData$vs and the numbers above are the actual value that is counted.
When you run the function:
text(barplotVS,table(mtcarsData$vs)/2,table(mtcarsData$vs),cex=1.25)
the first value will be the x positions where to put the labels (i.e. 0.7 and 1.9), the second parameter will be the y positions set in this case to total counts divided by two (i.e. 9 and 7) meaning to put the labels halfway in the bars, the third will be the labels (i.e. 18 and 14) and finally cex is a value that allows to change the size of the font.
Anyway R has in general a good documentation that you can call by using the ? operator (as suggested in the comments). In order to understand try to run the code and check what each variable contains with print or str functions. If you use a IDE (e.g. RStudio) have the content of the variables in a graphical panel so you don't event need to print.
I’m trying to obtain the proportions of individuals that that shares certain DNA sequences between two given points. And I want to use a specific sliding window. In order to show the problem I create this example. First I create a data frame with four columns.
x<-c(rep("sc256",times=2000),rep("sc784",times=2000))
pos1<-round(runif(2000,100,5000),digits=0)
pos2<-round(runif(2000,100,5000),digits=0)
y3<-rep(c(2,1),times=2000)
M1<-data.frame(x,pos1,pos2,y3)
colnames(M1)=c("iid","pos1","pos2","chr")
I also create a function to obtain the proportion of individuals that have sequences in a particular interval.
roh_island<-function(pop,chr,p1,p2){
a<-pop[pop$chr==chr,]
island<-subset(a,pos1>=p1 & pos2<=p2)
n<-nrow(island)/length(M1$iid)
return(n)
}
roh_island(M1,1,345,700)
Now I want to transform this interval into a sliding window of size 10 that moves between values 0 and 7000. So this window will take positions [0,10);(10,20),…,(6990,7000]. I also need that the new function with the slide window stores all the windows and proportion of individuals in each in a data frame to afterwards plot it. I try some solutions that I have found regarding sliding windows I saw but I could not make them work. Thanks
This code will slide p1 from 0 to 6990 in steps of 10 while p2 slides from 10 to 7000 in steps of 10:
output = apply(data.frame(seq(0,6990,10), seq(10,7000,10)), MARGIN=1,
function(x,y,z,a) roh_island(M1, 1, x[1], x[2]))
plot(output, col="blue")
grid(5, 5)
In GNUPLOT, I would like to plot 5 values on a single bar chart, separated with some spacing in between. If I have data formatted as such:
3342336, 3375103, 7110653, 32770, 0
where those 5 values are the y-values, how can I specify the x-values myself for where they should belong?
For example, I would like my bar chart to have each entry be of length 1,
so I plot y-value 3342336 at x-value 1,
y-value 3375103 at x-value 3,
y-value 7110653 at x-value 5,
y-value 32770 at x-value 7,
and y-value 0 at x-value 9.
I would appreciate any example code that can achieve this. Thanks.
If your data is in one row as shown, you can achieve this by using the plot for syntax looping over the column index, and calculating the x value from that index. We can grab the column by using the column function which retrieves the specified column number.
set boxwidth 1
set datafile separator comma # only if data is comma separated
plot for [i=1:5] (2*i-1):(column(i)) with boxes
If we need to ensure the same line type is used each time, we can explicitly state it in the plot command.
plot for [i=1:5] (2*i-1):(column(i)) with boxes lt 1
Additionally, if a key is to be generated, and we don't wish each plot statement to generate one, we can test for and only give a nonempty title on the first iteration (an empty title is treated the same as no title).
plot for [i=1:5] (2*i-1):(column(i)) with boxes lt 1 title (i==1)?"Title":""
If your data is separated into rows as is the normal format, this can be obtained a different way.
Gnuplot has several pseuduocolumns (see help pseudocolumns for details). In your case, column 0 is of interest. Column 0 gives the line number of the data starting at 0. Thus to get sequential odd numbers like that, you can use 2*$0+1.
For example, if your data (stored in datafile.txt) looks like
3342336
3375103
7110653
32770
0
and you wish to plot boxes of length 1 at those values, you can do
set boxwidth 1
plot "datafile.txt" u (2*$0+1):1 with boxes
Is there a way to get quantmod to draw a square line chart?
I've tried modifying my time series so that each data point is replicated one second before the next datapoint (hoping this would approximate a square line), but quantmod seems to data on the x axis sequentially & evenly spaces without regard to the actually values of x (i.e. the horizontal space between one point an the next is the same whether the delta-T is 1 second or 1 minute).
I suppose I could convert my timeseries from a sparse to a dense one (one entry per second instead of one entry per change in value), but this seems very kludgy and should be unnecessary.
I'm constructing my time series thus:
library(quantmod)
myNumericVector <- c(3,7,2,9,4)
myDateTimeStrings <- paste("2011-10-31", c("5:26:00", "5:26:10", "5:26:40", "5:26:50", "5:27:00"))
myXts <- xts(myNumericVector, order.by=as.POSIXct(myDateTimeStrings))
And drawing the chart like so:
chartSeries(myXts, type="line", show.grid="true", theme=chartTheme("black"))
To illustrate what I have vs. what I want, the result looks like the blue line below but I'd like something more like the green:
Also, for the curious, here is the code that replicates points in the time series such that the gap between one value and the next are as small as possible:
mySquareDateTimes <- rep(as.POSIXct(myDateTimeStrings),2)[-1]
mySquareDateTimes[seq(2,8,by=2)] <- mySquareDateTimes[seq(2,8,by=2)] - 1
mySquareXts <- xts(rep(myNumericVector,each=2)[-10], order.by=mySquareDateTimes)
chartSeries(mySquareXts, type="line", show.grid="true", theme=chartTheme("black"))
The results are less than ideal.
You want a line.type of "step":
chartSeries(myXts, line.type="s")
See ?plot, specifically "type" under ... in the Arguments section (you may want "S" instead of "s").