R - Changing the Magnitude of the x-axis with ggplot2 - r

I am student self-learning R. I have a simple question in relation to ggplot2.
Currently, I have plotted a graph with the following x-axis.
36000 36200 36400 36600 36800
I would like to change the magnitude of it to...
36.0 36.2 36.4 36.6 36.8
without changing the graph.
How would I accomplish this?

Related

Manage Circles size in plot using symbols

I am using symbols function in r to draw cycles in a map, which has been imported as a plot.
According to the function Cycles radius are scaled basted on the max value of the data set.
I am plotting the same map for different time periods (different data set) and i want the maps to be comparable, meaning that the circle radius refers to the same values in all different maps. Is there a way that I can manage circle scaling?
Thanks
This is my code
#for the first map 2010
plot(my_map)
symbols(data2010$Lon, data2010$Lat, circles= data2010$number, inches=0.25,add=T)
#then the map for 2011
plot(my_map)
symbols(data2011$Lon, data2011$Lat, circles= data2011$number, inches=0.25,add=T)
The manual page suggests that setting inches=FALSE will accomplish what you want. Since you did not provide a sample of your data, we have to use data already available. This data set is used in the Examples on the manual page for the symbols() function:
data(trees)
str(trees)
# 'data.frame': 31 obs. of 3 variables:
# $ Girth : num 8.3 8.6 8.8 10.5 10.7 10.8 11 11 11.1 11.2 ...
# $ Height: num 70 65 63 72 81 83 66 75 80 75 ...
# $ Volume: num 10.3 10.3 10.2 16.4 18.8 19.7 15.6 18.2 22.6 19.9 ...
Since we only have one sample, we can plot the the symbols with and without the 31th row which is the largest.
with(trees, symbols(Height, Volume, circles = Girth/24, inches = FALSE))
Now add the data without row 31:
with(trees[-31, ], symbols(Height, Volume, circles = Girth/24, fg="red", inches = FALSE, add=TRUE))
We can tell that the scaling is the same because the red circles match the black circles even though the largest girth is missing from the second plot. For this to work you will have to specify the same values for xlim= and ylim= in each plot.
Run this code again replacing inches=FALSE with inches=.5 to see the difference.

Find centre location coordinates in R - geospatial analysis

I am trying to find the centre coordinates of high-density areas in R.
The dataset I have has about 1.5million rows and looks like this (dummy data)
LATITUDE LONGITUDE val
1 35.83111 -90.64639 359.1
2 42.40630 -90.31810 74.5
3 40.07806 -83.07806 115.4
4 40.53210 -90.14730 112.0
5 42.76310 -84.76220 118.4
6 39.29750 -87.97460 134.4 ...
...
After plotting it using ggmap and ggplot using the command
ggmap(UK_Map) +
geom_density2d(data=processedSubsetData,aes(x=processedSubsetData$Longitude,y=processedSubsetData$Latitude), bins=5) +
stat_density2d(data=processedSubsetData,aes(x=processedSubsetData$Longitude,y=processedSubsetData$Latitude,fill=..level.., alpha=..level..), geom='polygon')
I have the visualization which looks like below image.
As you can see from the image, there some high-density areas. I need to find the local centre coordinates of these high-density areas in the map.
I have tried calculating distance between the points and also rounding the coordinates to group them. But I am not able to make it work and is stuck.
Thanks

Plot from two files with errorbars for Gnuplot

I am thinking which way to do the addition of errorbars better by thinking the format of data.
The standard way of adding errors bars is discussed here, for instance.
My original data is in ranges
Model Decreasing Constant Increasing
2025 73-78 80-85 87-92
2035 63-68 80-85 97-107
2050 42-57 75-90 104.5-119.5
where the values are ranges.
I cannot plot directly in Gnuplot so I have to split it to averages and to error values in two files:
Averages:
Model Decreasing Constant Increasing
2025 75.5 82.5 89.5
2035 65.5 82.5 102
2050 49.5 82.5 112
and error configuration in ybar
Model Decreasing Constant Increasing
2025 2.5 2.5 2.5
2035 2.5 2.5 5
2050 7.5 7.5 7.5
I normally plot data like this as a one file
plot for [i=2:4] 'data.dat' using 1:i w linespoints
but now I should go through two files at the same time while doing the plot.
The normal syntax of plotting errorbars is
plot 'data' using 1:2:0:($1+$3):4:5 with yerrorlines
and manual here.
How can you plot from two files with errorbars in Gnuplot?
Feel free to propose if you know better way to do the addition of these errorbars in gnuplot.
Output to Cristoph's answer
where error bars missing in the first and third points.
Gnuplot 5 supports that you specify several characters as data file separators.
So, if you are sure you'll never get negative values (which I hopen given the format of your data), then you can use your original data file and set both white space and hyphen as datafile separator:
set datafile separator " -"
plot for [i=2:6:2] "data" using 1:(0.5*(column(i)+column(i+1))):(0.5*(column(i+1)-column(i))) with yerrorlines
First of all, I wonder about your columns used for plotting with yerrorlines. If your first data for 2025 is 75.5+/-2.5, you usually plot it with
plot "datafile" using <xcolum>:<ycolum>:<yerrorcolumn>
Your six columns are for the case of xy errorbars and specify the point itself and the lower and upper absolute values in x and y. But may be you are just doing it as you need it...
Now back to your question:
Gnuplot can not handle data from two files simultaneously, i.e. it can not take xy-values from one file and y-errors from another.
If you're running linux, the command line tool join can help.
Your averages stored in file A and the errors in file B, join A B will concatenate lines with the same value in the first colum like this:
2025 75.5 82.5 89.5 2.5 2.5 2.5
So,
plot "<join A B" using 1:2:5 with yerrorlines
should do the job. ("<join A B" will call the join command in the background and read its output like a data file)

