Map cut on the "edges" of x-axis with Aitoff projection - r

I am trying to plot with ggplot2 (v3.3.2) data points on a map using a specific projection (called aitoff), which is useful especially for sky plots.
When doing so, the plot is "cropped" on the x-axis, i.e. the edges of the axis are located just outside the plot. I tried a few things (adjust the margin for example), but without success. Could you please help to make these part of the plot visible?
Here is the code to reproduce the issue, i.e. the point located at (0,0) is not visible.
skydata <- data.frame(RA=c(0,180,360), Dec=c(0,10,20))
ggplot(skydata) +
geom_point(aes(RA,Dec)) +
coord_map(projection="aitoff",orientation=c(90,180,0)) +
scale_y_continuous(breaks=(-2:2)*30,limits=c(-90,90)) +
scale_x_continuous(breaks=(0:8)*45,limits=c(0,360), labels=c("","","","","","","","","")) +
labs(x="R.A.(°)", y="Decl. (°)",title="Map of the sky")
I hope I was clear enough...
Thanks a lot!

I think that the clipping happens in the first place is a known issue. In the Github issue hadley says: "I think this is a long standing problem that I'm unlikely to solve in the near future"
I think there are two ways, you can more or less solve the problem for yourself. One solution was already mentioned by #Arnaud
(but both solutions have downsides)
Add clip = "off" in the coord_map part
Add expand = c(1.1,0) in the scale_x_continuous part
I added you some example plots, where you can see the results and problems.
1. Initial version (if I run your code):
Problem: The point at (0,0) can't be seen properly.
2. Version with expand:
skydata <- data.frame(RA=c(0,180,360), Dec=c(0,10,20))
ggplot(skydata) +
geom_point(aes(RA,Dec)) +
coord_map(projection="aitoff", orientation=c(90,180,0)) +
scale_y_continuous(breaks=(-2:2)*30,limits=c(-90,90)) +
scale_x_continuous(breaks=(0:8)*45,limits=c(0,360), ,expand = c(1.1, 0), labels=c("","","","","","","","","")) +
labs(x="R.A.(°)", y="Decl. (°)",title="Map of the sky")
Looks quite nice now. x-axis got expanded to both sides, point in (0,0) now clearly visible.
But attention seems to work only with natural numbers (like expand = c(5,0)). For the 1.1 I chose in my example the plot is somehow different and the y-axis seems distorted.
3. Version with clip = "off":
skydata <- data.frame(RA=c(0,180,360), Dec=c(0,10,20))
ggplot(skydata) +
geom_point(aes(RA,Dec)) +
coord_map(projection="aitoff", clip = "off", orientation=c(90,180,0)) +
scale_y_continuous(breaks=(-2:2)*30,limits=c(-90,90)) +
scale_x_continuous(breaks=(0:8)*45,limits=c(0,360), labels=c("","","","","","","","","")) +
labs(x="R.A.(°)", y="Decl. (°)",title="Map of the sky")
This version does not expand the x-axis, but it makes sure, the point at (0,0) is not clipped off. Definitely no distortion of the y-axis. But does not look as good as the solution with expand.

Related

Line graph is blurry, not clear & axes positioning difficulties in R

I am trying to plot (with ggplot2) a simple time-velocity graph in R, but my data looks messy. I am using Markdown Notebook.
I am using this base code for this:
ggplot(data, aes(x = time, y = velocity)) + geom_line() +
scale_x_continuous(name="Time (s)",
limits=c(min(data$time),max(data$time)),breaks=seq(0,3000,500)) +
scale_y_continuous(name="", limits=c(-1,max(data$velocity))) +
theme(axis.text = element_text(color="black"),axis.title = element_text(
color="black"))
This is how it looks like with the default settings:
After that, I tried to extend the figure horizontally, but then the labels became really small, and the data is still kind of blurry:
For this, I added the following (at the beginning of my Chunk):
{r fig1, fig.width=95, fig.asp = 0.15}
If I make the font sizes bigger the labels look okay, but the velocity graph stays the same (naturally). Does someone know a way to fix that? I thought that maybe this is because of my monitor, it has 4K resolution, but I'm not sure. I also wonder if anyone knows how to move the y-axis so it would start at the zero point of the x-axes (now there's a space, and it looks weird).
I am also open to suggestions on how to improve the visualization. :D Thank you in advance!
with the changed DPI (comment) it looks better in my opinion, but the figure is really small (I included the output window for reference):

ggplot geom_tile right justify