plotting degrees in IDL

I'm using IDL 8.2
I have a list of positions (RA and Dec) of stars and i want to plot them on a figure, eg.
37.9 ~ 37 54' 0"
37.7 ~ 37 42' 0"
I read in the positions (degrees) in as strings and extract the degrees, minutes and seconds into separate arrays. These are then used to convert the values to decimal degrees for plotting here.
I would like to also have the alternate axis labelled with degrees. i.e.
37.9 ~ 37 54' 0"
37.6 ~ 37 42' 0"
Is there a way to do this other than using something like power point to do it?
Also is there a better way, than having the axis scaled the same, to force the plot to be a square plot using the plot procedure?
A good solution was posted here.
https://groups.google.com/forum/#!starred/comp.lang.idl-pvwave/EsbGiqZnhRw
Effectively, writing a function to perform write user defined tick marks.

R barplot label size of each sample

I would like to label each of the boxes in a barplot by their size(i.e number of observations in dataframe which are in the group).
e.g If the first variable has 3 levels and the second variable has 4 levels, I would like 12 labels.
(Also, is it possible to control the size or position of these labels)
Thank you for any help.
Here's one way to do it, using the data VADeaths as an example (it will be in your R workspace by default, or if not, use library(datasets)).
bar <- barplot(VADeaths)
text(rep(bar,each=nrow(VADeaths)), as.vector(apply(VADeaths,2,cumsum)),
labels=as.vector(apply(VADeaths,2,cumsum)),pos=3)
It looks like this:
To modify the size of the font you can use text(...,cex=2) to make things twice the size they were, e.g.
Now, let's explain this code so you know how to do it yourself!
First, let's look at VADeaths: it's a tally of deaths in each age group by category:
> VADeaths
Rural Male Rural Female Urban Male Urban Female
50-54 11.7 8.7 15.4 8.4
55-59 18.1 11.7 24.3 13.6
60-64 26.9 20.3 37.0 19.3
65-69 41.0 30.9 54.6 35.1
70-74 66.0 54.3 71.1 50.0
Now, to do the text on the barplot, we basically draw the barplot, and then draw the text on top using R command text (see ?text).
text requires x,y coordinates and corresponding pieces of text to draw on the bar plot. We will give it the coordinates of each line in the bar plot to draw the text on.
To do this, see the "Value" section ?barplot. This function not only plots your bar plot, but returns the x coordinate of each bar. score!
> bar <- barplot(VADeaths)
> bar
[1] 0.7 1.9 3.1 4.3
Now all we need is y coordinates to go with our x coordinates.
Well, a stacked bar plot just tallies up the frequencies in VADeaths as you go along.
For example, in the 'Rural Male' group, the first line is drawn at 11.7, and the second is drawn at 11.7 + 18.1 = 29.8, the third at 11.7 + 18.1 + 26.9 = 56.7, and so on (see the values in VADeaths).
So, our y coordinates need to be cumulative sums going down the columns.
To calculate these for each column, we can use cumsum. For example
> cumsum(c(1,2,3,4,5))
[1] 1 3 6 10 15
Since we want to do this for each column in VADeaths, we have to use the function apply.
> apply(VADeaths,2,cumsum)
Rural Male Rural Female Urban Male Urban Female
50-54 11.7 8.7 15.4 8.4
55-59 29.8 20.4 39.7 22.0
60-64 56.7 40.7 76.7 41.3
65-69 97.7 71.6 131.3 76.4
70-74 163.7 125.9 202.4 126.4
apply(VADeaths,2,cumsum) means: "For each column in VADeaths, calculate the cumsum of that".
This gives us the y values for each line of the bar plot.
Let's save these yvalues for further use:
> yvals <- as.vector(apply(VADeaths,2,cumsum))
The reason I use as.vector is just to flatten the matrix into a vector of values -- it makes the plotting easier.
One last thing -- my x values (that I stored in bar) only have one value per bar, but I need to expand it out so there's one x value per line on each bar. To do this:
> xvals <- rep(bar,each=nrow(VADeaths))
This turns my previous x1,x2,x3,x4 into x1,x1,x1,x1,x1, x2,x2,x2,x2,x2, ..., x4,x4,x4,x4,x4.
Now my xvals match my yvals.
After this it's simply a case of using text.
> text( xvals, yvals, labels=yvals, pos=3 )
The labels arguments tells text what text to put at the x/y positions.
The pos=3 means "draw each bit of text just above my specified x/y value". Otherwise, the numbers would be drawn over the lines of the barplot which would be hard to read.
Now, there are many options for customising the position and size of text, and I suggest you read ?text to see them.
All this code condenses down to the two-liner I gave at the beginning of the answer, but this version might be a little more understandable:
bar <- barplot(VADeaths)
xvals <- rep(bar,each=nrow(VADeaths))
yvals <- as.vector(apply(VADeaths,2,cumsum))
text( xvals, yvals, labels=yvals, pos=3 )

Resources