I am trying to create what is essentially a Gantt chart using ggplot2. I am currently using geom_tile option in ggplot2 to produce something very close to what I need. On the x-axis is month, on the y axis is task, and the color of the blocks is hours of effort for that month.
The issue: The blocks drawn are centered on the month. I need them right justified so that when a month appears, the block sits to the right of the vertical gridline showing that month.
Is there an option like hjust for geom_tile? Here is my code thus far:
myGanttPlot <- ggplot(data=gantt_data, aes(x=workMonth, y=myTasks, fill=Hours, height=0.5)) +
geom_tile(hjust=1.0) +
scale_fill_distiller(palette="RdYlGn")
I get the error "Unknown parameters: hjust" with this code. Is there a better syntax I should use?
Shifting the value of workMonth by ~15 days should take care of this by centering the tiles between months, rather than on them.
ggplot(data=gantt_data, aes(x=workMonth + 60*60*24*15, y=myTasks, fill=Hours, height=0.5)) +
geom_tile() +
scale_fill_distiller(palette="RdYlGn")
Without a reproducible example, I can't test the code above, so please let me know if this solves your problem.

How to make the bg of a single legend transparent in a merged plot?

I don't have to mention I am new, and my problem has too many solutions. I tried about 12 different versions and couldn't solve it:
The example given is close to my desired plot I want to generate.
I overtook a given script from 2013, so I do not entirely understand what to do to change it in the way I would like:
Plot 1's legend in the bottom right corner without a title, transparent background and instead of 1 and 2 the labels "urban" and "non-urban".I am aware that "legend.position="none"" delets all legends, but was not able to find a solution that looked like at least close to this. Still it is not on the plot, not transparent and has a title.
Unfortunately somehow the dots changed into squares in this process and I have no clue why. I didn't change geom_point.
Another flaw I want to change is to remove the top line over the central plot. But how?
And last but not least I am not sure if the function geom.mooth(method=lm) does reflect the regression line + confidence interval, because the description says it adds a conditional mean which is, afaIk, not the same. Is my concern unnecessary?
Edit: shorter Version plot1 out of 3 merged plots:
library(ggplot2)
library(gridExtra)
set.seed(42)
DF <- data.frame(x=rnorm(100,mean=c(1,5)),y=rlnorm(100,meanlog=c(8,6)),group=1:2)
p1 <- ggplot(DF,aes(x=x,y=y,colour=factor(group))) + geom_point(shape=16) +
scale_x_continuous(expand=c(0.02,0)) +
scale_y_continuous(expand=c(0.02,0)) +
geom_rug() +
geom_smooth(method=lm,alpha=0.3) +
theme_bw()+
theme(legend.position=c(0.9,0.09),
legend.title=element_blank(),
plot.margin=unit(c(0,0,0,0),
"points"))
Thanks for any advice, I am researching on this topic since 2 weeks, even though I thought I am studying psychology, I learned a lot ... but not enough in the short time to success. :/

ggplot2 polar plot axis label location

This is just a extension for a old question
ggplot2 polar plot arrows
You will find the x axis is out of the most_out circle.
In ggplot2, I use "panel.grid.major = theme_line(colour = "black", size = 0.2, linetype=2)" to get the dashed circle, just as below:
So my question is how to make the axis label (180, 135, 90, .....) outside of the circle, because the text are merge with the circular lines.
I try to use "hjust" or "vjust" to adjust the distance between text and axis. But it does not work.
So do you have some ideas about this problem?
Thanks first!!!!
You have not provided code to reproduce the problem so this will be just a guess.
I've used whitespace, \n in particular, to move text "away" in the past. Perhaps a custom formatter might work here. Here is how you can write a custom tick mark label formatter.
If this fails, you can always hide the axis labels and paint them yourself using geom_text by adding another layer.
Hope this helps. #hadley's book on ggplot2 is very good, by the way.
I came across this question while I was trying to fix a similar issue myself. One workaround is pretty much covered in the answer to this post: Remove extra space and ring at the edge of a polar plot
You would have to adjust the limits of the x scale to match your axis labels. You would also have to create a new scale bar corresponding to the radial length of your arrows (the 0-300 scale bar on the left side of your plot), since
axis.text = element_blank
takes the scale bar away as well.

Irrelevant legend information in ggplot2

When running this code (go ahead, try it):
library(ggplot2)
(myDat <- data.frame(cbind(VarX=10:1, VarY=runif(10)),
Descrip=sample(LETTERS[1:3], 10, replace=TRUE)))
ggplot(myDat,aes(VarX,VarY,shape=Descrip,size=3)) + geom_point()
... the "size=3" statement does correctly set the point size. However it causes the legend to give birth to a little legend beneath it, entitled "3" and containing nothing but a big dot and the number 3.
This does the same
ggplot(myDat,aes(VarX,VarY,shape=Descrip)) + geom_point(aes(size=3))
Yes, it is funny. It would have driven me insane a couple hours ago if it weren't so funny. But now let's make it stop.
That's because it's interpreting it as an aesthetic mapping rather than a constant. This works I think:
ggplot(myDat,aes(VarX,VarY,shape=Descrip)) + geom_point(size=3)

Resources